boost::thread 构建错误(无法链接 lib && 未解析的外部)

发布于 2024-09-07 18:43:44 字数 1180 浏览 6 评论 0原文

我正在尝试遵循 VS 2008 中的 Boost::Thread (ver 1.4-3) 的简单教程:

#include <boost/thread/thread.hpp>

void Func()
{
    // Do something
}

void main()
{
    boost::thread _thrd(&Func);
    _thrd.join();
    ....
}

在编译过程中它会产生此错误:

Error 1 fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-gd-1_43.lib' CConsole

我必须通过添加 #define BOOST_ALL_NO_LIB 来解决该错误。 但是,它给了我另一个错误:

Error 3 fatal error LNK1120: 2 unresolved externals 
C:\xx\Documents\Visual Studio 2008\Projects\CConsole\Debug\CConsole.exe


Error 1 error LNK2019: unresolved external symbol "public: __thiscall boost::thread::~thread(void)" (??1thread@boost@@QAE@XZ) referenced in function _wmain CConsole.obj


Error 2 error LNK2019: unresolved external symbol "private: void __thiscall boost::thread::start_thread(void)" (?start_thread@thread@boost@@AAEXXZ) referenced in function "public: __thiscall boost::thread::thread<void (__cdecl*)(void)>(void (__cdecl*)(void),struct boost::thread::dummy *)" (??$?0P6AXXZ@thread@boost@@QAE@P6AXXZPAUdummy@01@@Z) CConsole.obj

有谁知道如何解决这个问题?

谢谢。

I'm trying to follow a simple tutorial of Boost::Thread (ver 1.4-3) in VS 2008:

#include <boost/thread/thread.hpp>

void Func()
{
    // Do something
}

void main()
{
    boost::thread _thrd(&Func);
    _thrd.join();
    ....
}

During compilation it produces this error:

Error 1 fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-gd-1_43.lib' CConsole

which I have to resolve by adding #define BOOST_ALL_NO_LIB.
However, it gives me another error:

Error 3 fatal error LNK1120: 2 unresolved externals 
C:\xx\Documents\Visual Studio 2008\Projects\CConsole\Debug\CConsole.exe


Error 1 error LNK2019: unresolved external symbol "public: __thiscall boost::thread::~thread(void)" (??1thread@boost@@QAE@XZ) referenced in function _wmain CConsole.obj


Error 2 error LNK2019: unresolved external symbol "private: void __thiscall boost::thread::start_thread(void)" (?start_thread@thread@boost@@AAEXXZ) referenced in function "public: __thiscall boost::thread::thread<void (__cdecl*)(void)>(void (__cdecl*)(void),struct boost::thread::dummy *)" (??$?0P6AXXZ@thread@boost@@QAE@P6AXXZPAUdummy@01@@Z) CConsole.obj

Does anyone know how to resolve the issue?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪失控 2024-09-14 18:43:44

我认为比“阅读该死的手册”更深入的答案可能会有所帮助!

这种链接错误表明您正在尝试链接不兼容的 Boost 库。

当我以为自己正在构建 64 位库时,错误地构建了 32 位 Boost 线程库时,我得到了这个信息。我花了一段时间才发现,当您将 --address-model=64 作为 bjam 命令行参数时,您犯了一个微妙的错误。 address-model 参数不得具有 -- 前缀。不幸的是,bjam 在看到不正确的语法时不会通知您。

您可以使用 dumpbin 程序检查库提供的符号,以及链接器认为未解析的符号。我发现库符号是用 __thiscall 装饰的,而不是 __cdecl 。这是架构不匹配的一个非常好的线索。 Microsoft 编译器在 32 位构建中使用 __thiscall 函数调用协议,但在 64 位构建中使用 __cdecl 。是的,微软的文档在这里有点薄弱!

检查 .lib 或 .dll 以了解其构建方式的最佳方法是使用 dumpbin 程序。这是一个示例:

dumpbin /headers libboost_thread-vc100-mt-gd-1_45.lib | findstr machine

当然,您必须调整库名称以适应您要链接的内容。这将明确地向您显示 .lib 或 .dll 是针对 x86(32 位)还是 x64(64 位)。

I think a deeper answer than "Read the F*cking Manual" might be helpful!

This kind of link error is a clue that you're trying to link an incompatible Boost library.

I got this when I mistakenly built a 32 bit Boost thread library when I thought I was building a 64 bit library. It took a while to figure out that when you say --address-model=64 as a bjam command line parameter you have made a subtle mistake. The address-model parameter must NOT have the -- prefix. Unfortunately bjam does not inform you when it sees the incorrect syntax.

You can use the dumpbin program to check the symbols provided by your library, versus the symbols that the linker says are unresolved. I found that the library symbols were decorated with __thiscall and not __cdecl. This is a screaming good clue of the architecture mismatch. The Microsoft compiler uses the __thiscall function call protocol for 32-bit builds, but it uses __cdecl for 64-bit builds. Yes, the Microsoft documentation is a little weak here!!

The best way to check a .lib or .dll to see how it was built is to use the dumpbin program. Here's an example:

dumpbin /headers libboost_thread-vc100-mt-gd-1_45.lib | findstr machine

You'll have to adjust the library name to suit what you're linking of course. This will show you unambiguously whether the .lib or .dll is targeted for x86 (which is 32-bit) or x64 (64-bit).

前事休说 2024-09-14 18:43:44

您需要构建 Boost Thread 库并告诉 Visual Studio 该库在哪里。所有这些都记录在入门文档中(即 入门Windows)。具体阅读 第 5 节,然后第 6 节

附言。您需要确保您的构建配置与 VS 设置的配置相匹配。入门指南解释了各种构建选项。

You need to both build the Boost Thread library and tell Visual Studio where the library is. All this is documented in the Getting Started documentation (i.e. Getting Started on Windows). Specifically read section 5 and then section 6.

PS. You need to make sure your build configuration matches what you have VS set to. The Getting Started explains the various build options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文