2

I use this line to compile a simple program:

g++ main.cc -lntl -lm -lgmp 

How do you include this into CMake?

find_package(NTL REQUIRED)
find_package(GMP REQUIRED)

Doesn't work. And gives the following error:

CMake Error at CMakeLists.txt:30 (find_package):
  Could not find module FindNTL.cmake or a configuration file for package
  NTL.
...
.

and

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++0x -lntl -lm -lgmp)

Doesn't work either (but I think it's just wrong in general).

Thank you!

kirill_igum
  • 3,953
  • 5
  • 47
  • 73

2 Answers2

5

If ntl, m, and gmp libraries are usually installed to somewhere in the default path (e.g. /usr/ or /usr/local/), you could simply do something like:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(test main.cc)
target_link_libraries(test ntl m gmp)


This is probably closest to your original g++ command, but it isn't very robust however; if any of the libraries aren't found, you won't know about it until you try linking. If you want to fail at configure time (i.e. while running CMake), you could add find_library calls for each of the required libs, e.g.

find_library(NTL_LIB ntl)
if(NOT NTL_LIB)
  message(FATAL_ERROR "ntl library not found.  Rerun cmake with -DCMAKE_PREFIX_PATH=\"<path to lib1>;<path to lib2>\"")
endif()

You'd then have to change your target_link_libraries command to

target_link_libraries(test ${NTL_LIB} ${M_LIB} ${GMP_LIB})

You'd probably also then have to do a find_file for one of each lib's header files to find out the appropriate path to add via the include_directories command (which translates to -I for g++).


Note, it's important to put quotes around the extra CXX_FLAGS arguments, or CMake treats them like separate values in a list and inserts a semi-colon between the flags.


For further information about find_library, find_file, etc. run:

cmake --help-command find_library
cmake --help-command find_file
Fraser
  • 74,704
  • 20
  • 238
  • 215
3

Regarding your error:

It doesn't look like there's a FindNTL.cmake module included with CMake. That means you'll have to either:

  • Write your own FindNTL.cmake,
  • Find another that somebody else has written,
  • Hack together a solution that:
    • Checks if NTL is installed
    • Provides link targets, relevant flags, etc.

From a (rather quick) Google search, it appears somebody has an NTL module for CMake. Since NTL use GMP, you will probably need the associated GMP module for CMake. It doesn't look like a fully-featured CMake module, but it also appears to be the best thing out there (again, it was a quick Google search, and I don't use NTL).

To use, you'll want to add some things to your CMakeLists.txt:

# Let CMake know where you've put the FindNTL.cmake module. 
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/CMake/Modules")
# Call the FindNTL module:
find_package(NTL REQUIRED)

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++0x -lntl -lm -lgmp)

Yes, this is wrong. You don't want to be setting your CXX_FLAGS with linking directives. I would use:

SET ( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=cxx0x )

to set the Cxx standard you want to use. To actually link to libraries, you'll want to:

  • Ensure that you've found the libraries (with the relevant find_package ( FOO ) lines)
  • Link those against your target, like this:

    # Build the Foo executable. (Or library, or whatever)
    add_executable (FooEXE ${Foo_SOURCES} )
    target_link_libraries (FooEXE
        ${bar_LIBRARIES}
        ${baz_LIBRARY}
        )
    

    Please note! ${bar_LIBRARIES} and ${baz_LIBRARY} is not a typo; there's no standard way of setting the relevant libraries in the FindFOO.cmake modules, which is, in my opinion, an annoyance. If one doesn't work, try the other, or, worst case, have a look in the relevant FindFOO.cmake file (there's a bunch installed with CMake by default) to see what each one uses. With the link i provided, you can use ${NTL_LIB} and ${GMP_LIB}.

simont
  • 68,704
  • 18
  • 117
  • 136