Search code examples
ccmakesimgrid

SimGrid. Undefined references to imported functions from msg.h


I want to implement master-worker application by using simgrid by CLion code editor. I have such CMakeLists.txt:

project(FirstAgent)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.c)
add_executable(FirstAgent ${SOURCE_FILES})

The structure of main.c is following. MSG_init(), MSG_main, etc are functions from simgrid/msg.h. (If I ctrl+left-click on the function, the corresponding function in simgrid/msg.g will be opened):

#include <stdio.h>
#include <simgrid/msg.h>

int main(int argc, char *argv[]){
    MSG_init(&argc, argv);
    MSG_function_register("master", master);
    MSG_create_environment("platform.xml");
    MSG_launch_application("deployment.xml");
    MSG_main();
    return 0;
}
int worker(int argc, char *argv[]){...}
int master(int argc, char *argv[]){...}

But functions which I imported from simrid/msg.h can't be recognized. It gives an error:

/home/ken/CLionProjects/FirstAgent/main.c:10: undefined reference to `sg_version_check'
/home/ken/CLionProjects/FirstAgent/main.c:10: undefined reference to `MSG_init_nocheck'
/home/ken/CLionProjects/FirstAgent/main.c:12: undefined reference to `MSG_function_register'
/home/ken/CLionProjects/FirstAgent/main.c:13: undefined reference to `MSG_function_register'
/home/ken/CLionProjects/FirstAgent/main.c:15: undefined reference to `MSG_create_environment'
/home/ken/CLionProjects/FirstAgent/main.c:16: undefined reference to `MSG_launch_application'
/home/ken/CLionProjects/FirstAgent/main.c:18: undefined reference to `MSG_main'
...

How properly set up the project to avoid that?

UPD

After adding target_link_libraries(FirstAgent simgrid) only one error remains:

/home/ken/CLionProjects/FirstAgent/main.c:10: undefined reference to

`sg_version_check'
collect2: error: ld returned 1 exit status
make[3]: *** [FirstAgent/FirstAgent] Error 1
make[2]: *** [CMakeFiles/FirstAgent.dir/all] Error 2
make[1]: *** [CMakeFiles/FirstAgent.dir/rule] Error 2
make: *** [FirstAgent] Error 2

Solution

  • If you are missing every SimGrid symbols, that's because you need to add the following at the end of your CMakeLists.txt

    target_link_libraries(FirstAgent simgrid)

    If you are missing only sg_version_check, it may well be that you are using a library of the wrong version.

    If you are using Linux, try typing ldd yourprogram and to find the path to the simgrid library that is used at runtime. If it's an old one as I suspect, simply erase it and relaunch your program. The system should search for another instance of simgrid on your disk, probably the right one.

    If you are not using Linux, search your whole disk for forgotten simgrid libraries and remove them.