【问题标题】:How to build and configure boost.thread in CodeBlocks on Windows如何在 Windows 上的 CodeBlocks 中构建和配置 boost.thread
【发布时间】:2012-09-16 16:17:12
【问题描述】:

我只想问这个非常琐碎的问题,我不知道这是否是正确的问题,或者以前是否有人问过这个问题,我知道这可以通过访问关于 boost 的文档来解决。但是我真的很迷茫,并且卡在C::B 中配置boost.thread

在这方面我只是一个初学者,目前正在学习如何制作 Windows 应用程序,并不认真,只是为了学习目的。我只是注意到我真的需要多线程的概念才能让它工作。所以我决定使用 Boost 库,我完全按照 boost wiki 在构建库时所说的那样做,我认为我没有做错什么。

我从文档中运行了一些代码,这些代码仅是标题,它可以完美运行,但是 boosts 中包含的库需要某种特殊处理,包括 boost.thread,我遇到了困难。我知道#includ-ing 在我的文件中不会使Boost.thread 起作用。我从online tutorial(我在那里找到的多线程页面上的第一个代码)中找到的这个基本代码出错了,它会产生一个错误,指出没有这样的文件目录。

||=== Multithreading_sample, Debug ===|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev          
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|1|boost/thread.hpp: No such 
file or directory|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev    
C++\App\Multithreading_sample\Multithreading_sample\main.cpp||In function `void   
wait(int)':|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev  
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|6|error: `boost' has not   
been declared|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev 
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|6|error: `boost' has not 
been declared|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev    
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|6|error: `seconds' cannot 
be used as a function|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev   
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|6|error: `sleep' undeclared 
(first use this function)|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev 
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|6|error: (Each undeclared 
identifier is reported only once for each function it appears in.)|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev   
C++\App\Multithreading_sample\Multithreading_sample\main.cpp||In function `int main()':|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev  
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|20|error: `boost' has not 
been declared|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev   
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|20|error: expected `;' 
before "t"|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev   
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|20|warning: statement is a   
reference, not call, to function `thread'|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev   
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|20|warning: statement has 
no effect|
C:\Documents and Settings\Owner\Desktop\Programming\C++\Dev   
C++\App\Multithreading_sample\Multithreading_sample\main.cpp|21|error: `t' undeclared 
(first use this function)|
||=== Build finished: 9 errors, 2 warnings ===|

我知道它没有检测到我的boost.thread 库,我不知道在这部分中去哪里。我已经搜索了谷歌,但我认为我最好的选择是单独构建 1boost.thread1,如 here 所示,我不知道下一步该做什么。

  • 我的 boost 库版本是 1.51.0,我的 C::B 是 10.05,在 windows XP 上运行,我想在我的 CodeBlocks 中使用 boost.thread。我只想粘贴代码并运行它,看看它是如何工作的。
  • 我的 MinGW 版本是 3.4.2

【问题讨论】:

  • multithreading section 中的第二个代码示例适用于 C::B、GCC 4.7.1 和 Boost 1.49.0。
  • 你有没有在你的 CodeBlocks 中配置 boost.thread 之类的东西,你是如何让它工作的?第一个代码对我不起作用。我还没有尝试过第二个代码。我想我会寻找预编译版本的 boost 线程,看看我是否可以从那里解决问题。感谢您的回复。
  • 我从 nuwen.net/mingw.html 获得了 MinGW,其中包括 Boost。它编译良好,无需对标头内部进行任何配置更改,但链接需要-lboost_thread
  • 好吧,也许我会把我的 MinGW 更新到最新版本,看看问题是否会自行解决。
  • 最新版本的 boost 是不是有点不稳定?如果是这种情况,我看到其他使用 boost 的人对旧版本没有问题。

标签: c++ boost mingw codeblocks boost-thread


【解决方案1】:

最后,经过几天的研究、谷歌搜索和交叉引用网络上的大量解决方案,我终于在我提供的网站上编写了第一个代码。

首先,我将 extract_directory 作为基础包含在全局变量的内置字段中。然后我转到项目选项,单击层次结构和搜索目录中的项目名称,在编译器选项卡上添加了 $(#boost.include),在链接器选项卡上添加了 $(boost)\stage\lib。

我重新从头开始重建 boost(尤其是构建 boost.thread 库),然后我再次按照文档上的确切内容进行操作,然后我终于收到了一些错误,说 Boost::system 上的未定义引用错误。我为解决这个问题所做的是将我的项目与那个 boost.system 链接起来(在我的例子中,它是我的 stage\lib 文件夹中的文件)。

我将这个库链接到我的项目中 libboost_system-mgw34-mt-1_51.a

当我编译我的程序时,出现一个错误,提示未定义对 boost::chrono 的引用,库名称是:

libboost_chrono-mgw34-mt-1_51.a

我还将我的项目链接到它,就像我为 boost::system 所做的那样。

然后编译!没有任何警告,我从这次经历中学到的是我必须寻找所需的库,到目前为止我所做的只是链接 boost.thread 库和错误只是继续说未定义的引用错误到 boost.system我不知道这与提升线程或其他库有关(这是我在搞砸之前遇到的第一个错误)。我认为每个 boost 库中有多个函数调用位于另一个库中,所以我必须与它们链接,这也需要这样的调用。

感谢各位热心的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多