gpt4 book ai didi

c++ - 定义 NDEBUG 时 boost 序列化链接器错误

转载 作者:行者123 更新时间:2023-11-28 06:56:12 26 4
gpt4 key购买 nike

当与 boost::serialization 链接时,我在项目的“发布”版本中遇到问题。我的“调试”和“发布”构建之间唯一真正的区别是我定义了 DEBUG 并在调试中使用了 -O0 并且我定义了 NDEBUG 和在发布中使用 -O3

我可以在 Debug模式下编译、链接和运行。

但是,当我使用 Release模式编译时,它可以正常编译但在链接期间失败并出现以下错误:

/home/jarrett/projects/icebreakers/glr/build/libglr.a(ModelManager.o): In function `serialize<boost::archive::text_iarchive, glr::models::Model>':
/home/jarrett/projects/icebreakers/glr/deps/boost/include/boost/serialization/access.hpp:118: undefined reference to `void glr::models::Model::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive&, unsigned int)'
/home/jarrett/projects/icebreakers/glr/build/libglr.a(ModelManager.o): In function `serialize<boost::archive::text_oarchive, glr::models::Model>':
/home/jarrett/projects/icebreakers/glr/deps/boost/include/boost/serialization/access.hpp:118: undefined reference to `void glr::models::Model::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsigned int)'
/home/jarrett/projects/icebreakers/glr/build/libglr.a(TextureManager.o): In function `serialize<boost::archive::text_iarchive, glr::glw::Texture2D>':
/home/jarrett/projects/icebreakers/glr/deps/boost/include/boost/serialization/access.hpp:118: undefined reference to `void glr::glw::Texture2D::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive&, unsigned int)'
/home/jarrett/projects/icebreakers/glr/build/libglr.a(TextureManager.o): In function `serialize<boost::archive::text_oarchive, glr::glw::Texture2D>':
/home/jarrett/projects/icebreakers/glr/deps/boost/include/boost/serialization/access.hpp:118: undefined reference to `void glr::glw::Texture2D::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsigned int)'
/home/jarrett/projects/icebreakers/glr/build/libglr.a(AnimationManager.o): In function `serialize<boost::archive::text_iarchive, glr::glw::Animation>':
/home/jarrett/projects/icebreakers/glr/deps/boost/include/boost/serialization/access.hpp:118: undefined reference to `void glr::glw::Animation::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive&, unsigned int)'
/home/jarrett/projects/icebreakers/glr/build/libglr.a(AnimationManager.o): In function `serialize<boost::archive::text_oarchive, glr::glw::Animation>':
/home/jarrett/projects/icebreakers/glr/deps/boost/include/boost/serialization/access.hpp:118: undefined reference to `void glr::glw::Animation::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsigned int)'
collect2: error: ld returned 1 exit status
scons: *** [build/darkhorizon] Error 1
scons: building terminated because of errors.

我不明白这一点,因为所讨论的方法已完全定义。我没有在这些类的代码中执行任何类型的 #ifdef NDEBUG 等宏。

有谁知道为什么会发生这种情况?

我的 Texture2D 类:
Texture2D.hpp
Texture2D.cpp

我的 TextureManager 类:
TextureManager.hpp
TextureManager.cpp

如果有帮助,我很乐意提供更直接/本地化的代码片段。

更新:如果我将serialize 方法的定义移动到头文件中,它会突然链接!我想这与它们是模板方法有关,但我仍然不明白为什么我的“调试”构建有效而我的“发布”构建却没有...

最佳答案

确实是因为这个。显然在 DEBUG 构建中,模板实例化获得外部链接(这在启用优化的情况下不常见,因为很可能可以内联(顶层)调用。

另见

关于c++ - 定义 NDEBUG 时 boost 序列化链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23145851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com