线程示例的静态编译

发布于 2024-08-18 09:25:02 字数 5490 浏览 6 评论 0原文

我编译了 Boost C++ 库,如下所示:

bjam install variant=release link=static threading=multi runtime-link=static

没有错误。然后我编译了以下源代码:

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

#define BOOST_THREAD_NO_LIB

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

int main(int argc, char* argv[]) {
  boost::thread thrd(&hello);
  thrd.join();
  return 0;
}

使用:

g++ -lboost_thread Thread.cpp

程序编译并运行良好,因为 Boost 库位于 /usr/local/lib 中。当我从 /etc/ld.so.conf.d/libc.conf 中删除 /usr/local/lib 时,我遇到了以下错误(毫不奇怪)

./a.out: error while loading shared libraries: libboost_thread.so.1.41.0: cannot
open shared object file: No such file or directory

:我要编译静态链接到Boost的Thread.cpp吗?我尝试了以下操作:

g++ -static -lboost_thread -lpthread Thread.cpp

我安装了以下 Boost 库:

 ldconfig -v | grep boost
 libboost_unit_test_framework.so.1.41.0 -> libboost_unit_test_framework.so.1.41.0
 libboost_math_tr1f.so.1.41.0 -> libboost_math_tr1f.so.1.41.0
 libboost_prg_exec_monitor.so.1.41.0 -> libboost_prg_exec_monitor.so.1.41.0
 libboost_math_c99.so.1.41.0 -> libboost_math_c99.so.1.41.0
 libboost_regex.so.1.41.0 -> libboost_regex.so.1.41.0
 libboost_signals.so.1.41.0 -> libboost_signals.so.1.41.0
 libboost_python.so.1.41.0 -> libboost_python.so.1.41.0
 libboost_serialization.so.1.41.0 -> libboost_serialization.so.1.41.0
 libboost_graph.so.1.41.0 -> libboost_graph.so.1.41.0
 libboost_date_time.so.1.41.0 -> libboost_date_time.so.1.41.0
 libboost_thread.so.1.41.0 -> libboost_thread.so.1.41.0
 libboost_math_c99l.so.1.41.0 -> libboost_math_c99l.so.1.41.0
 libboost_math_tr1l.so.1.41.0 -> libboost_math_tr1l.so.1.41.0
 libboost_wserialization.so.1.41.0 -> libboost_wserialization.so.1.41.0
 libboost_system.so.1.41.0 -> libboost_system.so.1.41.0
 libboost_math_tr1.so.1.41.0 -> libboost_math_tr1.so.1.41.0
 libboost_math_c99f.so.1.41.0 -> libboost_math_c99f.so.1.41.0
 libboost_wave.so.1.41.0 -> libboost_wave.so.1.41.0
 libboost_filesystem.so.1.41.0 -> libboost_filesystem.so.1.41.0
 libboost_program_options.so.1.41.0 -> libboost_program_options.so.1.41.0
 libboost_program_options.so.1.35.0 -> libboost_program_options.so.1.35.0
 libboost_program_options-mt.so.1.35.0 -> libboost_program_options-mt.so.1.35.0
 libboost_thread-gcc42-mt-1_34_1.so.1.34.1 -> libboost_thread-gcc42-mt-1_34_1.so.1.34.1

我收到以下编译错误:

/tmp/ccek8Br2.o: In function `main':
Thread.cpp:(.text+0x10b): undefined reference to `boost::thread::join()'
Thread.cpp:(.text+0x119): undefined reference to `boost::thread::~thread()'
Thread.cpp:(.text+0x140): undefined reference to `boost::thread::~thread()'
/tmp/ccek8Br2.o: In function `boost::mutex::mutex()':
Thread.cpp:(.text._ZN5boost5mutexC1Ev[boost::mutex::mutex()]+0x22): undefined reference to `pthread_mutex_init'
/tmp/ccek8Br2.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
Thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED0Ev[boost::detail::thread_data<void (*)()>::~thread_data()]+0x1c): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccek8Br2.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
Thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED1Ev[boost::detail::thread_data<void (*)()>::~thread_data()]+0x1c): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccek8Br2.o: In function `boost::condition_variable::condition_variable()':
Thread.cpp:(.text._ZN5boost18condition_variableC1Ev[boost::condition_variable::condition_variable()]+0x17): undefined reference to `pthread_cond_init'
/tmp/ccek8Br2.o: In function `boost::condition_variable::~condition_variable()':
Thread.cpp:(.text._ZN5boost18condition_variableD1Ev[boost::condition_variable::~condition_variable()]+0x11): undefined reference to `pthread_cond_destroy'
/tmp/ccek8Br2.o: In function `boost::mutex::~mutex()':
Thread.cpp:(.text._ZN5boost5mutexD1Ev[boost::mutex::~mutex()]+0x11): undefined reference to `pthread_mutex_destroy'
/tmp/ccek8Br2.o: In function `boost::detail::thread_data_base::thread_data_base()':
Thread.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[boost::detail::thread_data_base::thread_data_base()]+0x23): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccek8Br2.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':
Thread.cpp:(.text._ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x36): undefined reference to `boost::thread::start_thread()'
/tmp/ccek8Br2.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status

有什么想法吗?

I compiled the Boost C++ libraries as follows:

bjam install variant=release link=static threading=multi runtime-link=static

