This has the huge benefit that you will probably get a diagnostic at CMake configure time if the expected library cannot be found, instead of a random linker error at compile time. So instead, always pass absolute paths to target_link_libraries and use find_library to resolve the link directory: find_library(PROTOBUF_LIBRARY protobuf HINTS /usr/lib/x86_64-linux-gnu) Pass theseĪbsolute library file paths directly to the target_link_libraries()Ĭommand. Library locations returnedīy find_package() and find_library() are absolute paths. Note that this command is rarely necessary. Even the manpage specifically advises against it: This is a common beginner's mistake, as many other build environments work like this, but in CMake it's just asking for trouble. Why is link_directories not allowing cmake to find this library?ĭo not use link_directories like this in CMake. target_link_libraries(test /usr/lib/x86_64-linux-gnu/libprotobuf.so), it compiles and links fine. However, if I remove the link_directories line in my CMakeLists.txt file, and instead write the full path to the library when specifying the executable, i.e. If I do a search through all the files and subdirectories in build, there is not mention of anything related to protobuf. However, I then get linker errors telling me that there are undefined references to some of the functions in the protobuf library. My main.cpp uses functions in this library, by including the releveant header file, #include. In /usr/lib/x86_64-linux-gnu, there is a shared library called libprotobuf.so, which I want to link against. Link_directories(/usr/lib/x86_64-linux-gnu) In src, I have two files: main.cpp, and CMakeLists.txt, which has (only) the following text: add_executable(test main.cpp) I Ubuntu, I am learning about cmake and make, and just trying a simple example.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |