cmake build_error ai_generated true

CMake Error: Cannot specify link libraries for target which is not built by this project

ID: cmake/target-link-unknown

Also available as: JSON · Markdown
90%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3 active

Root Cause

target_link_libraries references a nonexistent target. Typo or wrong order in CMakeLists.txt.

generic

Workarounds

  1. 95% success Ensure add_executable is called before target_link_libraries
    add_executable(my_target main.cpp)
    target_link_libraries(my_target PRIVATE my_lib)

    Sources: https://cmake.org/cmake/help/latest/

  2. 90% success Check for typos between add_executable and target_link_libraries
  3. 85% success If target is in subdirectory, call add_subdirectory first

Dead Ends

Common approaches that don't work:

  1. Use add_custom_target to make it visible 78% fail

    add_custom_target is for custom commands, not linkable libraries

  2. Move target_link_libraries before add_executable 85% fail

    Target must exist before setting properties. add_executable must come FIRST.