find_library(SPDLOG_LIBRARIES
NAMES
spdlog
spdlogd
REQUIRED
)
# SPDLOG_LIBRARIES is C:\spdlog\lib\spdlogd.lib
# can work
target_link_libraries(test_example PRIVATE spdlog::spdlog)
# can't work => no such spdlog/spdlog.h
target_link_libraries(test_example PRIVATE ${SPDLOG_LIBRARIES})
#include "spdlog/spdlog.h"
int main(int, char *[])
{
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
}
Why target_link_libraries(test_example PRIVATE ${SPDLOG_LIBRARIES})
can not work?
From what I see here, you linked directly to the library binaries (${SPDLOG_LIBRARIES}), not to its CMake target, so now you are missing include paths to the library headers. And if you linked to the target instead (if you have a CMake config for it, that is), most likely headers paths would’ve been set too (but that of course depends on how this library’s maintainers composed its config).
That spdlog::spdlog looks like a proper target, but nothing in your code fragment indicates where did it come from.
include_directories( ${SPDLOG _INCLUDE_DIRS} )
target_link_libraries( DisplayImage ${SPDLOG _LIBS} )
# they are the same thing
target_link_libraries( DisplayImage spdlog::spdlog )
They are not the same. CMake targets (like spdlog::spdlog) may carry more information, than just the include dirs, and what to link (linking flags, defines,…).
Generally, if a target is available, you should link to it instead of playing with the variables. That’s the modern way.