Home >Backend Development >C++ >Why is CMake Failing to Find My Library When Using `link_directories`?
CMake Failing to Locate Library with "link_directories" Call
This issue occurs when attempting to link a library using the "link_directories" directive in CMake but encountering linker errors indicating undefined references to library functions.
In the provided example, the "link_directories" call is placed incorrectly. To resolve this problem, ensure that the "link_directories" call is located before the "add_executable" call in your CMakeLists.txt file.
Here is the modified CMakeLists.txt file:
link_directories(/usr/lib/x86_64-linux-gnu) add_executable(test main.cpp) target_link_libraries(test protobuf)
By placing "link_directories" before "add_executable," CMake properly identifies the library during linking and resolves the undefined reference errors.
It's worth noting that the specific error messages and path of the library may vary depending on your system and the library being linked against. However, the general principle of ensuring that "link_directories" precedes "add_executable" remains the same.
The above is the detailed content of Why is CMake Failing to Find My Library When Using `link_directories`?. For more information, please follow other related articles on the PHP Chinese website!