No errors. Then I compiled the following source:

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

#define BOOST_THREAD_NO_LIB

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

int main(int argc, char* argv[]) {
  boost::thread thrd(&hello);
  thrd.join();
  return 0;
}

Using:

g++ -lboost_thread Thread.cpp

The program compiled and ran fine because the Boost libraries are found in /usr/local/lib. When I removed /usr/local/lib from /etc/ld.so.conf.d/libc.conf I ran into the following error (no surpise):

./a.out: error while loading shared libraries: libboost_thread.so.1.41.0: cannot
open shared object file: No such file or directory

How do I compile Thread.cpp linking to Boost statically? I tried the following:

g++ -static -lboost_thread -lpthread Thread.cpp

I have the following Boost libraries installed:

 ldconfig -v | grep boost
 libboost_unit_test_framework.so.1.41.0 -> libboost_unit_test_framework.so.1.41.0
 libboost_math_tr1f.so.1.41.0 -> libboost_math_tr1f.so.1.41.0
 libboost_prg_exec_monitor.so.1.41.0 -> libboost_prg_exec_monitor.so.1.41.0
 libboost_math_c99.so.1.41.0 -> libboost_math_c99.so.1.41.0
 libboost_regex.so.1.41.0 -> libboost_regex.so.1.41.0
 libboost_signals.so.1.41.0 -> libboost_signals.so.1.41.0
 libboost_python.so.1.41.0 -> libboost_python.so.1.41.0
 libboost_serialization.so.1.41.0 -> libboost_serialization.so.1.41.0
 libboost_graph.so.1.41.0 -> libboost_graph.so.1.41.0
 libboost_date_time.so.1.41.0 -> libboost_date_time.so.1.41.0
 libboost_thread.so.1.41.0 -> libboost_thread.so.1.41.0
 libboost_math_c99l.so.1.41.0 -> libboost_math_c99l.so.1.41.0
 libboost_math_tr1l.so.1.41.0 -> libboost_math_tr1l.so.1.41.0
 libboost_wserialization.so.1.41.0 -> libboost_wserialization.so.1.41.0
 libboost_system.so.1.41.0 -> libboost_system.so.1.41.0
 libboost_math_tr1.so.1.41.0 -> libboost_math_tr1.so.1.41.0
 libboost_math_c99f.so.1.41.0 -> libboost_math_c99f.so.1.41.0
 libboost_wave.so.1.41.0 -> libboost_wave.so.1.41.0
 libboost_filesystem.so.1.41.0 -> libboost_filesystem.so.1.41.0
 libboost_program_options.so.1.41.0 -> libboost_program_options.so.1.41.0
 libboost_program_options.so.1.35.0 -> libboost_program_options.so.1.35.0
 libboost_program_options-mt.so.1.35.0 -> libboost_program_options-mt.so.1.35.0
 libboost_thread-gcc42-mt-1_34_1.so.1.34.1 -> libboost_thread-gcc42-mt-1_34_1.so.1.34.1

I receive the following compile errors:

/tmp/ccek8Br2.o: In function `main':
Thread.cpp:(.text+0x10b): undefined reference to `boost::thread::join()'
Thread.cpp:(.text+0x119): undefined reference to `boost::thread::~thread()'
Thread.cpp:(.text+0x140): undefined reference to `boost::thread::~thread()'
/tmp/ccek8Br2.o: In function `boost::mutex::mutex()':
Thread.cpp:(.text._ZN5boost5mutexC1Ev[boost::mutex::mutex()]+0x22): undefined reference to `pthread_mutex_init'
/tmp/ccek8Br2.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
Thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED0Ev[boost::detail::thread_data<void (*)()>::~thread_data()]+0x1c): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccek8Br2.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
Thread.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED1Ev[boost::detail::thread_data<void (*)()>::~thread_data()]+0x1c): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccek8Br2.o: In function `boost::condition_variable::condition_variable()':
Thread.cpp:(.text._ZN5boost18condition_variableC1Ev[boost::condition_variable::condition_variable()]+0x17): undefined reference to `pthread_cond_init'
/tmp/ccek8Br2.o: In function `boost::condition_variable::~condition_variable()':
Thread.cpp:(.text._ZN5boost18condition_variableD1Ev[boost::condition_variable::~condition_variable()]+0x11): undefined reference to `pthread_cond_destroy'
/tmp/ccek8Br2.o: In function `boost::mutex::~mutex()':
Thread.cpp:(.text._ZN5boost5mutexD1Ev[boost::mutex::~mutex()]+0x11): undefined reference to `pthread_mutex_destroy'
/tmp/ccek8Br2.o: In function `boost::detail::thread_data_base::thread_data_base()':
Thread.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[boost::detail::thread_data_base::thread_data_base()]+0x23): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccek8Br2.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':
Thread.cpp:(.text._ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x36): undefined reference to `boost::thread::start_thread()'
/tmp/ccek8Br2.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status

Any ideas?

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

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

发布评论

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

评论(1

日裸衫吸 2024-08-25 09:25:02

尝试:

g++ -static -pthread Thread.cpp -lboost_thread

必须在使用这些库中的符号的对象(或源)之后指定库。

Try:

g++ -static -pthread Thread.cpp -lboost_thread

Libraries must be specified after the objects (or sources) that use symbols from those libraries.

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