2014-04-24 3 views
1

내 PC에 부스트 통합 소스 코드를 컴파일하는 데 문제가 있습니다. 나는 컴파일 환경 아래부스트 멀티 스레딩 단순 프로그램의 컴파일 오류를 해결하는 방법

OS: CentOs 6.3 
Boost version: 1.41 
Boost installation header file directory: /usr/include/boost/ 
IDE: code::block 

프로그램을 일하고 :

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

using namespace std; 

void ThreadFunction() 
{ 
    int counter = 0; 

    for(;;) 
    { 
     std::cout << "thread iteration " << ++counter << " Press Enter to stop" << std::endl; 

     try 
     { 
      // Sleep and check for interrupt. 
      // To check for interrupt without sleep, 
      // use boost::this_thread::interruption_point() 
      // which also throws boost::thread_interrupted 
      boost::this_thread::sleep(boost::posix_time::milliseconds(500)); 
     } 
     catch(boost::thread_interrupted&) 
     { 
      std::cout << "Thread is stopped" << std::endl; 
      return; 
     } 
    } 
} 

int main() 
{ 
    // Start thread 
    boost::thread t(&ThreadFunction); 

    // Wait for Enter 
    char ch; 
    std::cin.get(ch); 

    // Ask thread to stop 
    t.interrupt(); 

    // Join - wait when thread actually exits 
    t.join(); 
    std::cout << "main: thread ended" << std::endl; 


    return 0; 
} 

오류 출력 : 부스트 라이브러리에 선언 된 메소드를 찾을 수 없습니다 오류 콘솔 컴파일러로

Build: Debug in testPro (compiler: GNU GCC Compiler) ===| 
    obj/Debug/main.o||In function `main':| 
    <path to code>/main.cpp|40|undefined reference to `boost::thread::interrupt()'| 
    <path to code>/main.cpp|43|undefined reference to `boost::thread::join()'| 
    <path to code>/main.cpp|47|undefined reference to `boost::thread::~thread()'| 
    <path to code>/main.cpp|47|undefined reference to `boost::thread::~thread()'| 
    obj/Debug/main.o||In function `thread_data_base':| 
    /usr/include/boost/thread/pthread/thread_data.hpp|65|undefined reference to `vtable for boost::detail::thread_data_base'| 
    obj/Debug/main.o||In function `void boost::this_thread::sleep<boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000l> >(boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000l> const&)':| 
    /usr/include/boost/thread/pthread/thread_data.hpp|122|undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'| 
    obj/Debug/main.o||In function `thread<void (*)()>':| 
    /usr/include/boost/thread/detail/thread.hpp|191|undefined reference to `boost::thread::start_thread()'| 
    obj/Debug/main.o||In function `~thread_data':| 
    /usr/include/boost/thread/detail/thread.hpp|40|undefined reference to `boost::detail::thread_data_base::~thread_data_base()'| 
    ]+0x10)||undefined reference to `typeinfo for boost::detail::thread_data_base'| 
    ||=== Build failed: 9 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===| 

. 몇 가지 샘플 애플리케이션을 시도했지만 결과는 동일했습니다. 문제를 찾아 내거나 부스트 설치 상태를 확인하는 방법을 알려주십시오.

+0

당신은 내가 코드 :: 블록 IDE를 사용하고 코드 –

+0

를 컴파일하는 데 사용하는 명령을 제공하고 지금은 추가로 시도 할 것이다 링커 디렉토리 및 추가 부스트 라이브러리를 프로젝트에 추가합니다. –

답변

1

당신은 boost_thread 라이브러리와 코드를 컴파일 할 필요가 예를 들어 -lboost_thread

gcc test.c -o test -lboost_thread 
+0

아마도 -pthread를 언급 할 수도 있습니다. – sehe

+0

예, code : block 프로젝트의 링커 검색 디렉토리 목록에 부스트 라이브러리 디렉토리를 추가했습니다. 그런 다음 효과가 있습니다. -감사. –

+0

@Dig 코드 : 환영합니다. –