简体   繁体   English

如何使用CMake包含NTL

[英]how to include NTL using CMake

I use this line to compile a simple program: 我使用此行来编译一个简单的程序:

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

How do you include this into CMake? 您如何将其包含到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! 谢谢!

If ntl , m , and gmp libraries are usually installed to somewhere in the default path (eg /usr/ or /usr/local/ ), you could simply do something like: 如果通常将ntlmgmp库安装到默认路径中的某个位置(例如/usr//usr/local/ ),则可以执行以下操作:

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; 这可能最接近您的原始g ++命令,但是它并不十分健壮。 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 (ie while running CMake), you could add find_library calls for each of the required libs, eg 如果您想在配置时失败(即在运行CMake时),则可以为每个必需的库添加find_library调用,例如

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命令更改为

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++). 然后,您可能还必须为每个lib的头文件之一创建一个find_file ,以通过include_directories命令(对于g ++转换为-I )找出要添加的适当路径。


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. 请注意,将引号放在多余的CXX_FLAGS参数周围非常重要,否则CMake会将其视为列表中的单独值,并在标志之间插入分号。


For further information about find_library , find_file , etc. run: 有关find_libraryfind_file等的更多信息,请运行:

cmake --help-command find_library
cmake --help-command find_file

Regarding your error: 关于您的错误:

It doesn't look like there's a FindNTL.cmake module included with CMake. 看起来好像CMake中没有包含FindNTL.cmake模块。 That means you'll have to either: 这意味着您必须:

  • Write your own FindNTL.cmake , 编写自己的FindNTL.cmake
  • Find another that somebody else has written, 找到别人写的另一个,
  • Hack together a solution that: 共同解决以下问题:
    • Checks if NTL is installed 检查是否已安装NTL
    • Provides link targets, relevant flags, etc. 提供链接目标,相关标志等。

From a (rather quick) Google search, it appears somebody has an NTL module for CMake . 从一个(相当快的)Google搜索中,似乎有人拥有一个用于CMake的NTL模块 Since NTL use GMP, you will probably need the associated GMP module for CMake . 由于NTL使用GMP,因此您可能需要用于CMake的关联GMP模块 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). 它看起来不像功能齐全的CMake模块,但它似乎也是最好的东西(再次,这是一个快速的 Google搜索,我不使用NTL)。

To use, you'll want to add some things to your CMakeLists.txt : 要使用,您需要在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) 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. 您不想使用链接指令设置CXX_FLAGS I would use: 我会用:

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

to set the Cxx standard you want to use. 设置要使用的Cxx标准。 To actually link to libraries, you'll want to: 要实际链接到库,您需要:

  • Ensure that you've found the libraries (with the relevant find_package ( FOO ) lines) 确保已找到库(带有相关的find_package ( FOO )行)
  • 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; ${bar_LIBRARIES}${baz_LIBRARY}不是错字; there's no standard way of setting the relevant libraries in the FindFOO.cmake modules, which is, in my opinion, an annoyance. FindFOO.cmake模块中没有设置相关库的标准方法,我认为这很FindFOO.cmake 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. 如果一个不起作用,请尝试另一个,或者在最坏的情况下,查看相关的FindFOO.cmake文件(默认情况下,CMake中装有一堆文件)以查看每个文件的用途。 With the link i provided, you can use ${NTL_LIB} and ${GMP_LIB}. 通过提供的链接,您可以使用$ {NTL_LIB}和$ {GMP_LIB}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM