Home >Backend Development >C++ >Why Does CMake's `link_directories` Fail to Find My Library Despite Correct Path Specification?

Why Does CMake's `link_directories` Fail to Find My Library Despite Correct Path Specification?

Linda Hamilton
Linda HamiltonOriginal
2024-12-10 20:31:15347browse

Why Does CMake's `link_directories` Fail to Find My Library Despite Correct Path Specification?

CMake: Link_Directories Fails to Locate Library

Issue:

In a CMake-based project with a src and build directory, a library named "protobuf" cannot be found despite using the link_directories command.

Details:

  • src/CMakeLists.txt includes a CMake configuration with link_directories pointing to "/usr/lib/x86_64-linux-gnu," the location of libprotobuf.so.
  • The CMakeLists.txt also includes add_executable and target_link_libraries commands.
  • main.cpp references functions from libprotobuf.so but encounters undefined reference errors at link time.
  • Using the full path to libprotobuf.so in the target_link_libraries command instead of link_directories results in successful linking.

Solution:

Ensure that the link_directories command appears before not only target_link_libraries but also add_executable in the CMakeLists.txt file.

The above is the detailed content of Why Does CMake's `link_directories` Fail to Find My Library Despite Correct Path Specification?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn