简单Boost序列化中的未定义参考错误 [英] Undefined reference errors in simple boost serialization

查看:333
本文介绍了简单Boost序列化中的未定义参考错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Boost序列化的最小示例,我尝试在二进制存档文件中保存一个整数

I have a minimal example of Boost serialization where I try to save an integer in a binary archive file

这是main.cpp:

Here is main.cpp:

#include <iostream>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>

int main() {
    int t = 0;
    std::ofstream file("Test.bin");
    boost::archive::binary_oarchive archive(file);
    archive << t;
    file.close();
    return 0;
}

这是CMake文件:

cmake_minimum_required(VERSION 3.15)
project(Test)

set(CMAKE_CXX_STANDARD 17)
find_package(Boost REQUIRED serialization)
add_executable(Test main.cpp)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(Test ${Boost_LIBRARIES})
endif()

当我尝试在CLion中运行此程序时,我得到了大量未定义的参考错误,如下所示: https://pastebin.com/8uX9MZFf

When I try to run this program in CLion, I get a large list of undefined reference errors as shown here: https://pastebin.com/8uX9MZFf

我已经使用vcpkg软件包管理器设置了Boost.我正在使用Mingw-w64进行编译. CMake文件加载没有错误(只有警告说新的Boost版本可能具有不正确的依赖项或缺少依赖项和导入的目标",尽管我听说此警告无关紧要,因为这仅表示当前的CMake版本不存在).尚不了解最新版的Boost.

I have setup Boost using vcpkg package manager. I'm compiling using Mingw-w64. The CMake file loads without errors (only a warning that says "New Boost version may have incorrect or missing dependencies and imported targets," though I've heard this warning isn't of concern, as it just means the current version of CMake isn't aware of the newest version of Boost).

我试图在所有地方寻找解决方案,但是似乎找不到任何可行的解决方案.任何帮助将不胜感激.

I've tried to look for solutions to this everywhere, but I can't seem to find anything that works here. Any help would be appreciated.

我正在使用cmake 3.15.3,boost 1.73.0和mingw-w64 6.0.

I'm using cmake 3.15.3, boost 1.73.0 and mingw-w64 6.0.

编辑

我在不使用程序包管理器的情况下卸载并重新安装了Boost,并尝试再次获取序列化库.在这种情况下,CMake遇到错误,指出无法通过序列化找到Boost(尽管可以单独找到Boost).我将Boost_DEBUG设置为ON并查看了输出,并注意到以下内容:

I uninstalled and reinstalled Boost without using the package manager, and tried getting the serialization library again. In this context, CMake runs into errors saying it can't find Boost with serialization (Though it can find Boost alone). I set Boost_DEBUG to ON and looked at the output, and noticed the following things:

_boost_COMPILER = "-mgw81" (guessed) CMake似乎猜测我用来编译boost的编译器是mgw81.我猜它是从我的gcc版本中获得的8.1,这是正确的.

_boost_COMPILER = "-mgw81" (guessed) CMake seems to guess that the compiler I used to compile boost was mgw81. I'm guessing it got the 8.1 from my gcc version, which is correct.

Searching for SERIALIZATION_LIBRARY_RELEASE: boost_serialization-mgw81-mt-x64-1_73;boost_serialization-mgw81-mt-x64;...

选择该编译器后,它将搜索名称中带有"-mgw81"的文件.问题是我构建boost时生成的库文件的命名如下:

As a result of that compiler selection, it searches for a file with "-mgw81" in the name. The problem is that the library files generated when I built boost are named like so:

libboost_serialization-mgw8-mt-x64-1_73.a

这说"-mgw8"而不是"-mgw81".我不知道如何以不会发生这种冲突的方式纠正CMake或构建增强.我尝试用toolset=gcc-8.1而不是toolset=gcc重建boost,但是在库文件名中仍然出现"-mgw8".

This says "-mgw8" instead of "-mgw81". I don't know how to correct CMake or build boost in such a way that this conflict doesn't happen. I've tried rebuilding boost with toolset=gcc-8.1 instead of toolset=gcc, but I still get "-mgw8" in the library file names.

编辑2

我找到了解决上述问题的方法.我已将其张贴在下面.

I found the solution to the above issue. I've posted it below.

推荐答案

意识到这一问题就是我在 EDIT 中提到的问题之后,我进一步研究了如何解决该问题,然后发现您可以手动设置用于搜索变量Boost_COMPILER的编译器.

After realizing that the issue was what I mentioned in EDIT, I looked further into how that issue could be resolved, and I found out you can manually set the compiler that is used to search through the variable Boost_COMPILER.

我将CMake文件更改为以下内容:

I changed my CMake file to the following:

cmake_minimum_required(VERSION 3.15)
project(Test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/boost_1_73_0")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/boost_1_73_0/libs")
set(Boost_DEBUG ON)
set(Boost_COMPILER -mgw8)
set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT C:/boost)
set(BOOST_INCLUDEDIR C:/boost/include/boost-1_73/boost)
set(BOOST_LIBRARYDIR C:/boost/lib)
set(BOOST_NO_SYSTEM_PATHS ON)
find_package(Boost REQUIRED serialization)
add_executable(Test main.cpp)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(Test ${Boost_LIBRARIES})
endif()

我相信这里的关键更改是设置Boost_COMPILERBoost_ARCHITECTURE.我意识到需要通过以下问题设置Boost_ARCHITECTURE:在CLion项目中链接boost .

I believe the critical changes here were setting Boost_COMPILER and Boost_ARCHITECTURE. I realized Boost_ARCHITECTURE needed to be set from this question: Linking boost in CLion project.

有了这个CMake文件,我上面的main.cpp文件正常运行了.

With this CMake file, my main.cpp file from above ran properly.

这篇关于简单Boost序列化中的未定义参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