Looking around on the net I have seen a lot of code like this:
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
target_include_directories(app SYSTEM PUBLIC ${SDL2_INCLUDE_DIRS}
target_link_libraries(app ${SDL2_LIBRARIES})
However that seems to be the wrong way about doing it, as it only uses the include directories and libraries, but ignored defines, library paths and other flags that might be returned by pkg-config
.
What would be the correct way to do this and ensure that all compile and link flags returned by pkg-config
are used by the compiled app
? And is there a single command to accomplish this, i.e. something like target_use(app SDL2)
?
If you are looking to add definitions from the library as well, the
add_definitions
instruction is there for that. Documentation can be found here, along with more ways to add compiler flags.The following code snippet uses this instruction to add GTKGL to the project:
First of, the call:
should be replaced with:
The
find_package()
call is more flexible and allows options such asREQUIRED
, that do things automatically that one would have to do manually withinclude()
.Secondly, manually calling
pkg-config
should be avoid when possible. CMake comes with a rich set of package definitions, found in Linux under/usr/share/cmake-3.0/Modules/Find*cmake
. These provide more options and choice for the user then a raw call topkg_search_module()
.As for the mentioned hypothetical
target_use()
command, CMake actually already has that build-in in a way with PUBLIC|PRIVATE|INTERFACE. A call liketarget_include_directories(mytarget PUBLIC ...)
will cause the include directories to be automatically used in every target that usesmytarget
, e.g.target_link_libraries(myapp mytarget)
. However this mechanism seems to be only for libraries created within theCMakeLists.txt
file and does not work for libraries acquired withpkg_search_module()
. The calladd_library(bar SHARED IMPORTED)
might be used for that, but I haven't yet looked into that.As for the main question, this here works in most cases:
The
SDL2_CFLAGS_OTHER
contains defines and other flags necessary for a successful compile. The flagsSDL2_LIBRARY_DIRS
andSDL2_LDFLAGS_OTHER
are however still ignored, no idea how often that would become a problem.More docu at http://www.cmake.org/cmake/help/v3.0/module/FindPkgConfig.html
It's rare that one would only need to link with SDL2. The currently popular answer uses
pkg_search_module()
which checks for given modules and uses the first working one.It is more likely that you want to link with SDL2 and SDL2_Mixer and SDL2_TTF, etc...
pkg_check_modules()
checks for all the given modules.Disclaimer: I would have simply commented on Grumbel's self answer if I had enough street creds with stackoverflow.
There is no such command as
target_use
. But I know several projects that have written such a command for their internal use. But every project want to pass additional flags or defines, thus it does not make sense to have it in general CMake. Another reason not to have it are C++ templated libraries like Eigen, there is no library but you only have a bunch of include files.The described way is often correct. It might differ for some libraries, then you'll have to add
_LDFLAGS
or_CFLAGS
. One more reason for not havingtarget_use
. If it does not work for you, ask a new question specific about SDL2 or whatever library you want use.