1

我使用这一行来编译一个简单的程序:

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

你如何将它包含到 CMake 中?

find_package(NTL REQUIRED)
find_package(GMP REQUIRED)

不工作。并给出以下错误:

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

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

也不起作用(但我认为这通常是错误的)。

谢谢!

4

2 回答 2

4

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
于 2012-05-08T00:52:31.817 回答
3

关于你的错误:

看起来FindNTL.cmakeCMake 中没有包含模块。这意味着您必须:

  • 自己写FindNTL.cmake
  • 再找一个别人写的,
  • 破解一个解决方案:
    • 检查是否安装了 NTL
    • 提供链接目标、相关标志等。

从(相当快速的)谷歌搜索中,似乎有人有一个用于 CMake 的 NTL 模块。由于 NTL 使用 GMP,您可能需要CMake 相关的 GMP 模块。它看起来不像一个功能齐全的 CMake 模块,但它似乎也是最好的东西(再次,它是一个快速的谷歌搜索,我不使用 NTL)。

要使用,你需要在你的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)

是的,这是错误的。你不想CXX_FLAGS链接指令设置你的。我会使用:

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

设置您要使用的 Cxx 标准。要实际链接到库,您需要:

  • 确保您找到了库(带有相关find_package ( FOO )行)
  • 将它们与您的目标联系起来,如下所示:

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

    请注意!${bar_LIBRARIES}并且${baz_LIBRARY}不是错字;没有在FindFOO.cmake模块中设置相关库的标准方法,在我看来,这很烦人。如果一个不起作用,请尝试另一个,或者,在最坏的情况下,查看相关FindFOO.cmake文件(默认情况下安装了一堆 CMake)以查看每个文件的用途。通过我提供的链接,您可以使用 ${NTL_LIB} 和 ${GMP_LIB}。

于 2012-05-07T23:56:01.460 回答