使用带有增强线程的makefile时遇到麻烦

时间:2012-08-15 02:36:01

标签: c++ multithreading boost xcode4.2

谁能告诉我我做错了什么? 这是我的代码main.cpp

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

using namespace boost;
using namespace boost::this_thread;
using namespace std;
// Global function called by thread
void GlobalFunction()
{
    for (int i=0;i<10;++i)
    {
        cout << i << "Do something in parallel with main method." << endl;
        boost::this_thread::yield();
    }
}
void GlobalThreadTest()
{
    boost::thread t(&GlobalFunction);
    for (int i = 0; i<10; ++i) {
        cout << i << "Do something in main method. " << endl;
    }
}

int main(int argc, const char * argv[])
{
    GlobalThreadTest();
    return 0;
}

制作文件代码

BOOST_ROOT=/opt/local
PRODUCT_NAME=example
BOOST_INCLUDE_DIR=$(BOOST_ROOT)/include
BOOST_LIB_DIR=/usr/local/lib
BOOST_LINK_FLAGS=lboost_thread-mt
main: main.cpp
    gcc main.cpp -I$(BOOST_INCLUDE_DIR) -L$(BOOST_LIB_DIR) -o $(PRODUCT_NAME)
clean:
    rm -f main

我的升级安装是通过MacPorts完成的,存储在/ opt / local / include

库文件夹/ usr / local / lib我相信是这种情况

这是我看到的错误

