In CMake, you do not link projects to other projects. Instead, you link targets to other targets.
CMake targets are only created via a few commands (such as add_library
, add_executable
, and add_custom_target
). The project
command does not create a CMake target, it merely declares a project.
Furthermore, the target_link_libraries()
command accepts the following arguments after the scoping keyword:
- A library target name
- A full path to a library file
- A plain library name
- A link flag
- A generator expression
- A
debug
, optimized
, or general
keyword
It does not accept project names, although if you put a project name, it will instead look for a CMake target or library file on your system with that name.
To get to the root of what I believe you're asking: If you provide link-item name to target_link_libraries()
that does not match an existing target, the command will simply search for a library file of that name instead.
To check if a target exists before trying to link it, you can do:
if (TARGET StackOverflow)
target_link_libraries(Stuff PUBLIC StackOverflow)
endif()
I suggest reading through the linked target_link_libraries()
documentation if you want more details about what this command does.