[CMake] Using cmake's UsePkgConfig
Alexander Neundorf
a.neundorf-work at gmx.net
Mon Jul 17 16:05:35 EDT 2006
Hi Antti,
Datum: Mon, 17 Jul 2006 18:09:23 +0300
> Hey.
>
> This is probably bit of a newbie question but good information about
> cmake is hard to come by.
>
> I wanted to build against GTK2 with cmake. The FindGTK module is only
> capable of detecting GTK 1.x and proved to be useless. So, I opted to
> use pkg-config, because it's probably what autotools uses as well.
>
> This is a piece of programming that I was able to make to work. The
> first part detects GTK1, and the next part detects GTK2. The defines are
> used to adapt the .c sources for the minor interface differences between
> the two.
>
>
> FIND_PACKAGE(GTK)
> IF (GTK_FOUND)
> INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIR})
> ADD_DEFINITIONS(-DHAVE_GTK)
> MESSAGE("GTK 1.x found and used as GUI\n")
> ELSE (GTK_FOUND)
> PKGCONFIG(gtk+-2.0 GTK2_INCLUDE_DIR GTK2_LINK_DIR GTK2_LINK_FLAGS
> GTK2_CFLAGS)
> PKGCONFIG(gthread-2.0 GTHREAD_INCLUDE_DIR GTHREAD_LINK_DIR
> GTHREAD_LINK_FLAGS+- GTHREAD_CFLAGS)
>
> IF (GTK2_INCLUDE_DIR)
> # ok, I can get compile work through CFLAGS
> SET(CMAKE_C_FLAGS "${GTK2_CFLAGS} ${GTHREAD_CFLAGS}")
> # but to include libraries I have to do these awful hacks
> # remove -l from in front of library
> STRING(REGEX REPLACE "-l" "" GTK_LIBRARIES "${GTK2_LINK_FLAGS}
> ${GTHREAD_LINK_FLAGS}")
> SEPARATE_ARGUMENTS(GTK_LIBRARIES)
> ADD_DEFINITIONS(-DHAVE_GTK2)
> MESSAGE("GTK 2.x found and used as GUI\n")
> ELSE (GTK2_INCLUDE_DIR)
> MESSAGE(FATAL_ERROR "GTK or GTK2 is required to build this
> project.")
> ENDIF (GTK2_INCLUDE_DIR)
> ENDIF (GTK_FOUND)
>
> I'd like to focus on the part inside IF (GTK2_INCLUDE_DIR). As can be
> seen, I'm "converting" the -lfoo -lbar type line from the link flags to
> library names and then, eventually, passing the variable GTK_LIBRARIES
> to the TARGET_LINK_LIBRARIES macro. Is there some easier macro to use
> that goes into the link line without this parsing work to convert
> things? This is obviously especially important if the link line happens
> to contain other but -l parameters. (or if the library name contains -l,
> such as "art-lgpl" ;-)
>
> I'd obviously prefer to be able to use PKGCONFIG in a manner like this:
>
> PKGCONFIG(some-library SOME_LIBRARY)
> # defines many magic variables, x_FOUND, x_DIRECTORIES, x_CFLAGS,
> x_LIBRARIES, etc.
>
> IF (SOME_LIBRARY_FOUND)
> ADD_DIRECTORIES(SOME_LIBRARY_DIRECTORIES) # include dirs:
> -I/some/path/here
> ADD_CFLAGS(SOME_LIBRARY_CFLAGS) # additional cflags:
> -D_REENTRANT, -msse or whatever
> ENDIF (SOME_LIBRARY_FOUND)
>
> And finally when compiling, this should be done:
>
> TARGET_LINK_LIBRARIES(${targetname} SOME_LIBRARY_LIBRARIES)
The following works for me (at least neither linker nor compiler complained):
include(UsePkgConfig)
PKGCONFIG(gtk+-2.0 GTK2_INCLUDE_DIR GTK2_LINK_DIR GTK2_LINK_FLAGS
GTK2_CFLAGS)
message(STATUS "inc: -${GTK2_INCLUDE_DIR}")
message(STATUS "link flags: -${GTK2_LINK_FLAGS}")
message(STATUS "cflags: -${GTK2_CFLAGS}")
message(STATUS "link dir: -${GTK2_LINK_DIR}")
# main.c is basically empty, but so we have
# an executable where we can link libs to
add_executable(blub main.c)
target_link_libraries(blub ${GTK2_LINK_FLAGS} )
This is the output:
hammer:~/src/tests/pkgconftest$ cmake .
-- inc: -/usr/include
-- link flags: --Wl,--export-dynamic -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0
-- cflags: --I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/pango-1.0 -I/usr/X11R6/include -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
-- link dir: -/usr/lib
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/src/tests/pkgconftest
hammer:~/src/tests/pkgconftest$ make VERBOSE=1
...
Linking C executable blub
/usr/local/bin/cmake -P CMakeFiles/blub.dir/cmake_clean_target.cmake
/usr/bin/gcc -fPIC "CMakeFiles/blub.dir/main.o" -o blub -rdynamic -Wl,--export-dynamic -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0
make[2]: Leaving directory `/home/src/my_stuff/tests/pkgconftest'
Doesn't it work for you ?
Bye
Alex
--
Echte DSL-Flatrate dauerhaft für 0,- Euro*!
"Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl
More information about the CMake
mailing list