How to link multiple libraries using CMake?

4,662 views
Skip to first unread message

heikki

unread,
Jul 4, 2013, 2:38:06 PM7/4/13
to dea...@googlegroups.com
Hi, I have a this kind of problem. Let's assume that I have a program and I have used deal.II to write this program. For example, we can consider step-1. Then I want to use some properties of other libraries like PETSc and SLEPc externally. (in this case deal.II is not compiled with PETSc or SLEPc) So, I don't want to compile deal.II library with libraries X and Y (even though it is possible). I just want to compile my program using deal.II, X and Y libraries. The question is how should I compile this program? Is it possible by modifying the content of the CMakeLists.txt file or should I do something else? An example, would help me greatly. I am sorry, that I am bothering you. Thanks!

-Heikki

Krzysztof Bzowski

unread,
Jul 4, 2013, 3:08:29 PM7/4/13
to dea...@googlegroups.com
Hi,
If I understand you correctly, you don't want to use deal.ii autopilot
macro. Instead you should write your own CMakeLists.txt from scratch.

I am using something like this to link with gmsh library - if you need
link PETSc or other libraries you need to find suitable find macro
(deal.ii has one or you can find another in google)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)

SET(TARGET "TEST_APP")
PROJECT(${TARGET})
SET(TARGET_SRC
main.cc
material.cc
utilities.cc
post_process.cc
)


FIND_PACKAGE( Boost REQUIRED COMPONENTS system filesystem )
LINK_DIRECTORIES ( ${Boost_LIBRARY_DIRS} )
INCLUDE_DIRECTORIES ( ${Boost_INCLUDE_DIRS} )

FIND_PACKAGE(deal.II 8.0 QUIET HINTS ${deal.II_DIR} ${DEAL_II_DIR}
$ENV{DEAL_II_DIR})
INCLUDE_DIRECTORIES(${DEAL_II_INCLUDE_DIRS})

FIND_PACKAGE(Gmsh REQUIRED $ENV{GMSH_DIR})
INCLUDE_DIRECTORIES(${GMSH_INCLUDE_DIRS})


SET(CMAKE_BUILD_TYPE "Debug" )

SET(CMAKE_CXX_FLAGS ${DEAL_II_CXX_FLAGS})
SET(CMAKE_CXX_FLAGS_RELEASE ${DEAL_II_CXX_FLAGS_RELEASE})
SET(CMAKE_CXX_FLAGS_DEBUG ${DEAL_II_CXX_FLAGS_DEBUG})

ADD_EXECUTABLE(${TARGET} ${TARGET_SRC})


TARGET_LINK_LIBRARIES(${TARGET}
${DEAL_II_LIBRARIES}
${Boost_FILESYSTEM_LIBRARY}
${GMSH_LIBRARY}
)

SET_TARGET_PROPERTIES(${TARGET} PROPERTIES
COMPILE_DEFINITIONS
"${DEAL_II_USER_DEFINITIONS}"
COMPILE_DEFINITIONS_DEBUG
"${DEAL_II_USER_DEFINITIONS_DEBUG}"
COMPILE_DEFINITIONS_RELEASE
"${DEAL_II_USER_DEFINITIONS_RELEASE}"
)
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES
LINK_FLAGS
"${DEAL_II_LINKER_FLAGS}"
LINK_FLAGS_DEBUG
"${DEAL_II_LINKER_FLAGS_DEBUG}"
LINK_FLAGS_RELEASE
"${DEAL_II_LINKER_FLAGS_RELEASE}"
)


Check also other examples of CMakeLists.txt here:
http://www.dealii.org/developer/development/cmakelists.html


--
Krzysztof

Matthias Maier

unread,
Jul 5, 2013, 6:12:53 PM7/5/13
to dea...@googlegroups.com
Hi,

just a small remark. :-)



You can shorten your example code by using the DEAL_II_SETUP_TARGET(...)
macro. It will setup include directories, compile definitions and the
link line for the target (in exactly the same way as you - or the
example in the documentation - have done by hand):

>
> CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
>
> SET(TARGET "TEST_APP")
> PROJECT(${TARGET})
> SET(TARGET_SRC
> main.cc
> material.cc
> utilities.cc
> post_process.cc
> )
>
> FIND_PACKAGE( Boost REQUIRED COMPONENTS system filesystem )
> LINK_DIRECTORIES ( ${Boost_LIBRARY_DIRS} )
> INCLUDE_DIRECTORIES ( ${Boost_INCLUDE_DIRS} )
>
> FIND_PACKAGE(deal.II 8.0 REQUIRED HINTS ${deal.II_DIR} ${DEAL_II_DIR} $ENV{DEAL_II_DIR})
>
> FIND_PACKAGE(Gmsh REQUIRED $ENV{GMSH_DIR})
> INCLUDE_DIRECTORIES(${GMSH_INCLUDE_DIRS})
>
> SET(CMAKE_BUILD_TYPE "Debug")
>
> SET(CMAKE_CXX_FLAGS ${DEAL_II_CXX_FLAGS})
> SET(CMAKE_CXX_FLAGS_RELEASE ${DEAL_II_CXX_FLAGS_RELEASE})
> SET(CMAKE_CXX_FLAGS_DEBUG ${DEAL_II_CXX_FLAGS_DEBUG})
>
> ADD_EXECUTABLE(${TARGET} ${TARGET_SRC})

DEAL_II_SETUP_TARGET(${TARGET})

>
> TARGET_LINK_LIBRARIES(${TARGET}
> ${Boost_FILESYSTEM_LIBRARY}
> ${GMSH_LIBRARY}
> )

You can even remove the manual build type and compile flag handling by
using DEAL_II_INITIALIZE_CACHED_VARIABLES() (which will do the same).

So, the bare minimum is something like:


CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)

FIND_PACKAGE(deal.II 8.0 REQUIRED HINTS ${deal.II_DIR} ${DEAL_II_DIR} $ENV{DEAL_II_DIR})
DEAL_II_INITIALIZE_CACHED_VARIABLES() # before the call to PROJECT(...)

PROJECT(TEST CXX)

FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem)
FIND_PACKAGE(Gmsh REQUIRED $ENV{GMSH_DIR})

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${GMSH_INCLUDE_DIRS})

ADD_EXECUTABLE(TEST_APP
main.cc
material.cc
utilities.cc
post_process.cc
)

DEAL_II_SETUP_TARGET(${TARGET})

TARGET_LINK_LIBRARIES(${TARGET}
${Boost_FILESYSTEM_LIBRARY}
${GMSH_LIBRARY}
)


Best,
Matthias
Reply all
Reply to author
Forward
0 new messages