Issue
My understanding is that if I want to use a 3rd library, I need the header file path and the link obj path.
in CMake, I think a successful find_package()
will set 2 variables: somelib_INCLUDE_DIRS
and somelib_LIBRARIES
.
But in many case, I only call
target_link_libraries(myexe
${somelib_LIBRARIES} )
and it seems to be sufficient. (somelib_INCLUDE_DIRS
is not used)
My question is: Is my experience correct? somelib_INCLUDE_DIRS
is not necessary?
If it's correct, can I say the reason is that target_link_libraries
calls target_include_directories
internally?
Thanks.
Solution
Will cmake target_link_libraries() call target_include_directories()?
If target_link_libraries
is called with a library target name that has PUBLIC
or INTERFACE
include directories, then target_link_libraries
will also add those include directories to the target. I.e. INTERFACE properties are "transitive" or are "inherited".
Is my experience correct? somelib_INCLUDE_DIRS is not necessary?
That 100% just depends on the specific library configuration. If the condition described above is satisfied, then yes. If not, somelib_LIBRARIES
has libraries or link flags and not CMake library target names, or those library target names do not have any PUBLIC or INTERFACE include directories, etc...., then not.
Some find_package
configuration files have been rewritten with add_library(... INTERFACE
. Some not. It depends on the specific library CMake configuration. Consult the documentation and source code of the specific library CMake configuration files to know how to use it.
If it's correct, can I say the reason is that target_link_libraries calls target_include_directories internally?
Yes.
Answered By - KamilCuk Answer Checked By - Candace Johnson (WPSolving Volunteer)