2012-08-15 2 views
0

누구든지 내가 뭘 잘못하고 있다고 말할 수 있습니까? 가 여기 내 코드 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를 통해 수행되고 저장된다/옵션/지역/포함

라이브러리 폴더는/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_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++을 사용해야합니다.

두 번째는 나에게 적절한 라이브러리 깃발처럼 보이지 않는 선

BOOST_LINK_FLAGS=lboost_thread-mt 

Makefile입니다. 게다가 링크 할 때 커맨드 라인에 라이브러리가 없기 때문에 라이브러리와 링크하지 않습니다.

관련 문제