【问题标题】:Linking to boost through SConstruct通过 SConstruct 链接到 boost
【发布时间】:2011-05-28 02:56:42
【问题描述】:

我正在尝试使用 boost 进行文件/目录操作的跨平台项目。我一直在使用visual studio,但是为了在linux上编译,我决定切换到SConstruct。

但是,我无法 [正确?] 链接到文件系统库。

我的 SConstruct 文件如下:

vLibs = [   
            'libboost_system-vc100-mt-1_44.lib',
            'libboost_filesystem-vc100-mt-1_44.lib'];
# LIBS=vLibs,
env = Environment();

env.AppendUnique(CXXFLAGS=Split("/EHsc"));
env.Append(CPPPATH = ["C:\\Program Files (x86)\\boost\\boost_1_44"]);
env.Append(LIBPATH = ["C:\\Program Files (x86)\\boost\\boost_1_44\\lib"]);

env.Program( Glob('test.cpp'),LIBS=vLibs)

我不断收到类似的错误

test.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ ) 在函数“void __cdecl boost::system::`dynamic initializer for 'posix_category''(void)”中引用 (??__Eposix_category@system@boost@@YAXXZ)

如果我使用 Visual Studio,我会收到类似的错误:

错误 2 错误 LNK2001: 无法解析的外部符号 "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAABVerror_category@12@XZ) T :\VS\temp\test.obj 温度

直到我将库添加到项目中,之后错误消失并且编译正常(在 Visual Studio 中不是 SConstruct)。

我已经尝试了库的每种组合(共享/非共享/运行时共享/运行时非共享/调试),但我总是遇到相同的错误。

我在过去 6 个多小时内一直在寻找答案,所以我很感激任何帮助。

注意:我没有使用自动链接(#define BOOST_ALL_NO_LIB),因为 gcc 不支持它)

这是 Scons 的输出:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...


link /nologo /OUT:build\test.exe "/LIBPATH:C:\Program Files (x86)\boost\boost_1_44\lib" libboost_system-vc100-mt-1_44.lib     libboost_filesystem-vc100-mt-1_44.lib build\test.obj
test.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl     boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'posix_category''(void)" (??    __Eposix_category@system@boost@@YAXXZ)
test.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl     boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??    __Enative_ecat@system@boost@@YAXXZ)
build\test.exe : fatal error LNK1120: 2 unresolved externals
scons: *** [build\test.exe] Error 1120
scons: building terminated because of errors.

【问题讨论】:

  • 请贴出 scons 生成的链接命令行。通常LIBS 是一个库名列表(例如boost_system-vc100-mt-1_44),而不是完整的文件名。
  • 是的,lib 扩展不应该在那里,但无论哪种方式都没有区别 - 如果没有扩展,它会给出相同的错误。在这里,我逐字复制了名称,以确保它在 Visual Studio 中有效,但在 scons 中无效。
  • 另一个猜测:上一个问题 (stackoverflow.com/questions/1066071/…) 暗示它可能是架构问题:32 位与 64 位库。
  • 我没有看到这个问题(希望我有 - 可以节省我 6 多个小时)。我曾假设 scons 默认使用 32 位(如 Visual Studio),但事实并非如此。 “env = 环境(TARGET_ARCH='x86');”解决了这个问题。我真的很想给你答案戴夫,你救了我无数小时的痛苦!

标签: c++ visual-studio boost linker scons


【解决方案1】:

在使用 bjam 的 address-model=64 参数重建 boost 之前,我得到了完全相同的错误

【讨论】:

  • 将此标记为答案,但如果您想构建 32 位库,请参阅下面的我的。
【解决方案2】:

默认情况下 Boost 为 x86 构建,而 SConstruct 默认为 x64 构建。要将 SConstruct 更改为使用 x86,请使用 env = Environment(TARGET_ARCH='x86'); 或将 Boost 更改为 x64,请将 address-model=64 参数传递给 bjam。

【讨论】:

    【解决方案3】:

    我不认为应该存在 lib 前缀或后缀。在你的 SCons LIBS 中像这样尝试这些:

    boost_filesystem-vc100-mt-1_44
    

    【讨论】:

    • 不(尝试了不同库的各种组合)。我知道它找到了库(因为否则它会抱怨它找不到它们)。问题是链接似乎没有做任何事情。我不知道是否有特殊标志或我需要通过什么,但似乎没有任何作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2013-06-13
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多