Home >Backend Development >C++ >Why Does CMake's `link_directories` Fail to Find Libraries Unless Placed Before `add_executable`?

Why Does CMake's `link_directories` Fail to Find Libraries Unless Placed Before `add_executable`?

Barbara Streisand
Barbara StreisandOriginal
2024-12-02 13:09:13281browse

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!

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