Home >Backend Development >C++ >Why Does CMake's `link_directories` Fail to Find Libraries Unless Placed Before `add_executable`?
CMake's "link_directories" Fails to Locate Libraries
When encountering linker errors indicating missing references to functions from a library, despite using "link_directories" in CMake, it's crucial to inspect the order of your commands. The solution lies in ensuring that the "link_directories" directive precedes the "add_executable" invocation, as demonstrated below:
link_directories(/usr/lib/x86_64-linux-gnu) add_executable(test main.cpp)
Initially, the misconception was that "link_directories" only needed to appear before "target_link_libraries." However, placing it before "add_executable" resolved the issue, allowing CMake to successfully link the required library.
The above is the detailed content of Why Does CMake's `link_directories` Fail to Find Libraries Unless Placed Before `add_executable`?. For more information, please follow other related articles on the PHP Chinese website!