c++ - 如何编译静态链接除一个库之外的所有库 (G++)

标签 c++ linux g++

现在我使用此命令编译我的应用程序:

g++  -o newserver  test.cpp ... -lboost_system -std=c++0x

但是我如何静态编译它(glibc 和其他),但 boost::system 应该动态链接。

谢谢!

更新

当我这样编译时:

g++  -o newserver  test.cpp ... -lboost_system -static -std=c++0x

我有一个错误:

/tmp/cc1hTPLw.o: In function `NativesResolveDns(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Natives.cpp:(.text+0x1498): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/tmp/ccHzlUIU.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
stdafx.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED2Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()]+0x15): undefined reference to `pthread_key_delete'
/tmp/ccHzlUIU.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
stdafx.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC2Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()]+0x28): undefined reference to `pthread_key_create'
/tmp/cczd8U27.o: In function `Helper::Initialize()':
Helper.cpp:(.text+0x179): undefined reference to `pthread_create'
/tmp/cczd8U27.o: In function `boost::asio::detail::posix_mutex::posix_mutex()':
Helper.cpp:(.text._ZN5boost4asio6detail11posix_mutexC1Ev[boost::asio::detail::posix_mutex::posix_mutex()]+0x28): undefined reference to `pthread_mutex_init'
/tmp/cczd8U27.o: In function `boost::asio::detail::posix_mutex::~posix_mutex()':
Helper.cpp:(.text._ZN5boost4asio6detail11posix_mutexD1Ev[boost::asio::detail::posix_mutex::~posix_mutex()]+0x14): undefined reference to `pthread_mutex_destroy'
/tmp/cczd8U27.o: In function `boost::asio::detail::posix_mutex::lock()':
Helper.cpp:(.text._ZN5boost4asio6detail11posix_mutex4lockEv[boost::asio::detail::posix_mutex::lock()]+0x17): undefined reference to `pthread_mutex_lock'
/tmp/cczd8U27.o: In function `boost::asio::detail::posix_mutex::unlock()':
Helper.cpp:(.text._ZN5boost4asio6detail11posix_mutex6unlockEv[boost::asio::detail::posix_mutex::unlock()]+0x17): undefined reference to `pthread_mutex_unlock'
/tmp/ccvfBQyf.o: In function `boost::asio::detail::posix_event::posix_event()':
NetworkServer.cpp:(.text._ZN5boost4asio6detail11posix_eventC1Ev[boost::asio::detail::posix_event::posix_event()]+0x30): undefined reference to `pthread_cond_init'
/tmp/ccvfBQyf.o: In function `boost::asio::detail::posix_event::~posix_event()':
NetworkServer.cpp:(.text._ZN5boost4asio6detail11posix_eventD1Ev[boost::asio::detail::posix_event::~posix_event()]+0x14): undefined reference to `pthread_cond_destroy'
/tmp/ccvfBQyf.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
NetworkServer.cpp:(.text._ZN5boost4asio6detail12posix_threadD1Ev[boost::asio::detail::posix_thread::~posix_thread()]+0x26): undefined reference to `pthread_detach'
/tmp/ccvfBQyf.o: In function `boost::asio::detail::posix_thread::join()':
NetworkServer.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x2b): undefined reference to `pthread_join'
/tmp/ccvfBQyf.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::operator boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context*() const':
NetworkServer.cpp:(.text._ZNK5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEcvPS9_Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::operator boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context*() const]+0x15): undefined reference to `pthread_getspecific'
/tmp/ccvfBQyf.o: In function `void boost::asio::detail::posix_event::wait<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> >(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&)':
NetworkServer.cpp:(.text._ZN5boost4asio6detail11posix_event4waitINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_[void boost::asio::detail::posix_event::wait<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> >(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&)]+0x53): undefined reference to `pthread_cond_wait'
/tmp/ccvfBQyf.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::operator=(boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context*)':
NetworkServer.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEaSEPS9_[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::operator=(boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context*)]+0x20): undefined reference to `pthread_setspecific'
/tmp/ccvfBQyf.o: In function `void boost::asio::detail::posix_event::signal<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> >(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&)':
NetworkServer.cpp:(.text._ZN5boost4asio6detail11posix_event6signalINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_[void boost::asio::detail::posix_event::signal<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> >(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&)]+0x49): undefined reference to `pthread_cond_signal'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale.o): In function `std::locale::_Impl::_M_install_cache(std::locale::facet const*, unsigned long)':
(.text._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEm+0x4e): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale.o): In function `std::locale::_Impl::_M_install_cache(std::locale::facet const*, unsigned long)':
(.text._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEm+0x83): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale.o): In function `std::locale::_Impl::_M_install_cache(std::locale::facet const*, unsigned long)':
(.text._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEm+0x1ca): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::locale()':
(.text._ZNSt6localeC1Ev+0x2b): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::locale()':
(.text._ZNSt6localeC1Ev+0x4c): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::global(std::locale const&)':
(.text._ZNSt6locale6globalERKS_+0x47): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::global(std::locale const&)':
(.text._ZNSt6locale6globalERKS_+0xa5): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::global(std::locale const&)':
(.text._ZNSt6locale6globalERKS_+0x1a4): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::locale()':
(.text._ZNSt6localeC2Ev+0x2b): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(locale_init.o): In function `std::locale::locale()':
(.text._ZNSt6localeC2Ev+0x4c): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__gnu_cxx::__scoped_lock::~__scoped_lock()':
(.text._ZN9__gnu_cxx13__scoped_lockD1Ev[__gnu_cxx::__scoped_lock::~__scoped_lock()]+0x12): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_free_dependent_exception':
(.text.__cxa_free_dependent_exception+0x48): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_free_dependent_exception':
(.text.__cxa_free_dependent_exception+0x6b): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_free_exception':
(.text.__cxa_free_exception+0x47): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_free_exception':
(.text.__cxa_free_exception+0x6a): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_exception':
(.text.__cxa_allocate_exception+0xac): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_exception':
(.text.__cxa_allocate_exception+0x11a): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_dependent_exception':
(.text.__cxa_allocate_dependent_exception+0x5c): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_dependent_exception':
(.text.__cxa_allocate_dependent_exception+0xcb): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_bases':
(.text+0x42): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_table_bases':
(.text+0xdb): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x1776): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x17c7): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__deregister_frame_info_bases':
(.text+0x1992): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__deregister_frame_info_bases':
(.text+0x1a16): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_bases':
(.text+0x61): undefined reference to `pthread_mutex_unlock'
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_table_bases':
(.text+0xfa): undefined reference to `pthread_mutex_unlock'
collect2: ld returned 1 exit status

如您所见,静态链接出现问题

最佳答案

试试这个。

g++ -o newserver -static <libname> -dynamic <libname>

这应该是命令。但海湾合作委员会可能不会将一切视为动态

关于c++ - 如何编译静态链接除一个库之外的所有库 (G++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14603431/

相关文章:

c++ - 为什么我的 double 或 int 值在除法后总是 0?

c++ - 不能在 cmake_pch.h 中包含标准库

c++ - GetSystemTimes() 的问题

c++ - g++ 和 cilkscreen 用于检测竞争条件

c++ - 修改 Makefile 以包含库并使用 C++11

c++ - 将特征矩阵转换为 unsigned char *

android - Android NDK 应用程序中的 wstring 支持

linux - 如何访问 .gdbinit 内部和 gdb 内部的环境变量?

linux - Ubuntu 16.04 如何删除多个大文件集

c++ - Opencv C++ g++ MakeFile 以加快编译速度