[CMake] How can I write CMakeLists.txt to link a special library?

Michael Wild themiwi at gmail.com
Mon Dec 15 04:46:56 EST 2008


On 14. Dec, 2008, at 5:41, Kermit Mei wrote:

> Werner Smekal wrote:
>> Hi,
>>
>> Kermit Mei wrote:
>>> Hello, all.
>>> I used sigc++ library in my program, so I must compile my main.cc  
>>> like this:
>>>
>>> c++ -Wall main.cc -o main -I/usr/include/sigc++-2.0 -I/usr/lib/sigc 
>>> ++-2.0/include  -lsigc-2.0
>>>
>>> Then, how can I write the CMakeLists.txt? I worte it like this:
>>>
>>> cmake_minimum_required(VERSION 2.6)
>>>
>>> PROJECT(MEM_BER)
>>> SET(SRC_LIST main.cc)
>>>
>>> MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})
>>> MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR})
>>>
>>> INCLUDE_DIRECTORIES(/usr/include/sigc++-2.0)
>>> INCLUDE_DIRECTORIES(/usr/lib/sigc++-2.0/include)
>>>
>>> LINK_DIRECTORIES(/usr/lib/)
>>>
>>> ADD_EXECUTABLE(main ${SRC_LIST})
>>>
>>> #################################
>>>
>>> But I can't link the libsigc.
>>
>> http://www.cmake.org/cmake/help/ 
>> cmake2.6docs.html#command:target_link_libraries
>>
>> target_link_libraries(main /usr/lib/libsigc-2.0.so)
>> (whatever sigc library is called and where it is, don't know)
>>
>> HTH,
>> Werner
>>
> Yes, I see. Thanks.
>

But then, you probably want to use find_path and find_library to make  
things more flexible. Even better, create a FindLibSigCXX2.cmake  
module which will do things properly and can be reused. Here is a  
proposed implementation:

# - Try to find libsigc++-2.0
# Once done this will define
#
#  LIBSIGCXX2_FOUND - System has libsigc++2
#  LIBSIGCXX2_INCLUDE_DIRS - The libsigc++2 include directories
#  LIBSIGCXX2_LIBRARIES - The libraries needed to use libsigc++2

# Copyright (c) 2008, Michael Wild <themiwi at users.sf.net>
#
# Redistribution and use is allowed according to the terms of the BSD  
license.

if (NOT WIN32)
   # use pkg-config to get the directories and then use these values
   # in the FIND_PATH() and FIND_LIBRARY() calls
   find_package( PkgConfig REQUIRED )
   pkg_check_modules( _sigcxx2  REQUIRED sigc++-2.0 )
endif (NOT WIN32)

# find sigc++/sigc++.h
find_path( LIBSIGCXX2_sigcxx_h_DIR sigc++/sigc++.h
   PATHS ${_sigcxx2_INCLUDE_DIRS}
   PATH_SUFFIXES sigc++-2.0
   DOC "Include directory for sigc++.h"
   )

# find sigc++config.h
find_path( LIBSIGCXX2_sigcxxconfig_h_DIR sigc++config.h
   PATHS ${_sigcxx2_INCLUDE_DIRS}
   DOC "Include directory for sigc++config.h"
   )

# combine the two paths
set( LIBSIGCXX2_INCLUDE_DIRS ${LIBSIGCXX2_sigcxx_h_DIR} $ 
{LIBSIGCXX2_sigcxxconfig_h_DIR} )

# find the sigc-2.0 library
find_library( LIBSIGCXX2_LIBRARIES
   NAMES sigc-2.0 ${_sigcxx2_LIBRARIES}
   PATHS ${_sigcxx2_LIBRARY_DIRS}
   DOC "Libraries for libsigc++2"
   )

# let find_package_handle_standard_args handle all the boilerplate stuff
include( FindPackageHandleStandardArgs )
find_package_handle_standard_args(
   LibSigCXX2 LIBSIGCXX2_LIBRARIES LIBSIGCXX2_sigcxx_h_DIR  
LIBSIGCXX2_sigcxxconfig_h_DIR
   )

# mark these cache entries as advanced
mark_as_advanced( LIBSIGCXX2_LIBRARIES LIBSIGCXX2_sigcxx_h_DIR
   LIBSIGCXX2_sigcxxconfig_h_DIR LIBSIGCXX2_INCLUDE_DIR
   )




Put this in some directory where CMake can find it or adjust  
CMAKE_MODULE_PATH in your CMakeLists.txt accordingly. Then, in your  
CMakeLists.txt do the following:

find_package( LibSigCXX2 REQUIRED )
if( NOT LIBSIGCXX2_FOUND )
   message( SEND_ERROR "Failed to find the required package 'libsigc+ 
+' )
endif( NOT LIBSIGCXX2_FOUND )

include_directories( ${LIBSIGCXX2_INCLUDE_DIRS} )

...

target_link_libraries( main ${LIBSIGCXX2_LIBRARIES} )


HTH

Michael


More information about the CMake mailing list