$ make -f makefile
gcc main.cpp -I/opt/local/include -L/usr/local/lib -o example
Undefined symbols for architecture x86_64:
  "boost::this_thread::yield()", referenced from:
      GlobalFunction()     in ccPsXnoZ.o
  "boost::detail::thread_data_base::~thread_data_base()", referenced from:
      boost::detail::thread_data<void (*)()>::~thread_data()in ccPsXnoZ.o
      boost::detail::thread_data<void (*)()>::~thread_data()in ccPsXnoZ.o
  "boost::system::system_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccPsXnoZ.o
      boost::thread_exception::thread_exception(int, char const*)in ccPsXnoZ.o
  "boost::system::generic_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccPsXnoZ.o
  "boost::thread::start_thread()", referenced from:
      boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)in ccPsXnoZ.o
  "boost::thread::detach()", referenced from:
      boost::thread::~thread()in ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::empty() const", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
  "std::bad_exception::what() const", referenced from:
      vtable for boost::exception_detail::bad_exception_in ccPsXnoZ.o
      vtable for boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>in ccPsXnoZ.o
  "std::runtime_error::what() const", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
  "std::bad_alloc::what() const", referenced from:
      vtable for boost::exception_detail::bad_alloc_in ccPsXnoZ.o
      vtable for boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>in ccPsXnoZ.o
  "std::allocator<char>::allocator()", referenced from:
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
  "std::allocator<char>::~allocator()", referenced from:
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      GlobalThreadTest()     in ccPsXnoZ.o
      GlobalFunction()     in ccPsXnoZ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
      GlobalThreadTest()     in ccPsXnoZ.o
      GlobalFunction()     in ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      std::runtime_error::runtime_error(std::runtime_error const&)in ccPsXnoZ.o
      boost::system::system_error::system_error(boost::system::system_error const&)in ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()", referenced from:
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
  "std::bad_exception::~bad_exception()", referenced from:
      boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
      boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
      boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
  "std::runtime_error::runtime_error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
  "std::runtime_error::~runtime_error()", referenced from:
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::system_error(boost::system::error_code, char const*)in ccPsXnoZ.o
      typeinfo for boost::thread_exceptionin ccPsXnoZ.o
      typeinfo for boost::thread_resource_errorin ccPsXnoZ.o
      ...
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for __cxxabiv1::__vmi_class_type_info", referenced from:
      typeinfo for boost::exception_detail::bad_alloc_in ccPsXnoZ.o
      typeinfo for boost::exception_detail::bad_exception_in ccPsXnoZ.o
      typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>in ccPsXnoZ.o
      typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>in ccPsXnoZ.o
      typeinfo for boost::exception_detail::error_info_injector<boost::thread_resource_error>in ccPsXnoZ.o
      typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >in ccPsXnoZ.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for boost::detail::thread_data_base", referenced from:
      boost::detail::thread_data_base::thread_data_base()in ccPsXnoZ.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for std::bad_exception", referenced from:
      std::bad_exception::bad_exception()in ccPsXnoZ.o
      std::bad_exception::bad_exception(std::bad_exception const&)in ccPsXnoZ.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for std::runtime_error", referenced from:
      std::runtime_error::runtime_error(std::runtime_error const&)in ccPsXnoZ.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for std::bad_alloc", referenced from:
      std::bad_alloc::bad_alloc()in ccPsXnoZ.o
      std::bad_alloc::bad_alloc(std::bad_alloc const&)in ccPsXnoZ.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for std::exception", referenced from:
      std::exception::exception()in ccPsXnoZ.o
      std::exception::exception(std::exception const&)in ccPsXnoZ.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator delete(void*)", referenced from:
      boost::detail::sp_counted_base::~sp_counted_base()in ccPsXnoZ.o
      boost::detail::sp_counted_base::~sp_counted_base()in ccPsXnoZ.o
      boost::exception_detail::clone_base::~clone_base()in ccPsXnoZ.o
      boost::exception_detail::clone_base::~clone_base()in ccPsXnoZ.o
      boost::exception_detail::bad_alloc_::~bad_alloc_()in ccPsXnoZ.o
      boost::exception_detail::bad_alloc_::~bad_alloc_()in ccPsXnoZ.o
      boost::exception_detail::bad_exception_::~bad_exception_()in ccPsXnoZ.o
      ...
  "operator new(unsigned long)", referenced from:
      boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>::clone() constin ccPsXnoZ.o
      boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>::clone() constin ccPsXnoZ.o
      boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::clone() constin ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in ccPsXnoZ.o
      ...
  "___cxa_allocate_exception", referenced from:
      boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>::rethrow() constin ccPsXnoZ.o
      boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>::rethrow() constin ccPsXnoZ.o
      boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::rethrow() constin ccPsXnoZ.o
      void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in ccPsXnoZ.o
  "___cxa_begin_catch", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in ccPsXnoZ.o
  "___cxa_call_unexpected", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
      boost::exception::~exception()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
      boost::system::system_error::~system_error()in ccPsXnoZ.o
  "___cxa_end_catch", referenced from:
      boost::system::system_error::what() constin ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in ccPsXnoZ.o
  "___cxa_free_exception", referenced from:
      void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in ccPsXnoZ.o
  "___cxa_guard_abort", referenced from:
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in ccPsXnoZ.o
  "___cxa_guard_acquire", referenced from:
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in ccPsXnoZ.o
  "___cxa_guard_release", referenced from:
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in ccPsXnoZ.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in ccPsXnoZ.o
  "___cxa_pure_virtual", referenced from:
      vtable for boost::detail::sp_counted_basein ccPsXnoZ.o
      vtable for boost::exceptionin ccPsXnoZ.o
      vtable for boost::exception_detail::clone_basein ccPsXnoZ.o
      vtable for boost::system::error_categoryin ccPsXnoZ.o
  "___cxa_rethrow", referenced from:
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in ccPsXnoZ.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in cck3nfxq.o
      boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in cck3nfxq.o
  "___cxa_call_unexpected", referenced from:
      boost::system::system_error::what() constin cck3nfxq.o
      boost::exception::~exception()in cck3nfxq.o
      boost::system::system_error::~system_error()in cck3nfxq.o
      boost::system::system_error::~system_error()in cck3nfxq.o
      boost::system::system_error::~system_error()in cck3nfxq.o
  "___cxa_end_catch", referenced from:
      boost::system::system_error::what() constin cck3nfxq.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in cck3nfxq.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in cck3nfxq.o
      boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in cck3nfxq.o
  "___cxa_free_exception", referenced from:
      void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in cck3nfxq.o
  "___cxa_guard_abort", referenced from:
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in cck3nfxq.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in cck3nfxq.o
  "___cxa_guard_acquire", referenced from:
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in cck3nfxq.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in cck3nfxq.o
  "___cxa_guard_release", referenced from:
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>()in cck3nfxq.o
      boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_exception_>()in cck3nfxq.o
  "___cxa_pure_virtual", referenced from:
      vtable for boost::detail::sp_counted_basein cck3nfxq.o
      vtable for boost::exceptionin cck3nfxq.o
      vtable for boost::exception_detail::clone_basein cck3nfxq.o
      vtable for boost::system::error_categoryin cck3nfxq.o
  "___cxa_rethrow", referenced from:
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>*)in cck3nfxq.o
      boost::detail::shared_count::shared_count<boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_> >(boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>*)in cck3nfxq.o
      boost::detail::shared_count::shared_count<boost::detail::thread_data<void (*)()> >(boost::detail::thread_data<void (*)()>*)in cck3nfxq.o
  "___cxa_throw", referenced from:
      boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_>::rethrow() constin cck3nfxq.o
      boost::exception_detail::clone_impl<boost::exception_detail::bad_exception_>::rethrow() constin cck3nfxq.o
      boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::rethrow() constin cck3nfxq.o
      void boost::throw_exception<boost::thread_resource_error>(boost::thread_resource_error const&)in cck3nfxq.o
  "___gxx_personality_v0", referenced from:
      Dwarf Exception Unwind Info (__eh_frame) in cck3nfxq.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1

我也无法使用XCode。我一直看到编译错误。如果有人有任何想法如何让使用xcode 4的boost线程程序,请分享。

更新* 我添加了链接器

BOOST_ROOT=/opt/local
PRODUCT_NAME=example
BOOST_INCLUDE_DIR=$(BOOST_ROOT)/include
BOOST_LIB_DIR=/usr/local/lib
BOOST_LINK_FLAGS=lboost_thread-mt
main: main.cpp
    g++ main.cpp -I$(BOOST_INCLUDE_DIR) -$(BOOST_LINK_FLAGS) -L$(BOOST_LIB_DIR) -o $(PRODUCT_NAME)
clean:
    rm -f main

错误:

make -f makefile
g++ main.cpp -I/opt/local/include -lboost_thread-mt -L/usr/local/lib -o example
ld: library not found for -lboost_thread-mt
collect2: ld returned 1 exit status
make: *** [main] Error 1

1 个答案:

答案 0 :(得分:2)

你有两个错误。

首先,您尝试使用gcc编译C ++文件。虽然它有效但它不会添加C ++文件所需的所有库和其他东西。您应该使用g++代替。

第二个是在行中的Makefile

BOOST_LINK_FLAGS=lboost_thread-mt

对我来说,这看起来不像是一个合适的图书馆旗帜。此外,链接时甚至没有在命令行中使用它,这意味着您不会链接到库。