Home > Back-end >  where does target_link_libraries look for the required files?
where does target_link_libraries look for the required files?

Time:05-02

I am building a project from source and trying to understand what is happening in the CMakeLists.txt files. where exactly target_link_libraries looking for the required library files?

the specific CMakeLists.txt file I have has:

target_link_libraries(MyApplication PRIVATE
Magnum::Application
Magnum::GL
Magnum::Magnum
Magnum::Shaders)

I found folders that have the names GL and Shaders in a directory called Magnum in the project, and they have a collection of header files in them. I believed that target_link_libraries is telling cmake to include the libraries in the GL and Shaders folder. However, I cannot seem to find a corresponding folder for Application, hence my line of reasoning must be flawed. What I do know is target_link_libraries is doing something related to allowing the finally put together program to be able to use a set of libraries.

What exactly does target_link_libraries do? where does it look for the required files in order to be able to use the libraries it needs to?

CodePudding user response:

target_link_libraries doesn't link anything automatically. You should have a target previously created via add_library (or add_executable), where all files are listed.

The way these targets added into your CMake project may differ. E.g. you may have a library source files with CMakeLists.txt configuration (where the said add_library command is) under some folder libs/mylib. Then in your CMakeLists.txt you may have the library added with add_subdirectory(libs/mylib). Another option is to add the library with find_package.

CodePudding user response:

In your specific case, you pass targets to target_link_libraries(). Targets with a shape like Magnum::Application etc are either imported targets or ALIAS targets. From what you say, it seems than Magnum is vendored into your project, so I guess your are linking ALIAS targets, like the one defined here https://github.com/mosra/magnum/blob/cfc02599e54e02337dd56bb61f70b2e61eb9ce8d/src/Magnum/CMakeLists.txt#L295

Targets defined by add_library() or add_executable() in CMake are an abstraction carrying several informations, including location of files.

  • Related