2012-08-15 132 views
0

任何人都可以告訴我我做錯了什麼嗎? 這裏是我的代碼main.cpp中在使用makefile和boost線程時遇到問題

#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; 
} 

make文件代碼

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的完成,存儲在/選擇/本地/包括

庫文件夾中的/ usr /本地/ 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一起工作。我不斷看到編譯錯誤。如果任何人有任何想法如何讓boost線程程序使用xcode 4請分享。

更新* 我添加了鏈接

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 

回答

2

你有兩個錯誤。

首先是您嘗試使用gcc編譯C++文件。雖然它起作用,但不會添加C++文件所需的所有庫和其他內容。您應該改用g++

二是在Makefile在該行

BOOST_LINK_FLAGS=lboost_thread-mt 

這看起來並不像一個正確的庫標誌給我。此外,鏈接時甚至不會在命令行中使用它,這意味着您不會鏈接庫。

相關問題