尝试在CLion中将Boost与CMake结合使用

时间:2020-07-09 10:38:32

标签: c++ macos boost cmake gmp

我是C ++的新手,遇到了一个在Internet上找不到解决方案的问题。

我的主板上装有Mac OS Catalina,brew附带捆绑了CMake,Boost和gmp。

因此,尝试使用Boost(特别是多精度lib)来估计大斐波那契数。这是代码:

#include <iostream>
#include <ctime>
#include <boost/multiprecision/gmp.hpp>

using namespace boost::multiprecision;

int main() {
    unsigned int start = clock();

    mpz_int numFirst = 0;
    mpz_int numSecond = 1;

    mpz_int numNext = 0;

    int n;
    std::cout << "n = ";
    std::cin >> n;

    for (int i = 1; i <= n; i++)
    {
        numNext = numFirst + numSecond;
        numFirst = numSecond;
        numSecond = numNext;
    }

    std::cout << n << " Fib's number: " << numNext << std::endl;

    std::cout << "Working time: " << clock() - start;


    return 0;
}

通过按Command + J,用CLion的模板填充CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(testing)

set(CMAKE_CXX_STANDARD 20)

# see https://cmake.org/cmake/help/latest/module/FindBoost.html
find_package(Boost REQUIRED)

include_directories(${Boost_INCLUDE_DIR})

add_executable(testing main.cpp)

# Note: a target should be already defined using 'add_executable' or 'add_library'
target_link_libraries(testing ${Boost_LIBRARIES})

编译程序有错误:

In file included from /Users/joelcolbeck/CLionProjects/testing/main.cpp:3:
/usr/local/Cellar/boost/1.72.0_3/include/boost/multiprecision/gmp.hpp:31:10: fatal error: 'gmp.h' file not found

因此,我试图通过编写包含目录的完整路径来在CMakeLists.txt中包含gmp:

...
include_directories(${Boost_INCLUDE_DIR} /usr/local/Cellar/gmp/6.2.0/include)

add_executable(testing main.cpp)

# Note: a target should be already defined using 'add_executable' or 'add_library'
target_link_libraries(testing ${Boost_LIBRARIES} /usr/local/Cellar/gmp/6.2.0/lib)

然后,在编译程序时,出现链接器错误:

Undefined symbols for architecture x86_64:
  "___gmp_get_memory_functions", referenced from:
      boost::multiprecision::backends::gmp_int::str(long, unsigned int) const in main.cpp.o
  "___gmpz_add", referenced from:
      boost::multiprecision::backends::eval_add(boost::multiprecision::backends::gmp_int&, boost::multiprecision::backends::gmp_int const&, boost::multiprecision::backends::gmp_int const&) in main.cpp.o
  "___gmpz_clear", referenced from:
      boost::multiprecision::backends::gmp_int::~gmp_int() in main.cpp.o
  "___gmpz_get_str", referenced from:
      boost::multiprecision::backends::gmp_int::str(long, unsigned int) const in main.cpp.o
  "___gmpz_init", referenced from:
      boost::multiprecision::backends::gmp_int::operator=(long) in main.cpp.o
      boost::multiprecision::backends::gmp_int::gmp_int() in main.cpp.o
      boost::multiprecision::backends::gmp_int::operator=(boost::multiprecision::backends::gmp_int const&) in main.cpp.o
  "___gmpz_set", referenced from:
      boost::multiprecision::backends::gmp_int::operator=(boost::multiprecision::backends::gmp_int const&) in main.cpp.o
  "___gmpz_set_si", referenced from:
      boost::multiprecision::backends::gmp_int::operator=(long) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [testing] Error 1
make[2]: *** [CMakeFiles/testing.dir/all] Error 2
make[1]: *** [CMakeFiles/testing.dir/rule] Error 2
make: *** [testing] Error 2

不知道该怎么办。我看了一些关于链接器如何工作的视频,但没有任何类似的问题

0 个答案:

没有答案