Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编译C++;使用Boost.Thread的源文件 我正在尝试学习如何使用C++ Booost线程库。我已经在我的Ubuntu 11.10系统上安装了Boost库。我是通过Salal-特别是第66页的示例6.1来跟随这本书“Boost C++库”。我正在尝试编译以下代码示例: #include <boost/thread.hpp> #include <iostream> void wait(int seconds) { boost::this_thread::sleep(boost::posix_time::seconds(seconds)); } void thread() { for(int i = 0; i < 5; ++i) { wait(1); std::cout << i << std::endl; } } int main() { boost::thread t(thread); t.join(); }_C++_Boost_Boost Thread - Fatal编程技术网

编译C++;使用Boost.Thread的源文件 我正在尝试学习如何使用C++ Booost线程库。我已经在我的Ubuntu 11.10系统上安装了Boost库。我是通过Salal-特别是第66页的示例6.1来跟随这本书“Boost C++库”。我正在尝试编译以下代码示例: #include <boost/thread.hpp> #include <iostream> void wait(int seconds) { boost::this_thread::sleep(boost::posix_time::seconds(seconds)); } void thread() { for(int i = 0; i < 5; ++i) { wait(1); std::cout << i << std::endl; } } int main() { boost::thread t(thread); t.join(); }

编译C++;使用Boost.Thread的源文件 我正在尝试学习如何使用C++ Booost线程库。我已经在我的Ubuntu 11.10系统上安装了Boost库。我是通过Salal-特别是第66页的示例6.1来跟随这本书“Boost C++库”。我正在尝试编译以下代码示例: #include <boost/thread.hpp> #include <iostream> void wait(int seconds) { boost::this_thread::sleep(boost::posix_time::seconds(seconds)); } void thread() { for(int i = 0; i < 5; ++i) { wait(1); std::cout << i << std::endl; } } int main() { boost::thread t(thread); t.join(); },c++,boost,boost-thread,C++,Boost,Boost Thread,我得到以下输出: /tmp/cc6bVu1F.o:在函数“main”中: 示例6.cpp:(.text+0x9d):对“boost::thread::join()”的未定义引用 示例6.cpp:(.text+0xae):对“boost::thread::~thread()”的未定义引用 示例6.cpp:(.text+0xc6):对“boost::thread::~thread()”的未定义引用 /tmp/cc6bVu1F.o:在函数“boost::detail::thread_data_base

我得到以下输出:

/tmp/cc6bVu1F.o:在函数“main”中:
示例6.cpp:(.text+0x9d):对“boost::thread::join()”的未定义引用
示例6.cpp:(.text+0xae):对“boost::thread::~thread()”的未定义引用
示例6.cpp:(.text+0xc6):对“boost::thread::~thread()”的未定义引用
/tmp/cc6bVu1F.o:在函数“boost::detail::thread_data_base::thread_data_base()”中:
示例6.cpp:(.text.ZN5boost6detail16thread_data_baseC2Ev[ZN5boost6detail16thread_data_baseC5Ev]+0x24):未定义对“vtable for boost::detail::thread_data_base”的引用
/tmp/cc6bVu1F.o:在函数“void boost::this_thread::sleep(boost::posix_time::seconds const&)”中:
示例6.cpp:(.text.\u zn5boost11此线程5sleepins\u 10posix\u time7secondseevrkt\u[void boost::this\u thread::sleep(boost::posix\u times::seconds const&)]+0x35:boost::this\u thread::sleep(boost::posix\u times::ptime::ptime const&)的未定义引用
/tmp/cc6bVu1F.o:在函数“boost::thread::thread(void(*)(),boost::disable_if::type)”中:
示例6.cpp:(.text.ZN5boost6threadC2IPFvvEEET\u NS\u 10disable\u 14is\u convertibleIRS4\u NS\u 6detail13 thread\u move\u tIS4\u eepens0\u 5dummyEE4typeE[zn5boost6;threadc5ipfvveeet\u 10disable\u 14is\u 14is\u 6detailtIRS4\u 6detailt13 thread\u-move\u eepens0\u 5dummyee4e]+0x30):未定义线程的参考
/tmp/cc6bVu1F.o:在函数“boost::detail::thread_data::~thread_data()”中:
示例6.cpp:(.text.ZN5boost6detail11thread_dataIPFvvEED2Ev[ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f):对“boost::detail::thread_data_base::~thread_data_base()”的未定义引用
/tmp/cc6bVu1F.o:(.rodata.\u ZTIN5boost6detail11thread\u dataIPFvvEEE[boost::detail::thread\u data的类型信息]+0x10):未定义对“boost::detail::thread\u数据库的类型信息”的引用
collect2:ld返回了1个退出状态

我不知道怎么解释这个。有人能帮忙吗?非常感谢你

这是一个链接错误。这意味着您的代码是正确的,并且包含了正确的头,但是编译器没有链接到boost线程库。要解决此问题,您需要如下编译:

g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread
如果已将Boost threading library安装到非标准路径,则还必须将其添加到搜索路径:

g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread -L/usr/local/lib

Boost线程不是一个只支持模板的库。您需要在链接/编译时添加-lboost_线程


boost中的大多数库都是在头文件中实现的。它们可以像您所做的那样简单地包括在内。另一方面,Boost线程的性质是,您需要依赖于它的编译单元,只有它的函数声明在头中对您可用。因此,负责将调用链接到已声明函数/类的编译器(更准确地说是链接器)需要知道在何处查找这些符号。通过使用-lboost_线程调用编译器,可以告诉它链接到库(-l)boost线程

您需要与库链接。一些Boost库完全在头文件中实现,不需要库。但其他的,比如线程,部分是在头文件中实现的,部分是在编译的库代码中实现的


我认为您需要将
-lboost\u thread-mt
添加到compile命令中。

以下是我与您分享的pocketcpp编译工具的编译字符串:

g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"

祝你好运,

我在上面发表了评论,但我想在这里分享完整的命令行

g++-std=c++11线程\u示例.cpp-lboost\u线程-lboost\u系统


[我正在使用thread_example.cpp作为源文件名;请替换为您自己的文件名]

您必须链接到boost thread library@Violet Giraffe,您能帮我怎么做吗?谢谢您是否自己构建了Boost库?这是很难做到的。如果我是你,我会卸载你从/usr/local/include和/usr/local/lib安装的所有东西,并且
apt-get-install-libboost-thread-dev
@Zan-Lynx,我最初是这么做的。但是Ubuntu软件包是1.46版的,而我下面的书是1.47版的。我本来想有同样的版本。我是否应该使用更简单的安装方法,并在以后担心差异?谢谢你的建议@TJB:您的一些问题是由于安装到一个不寻常的目录引起的。但是如果您想保留它,接下来需要做的就是将/usr/local/lib添加到您的共享库查找目录中。@Antii,这几乎可以工作了。不管怎样,它都可以编译。但是当我运行它时,我得到以下信息:./example6:加载共享库时出错:libboost_thread.so.1.47.0:无法打开共享对象文件:没有这样的文件或目录您需要传递到库的路径,以便在运行时可以找到它:LD_library_path=/usr/local/lib./example6感谢帮助。成功了。这是Boost的非标准路径吗?文件建议使用/usr/local。我应该把它放在Ubuntu的哪里?非常感谢/usr/local是存储您自己安装的内容的常用路径,但它通常不在编译器的标准搜索路径中。将boost放在那里很好,但通常您会从Ubuntu存储库安装boost,在这种情况下,它会安装在/usr中(并且会由编译器自动找到)。谢谢您的帮助。我真的很感激。
g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"