如何在 CMake 项目中使用 Boost Multi precision?

问题描述 投票:0回答:1

我想在我的 C++ 项目中使用 Boost Multi precision,但我无法做到。我怀疑我没有在我的根

CMakeLists.txt
配置中添加正确的依赖项,其相关部分如下所示:

cmake_minimum_required(VERSION 3.30.0)
project(MyProject VERSION 1.0.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(src)
add_subdirectory(app)
add_subdirectory(test)

find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(MPFR REQUIRED)
include_directories(${MPFR_INCLUDE_DIRS})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

我确保在

app/CMakeLists.txt
中链接我的可执行文件的库:

add_executable(main main.cpp)
target_link_libraries(main PRIVATE myLib)
target_link_libraries(main ${Boost_LIBRARIES})
target_link_libraries(main ${MPFR_LIBRARIES})

以及我的图书馆

src/CMakeLists.txt

file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${MyProject_SOURCE_DIR}/include/*.h")
add_library(myLib SomeFile.cpp ${HEADER_LIST})
target_include_directories(myLib PUBLIC ../include)
target_link_libraries(myLib ${Boost_LIBRARIES})
target_link_libraries(myLib ${MPFR_LIBRARIES})

我已经通过包管理器验证了

Boost
MPFR
都安装在我的系统上。 当我包含必要的标头并使用多精度类型时,我的 linter (
Clangd
) 不会产生错误:

#include <boost/multiprecision/mpfr.hpp>
// ...

int main() {
    IOParser<boost::multiprecision::mpfr_float_100> parser;
    // ...
}

但是当我编译程序时,我得到了一些对各种 MPFR 函数的未定义引用,例如:

[build] /usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `boost::multiprecision::backends::detail::mpfr_float_imp<100u, (boost::multiprecision::mpfr_allocation_type)1>::operator=(boost::multiprecision::backends::detail::mpfr_float_imp<100u, (boost::multiprecision::mpfr_allocation_type)1>&&) [clone .isra.0]':
[build] main.cpp:(.text+0x2dc): undefined reference to `mpfr_swap'
[build] /usr/bin/ld: main.cpp:(.text+0x30c): undefined reference to `mpfr_set4'
[build] /usr/bin/ld: main.cpp:(.text+0x386): undefined reference to `mpfr_init2'

我做错了什么?

c++ cmake boost boost-multiprecision multiprecision
1个回答
0
投票

您需要链接到 MPFR 和/或 GMP,具体取决于代码中使用的后端。这些是传递依赖。

如果您坚持使用您的 cpp_* ,则库可以仅是标头,而不依赖于外部库。

© www.soinside.com 2019 - 2024. All rights reserved.