Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting away with only writing target_link_libraries(not specify include path) in CMakeLIsts.txt

Tags:

c++

linker

cmake

I found that when I use target_link_libraries in CMaksList.txt , I get to ignore the include path(target_include_directories) and run the application successfully for example :

cmake_minimum_required(VERSION 3.5)
project(hello)

find_package(OpenCV REQUIRED)
add_executable(test test.cpp) #or add_library(test SHARED test.cpp)

target_link_libraries(  yolo
                        ${OpenCV_LIBS})

And it turns out that everything works correctly and I am able to run application without any problem. Here${OpenCV_LIBS} and ${InferenceEngine_LIBRARIES} is just.so file locate in somewhere in the system.

I would like to know why this is working ? In the other word, What kind of information does .so file contain ? Does it contains include path ? How does this work behind the scene ? Thanks !

like image 968
Pro_gram_mer Avatar asked Oct 27 '25 23:10

Pro_gram_mer


1 Answers

The CMake docs for target_include_directory state:

The INTERFACE, PUBLIC and PRIVATE keywords are required to specify the scope of the following arguments. PRIVATE and PUBLIC items will populate the INCLUDE_DIRECTORIES property of . PUBLIC and INTERFACE items will populate the INTERFACE_INCLUDE_DIRECTORIES property of <target>.

The documentation of INTERFACE_INCLUDE_DIRECTORIES states:

Targets may populate this property to publish the include directories required to compile against the headers for the target. The target_include_directories() command populates this property with values given to the PUBLIC and INTERFACE keywords. Projects may also get and set the property directly.

When target dependencies are specified using target_link_libraries(), CMake will read this property from all target dependencies to determine the build properties of the consumer.

In short, the libraries in opencv_LIBS likely each specified their public headers (those that you will #include as a user of the library) via target_include_directories, so that they would automatically be added to the include path when linked against. It's one of the nice things about CMake targets when set up correctly by library authors (which in this case it was).

like image 113
starball Avatar answered Oct 29 '25 15:10

starball