Taozheng Yang
Taozheng Yang

Reputation: 11

how to include gecode as an external library in my c++ project using cmake?

I am new to CMake, and need some directions on a simple issue: I build the gecode from source in my Ubuntu 18.04 machine by following this link. The library is installed in /opt/gecode-release-6.2.0 directory. I have a C++ simple project whose source code make use of the gecode library functions, and the project is built using CMake. However it seems like the computer cannot find where the gecode is installed.

I have tried the following in my CMakeLists.txt hoping the gecode library can be correctly located, but none is working:

error message when "make" the c++ project

my current non-working CmakeLists.txt

Upvotes: 0

Views: 427

Answers (2)

Dekker1
Dekker1

Reputation: 5786

Gecode is a rather complicated library with its various "sub-libraries" or components, so I would like to offer an alternative to direct adding of find and link commands in your CMake file.

Gecode can be used as a separate CMake package. I won't go into details what this means, but once you define Gecode as a package you can use the find_package() function to find and link to Gecode, (eg, find_package(Gecode 6.0 COMPONENTS Driver Kernel) would require at least Gecode version 6 and require the driver and kernel components). As Gecode doesn't ship as a CMake package, to treat Gecode as a package you have to define a find module. A fully functional find module for gecode can be found in the MiniZinc repository: https://github.com/MiniZinc/libminizinc/blob/master/cmake/modules/FindGecode.cmake This find module will have the instructions to find Gecode and will define both imported targets and variables with the information of where the libraries are located and the associated header files.

A simple example of linking a target test with Gecode will then look as follows:

# Add the place of the find module to the module path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# Find Gecode
find_package(Gecode 6.0 REQUIRED COMPONENTS Driver Int Kernel Search)
# Link Gecode to target test
target_link_libraries(test Gecode::Driver Gecode::Int Gecode::Kernel Gecode::Search)

Note that the components required will be different depending on your application. It is also possible to use the GECODE_INCLUDE_DIRS and GECODE_LIBRARIES defined by the find module; however, this might give you problems if you then export your library as a CMake package.

Disclaimer: I currently maintain the CMake setup for the MiniZinc project.

Upvotes: 1

Taozheng Yang
Taozheng Yang

Reputation: 11

I have figured out a working version, the CmakeLists.txt is as shown in the picture below: enter image description here

Upvotes: 0

Related Questions