gpt4 book ai didi

c++ - 使用 boost 库时出错

转载 作者:行者123 更新时间:2023-11-28 04:54:49 24 4
gpt4 key购买 nike

我正在尝试使用 Boost 库,但无法编译任何代码。

我在 Linux mint 中使用以下命令安装 boost

$ sudo ./bootstrap.sh --prefix=/usr/local
$ sudo ./b2 install

这是我的程序,我从这个网站复制粘贴https://en.wikibooks.org/wiki/C%2B%2B_Programming/Libraries/Boost

#include <boost/thread/thread.hpp>
#include <iostream>

using namespace std;

void hello_world()
{
cout << "Hello world, I'm a thread!" << endl;
}

int main(int argc, char* argv[])
{
// start two new threads that calls the "hello_world" function
boost::thread my_thread1(&hello_world);
boost::thread my_thread2(&hello_world);

// wait for both threads to finish
my_thread1.join();
my_thread2.join();

return 0;
}

当你试图编译它时,会发生这个错误

$ g++ -Wall -Wextra -g cppc.cpp -o test
cppc.cpp:32:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
int main(int argc , char * argv[]){
^
cppc.cpp:32:33: warning: unused parameter ‘argv’ [-Wunused-parameter]
int main(int argc , char * argv[]){
^
/tmp/ccOvZcci.o: En la función `__static_initialization_and_destruction_0(int, int)':
/usr/local/include/boost/system/error_code.hpp:206: referencia a `boost::system::generic_category()' sin definir
/usr/local/include/boost/system/error_code.hpp:208: referencia a `boost::system::generic_category()' sin definir
/usr/local/include/boost/system/error_code.hpp:210: referencia a `boost::system::system_category()' sin definir
/tmp/ccOvZcci.o: En la función `boost::thread_exception::thread_exception(int, char const*)':
/usr/local/include/boost/thread/exceptions.hpp:51: referencia a `boost::system::generic_category()' sin definir
/tmp/ccOvZcci.o: En la función `boost::detail::thread_data_base::thread_data_base()':
/usr/local/include/boost/thread/pthread/thread_data.hpp:152: referencia a `vtable for boost::detail::thread_data_base' sin definir
/tmp/ccOvZcci.o: En la función `boost::thread::start_thread()':
/usr/local/include/boost/thread/detail/thread.hpp:186: referencia a `boost::thread::start_thread_noexcept()' sin definir
/tmp/ccOvZcci.o: En la función `boost::thread::~thread()':
/usr/local/include/boost/thread/detail/thread.hpp:261: referencia a `boost::thread::detach()' sin definir
/tmp/ccOvZcci.o: En la función `boost::thread::get_id() const':
/usr/local/include/boost/thread/detail/thread.hpp:751: referencia a `boost::thread::native_handle()' sin definir
/tmp/ccOvZcci.o: En la función `boost::thread::join()':
/usr/local/include/boost/thread/detail/thread.hpp:777: referencia a `boost::thread::join_noexcept()' sin definir
/tmp/ccOvZcci.o: En la función `boost::detail::thread_data<void (*)()>::~thread_data()':
/usr/local/include/boost/thread/detail/thread.hpp:90: referencia a `boost::detail::thread_data_base::~thread_data_base()' sin definir
/tmp/ccOvZcci.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10): referencia a `typeinfo for boost::detail::thread_data_base' sin definir
collect2: error: ld returned 1 exit stat

我能做什么?

最佳答案

这是链接器错误,不是编译器错误。您必须链接到 Boost 库。

g++ -Wall -Wextra -g cppc.cpp -o test -lboost_thread -lboost_system

有关此内容的更多信息,请参阅 documentation 的“使用和构建库”部分:

Boost.Thread depends on some non header-only libraries.

  • Boost.System: This dependency is mandatory and you will need to link with the library.
  • Boost.Chrono: This dependency is optional (see below how to configure) and you will need to link with the library if you use some of the time related interfaces.
  • Boost.DateTime: This dependency is mandatory, but even if Boost.DateTime is a non header-only library Boost.Thread uses only parts that are header-only, so in principle you should not need to link with the library.

也许你想使用 std::thread (C++11) 哪个不需要链接第 3 方库?

关于c++ - 使用 boost 库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47404594/

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