cmake does not report an error when linking to nonexisting target
Why does cmake not issue an error in this case, where special_lib
does not exist as target?
I want to get warned about this, since perhaps I'm having problems betting main.cpp
to compile because of header problems, where I'm assuming that transitive interface include_directories
are coming from target special_lib
... but oh man... special_lib
does not even exist.
We need errors in this case! (don't we?) Imagine a spelling mistake of the LINK_LIBRARY in target_link_libraries
. I'd be messing around with INCLUDE_DIRECTORIES, when all the while the mistake is a type in the target_link_libraries
line.
cmake_minimum_required(VERSION 3.12)
project(go)
file(WRITE ${CMAKE_BINARY_DIR}/main.cpp
"#include <iostream>
int main() {}")
file(WRITE ${CMAKE_BINARY_DIR}/emtpy.cpp
"")
add_executable(go ${CMAKE_BINARY_DIR}/main.cpp)
target_link_libraries(go special_lib) # WHY DOES CMAKE NOT REPORT AN ERROR,
# if special_lib does not exists as target?
#add_library(special_lib ${CMAKE_BINARY_DIR}/emtpy.cpp) # uncomment this line to fix
get_property(liblist TARGET go PROPERTY LINK_LIBRARIES)
foreach(lib IN LISTS liblist)
if (NOT TARGET ${lib})
message(WARNING "Warning: target ${lib} does not exists")
endif()
endforeach()
My own warning-message is brittle, since special_lib might be defined later. Surely cmake must be able to iterate through targets at the end and check that all LINK_LIBRARIES actually exist. (Does it not build up an internal C++ representation of this stuff?)