0

I'm trying to create a boost::thread application in Qt. Here is my code:

#include <iostream>

#include "boost/thread.hpp"
#include "boost/bind.hpp"

using namespace std;

class A {
public:
    void tf() {
        for (int i = 0; i < 100; ++i) {
            cout << i << endl;
        }
    }
};

int main()
{
    boost::shared_ptr<A> aPtr;
    cout << "Hello World!" << endl;
    boost::thread t = boost::thread(boost::bind(&A::tf, aPtr.get()));
    cout << "Thread started" << endl;
    return 0;
}

And the corresponding .pro file:

TEMPLATE = app
CONFIG += console
CONFIG -= qt

LIBS += -L"C:/Program Files (x86)/boost/boost_1_49/lib"
DEPENDPATH += "C:/Program Files (x86)/boost/boost_1_49"
INCLUDEPATH += "C:/Program Files (x86)/boost/boost_1_49"

SOURCES += main.cpp

When i try to compile it i get:

{{path}}\main.cpp:21: error: undefined reference to `_imp___ZN5boost6threadD1Ev'
{{path}}\main.o:-1: In function `ZN5boost6threadC1INS_3_bi6bind_tIvNS_4_mfi3mf0Iv1AEENS2_5list1INS2_5valueIPS6_EEEEEEEET_NS_10disable_ifINS_14is_convertibleIRSE_NS_6detail13thread_move_tISE_EEEEPNS0_5dummyEE4typeE':
c:\Program Files (x86)\boost\boost_1_49\boost\thread\detail\thread.hpp:205: error: undefined reference to `_imp___ZN5boost6thread12start_threadEv'
collect2.exe:-1: error: error: ld returned 1 exit status

Whats the problem? What am i missing?

M.

Roman C
  • 49,761
  • 33
  • 66
  • 176
spiralfuzet
  • 75
  • 10
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – sashoalm Apr 03 '13 at 11:37

1 Answers1

2

You aren't linking to the Boost Thread library, you are just telling Qt where it is.

LIBS += -L"C:/Program Files (x86)/boost/boost_1_49/lib" -lboost_thread
cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • Hi! I've already tried that. I get ":-1: error: cannot find -lboost_thread" M. – spiralfuzet Apr 03 '13 at 09:50
  • even if this does not work it is the way to go. you need at least that. – UmNyobe Apr 03 '13 at 09:56
  • @spiralfuzet Please tell me you didn't just copy and paste my example without even checking what the file is called in your `boost_1_49/lib` directory!? – cmannett85 Apr 03 '13 at 10:16
  • @cmannett85 Please be patient. I haven't got any experience with boost. How do i get the filename? Do i need to create it my own? – spiralfuzet Apr 03 '13 at 10:27
  • @spiralfuzet Look in your `boost_1_49/lib` directory and find the library with the word 'thread' in it, there may be a few versions with increasing degrees of version specificity - choose the least specific. If you are unsure, list them here. – cmannett85 Apr 03 '13 at 10:50
  • @spiralfuzet you have to build boost, as described here: http://www.boost.org/doc/libs/1_53_0/more/getting_started/windows.html . Then you'll get `libboost_thread.a` static library, which you link to your project. – Igor R. Apr 03 '13 at 10:51
  • Its seems i've a little misunderstanding about boost header-only and binary library. I wasn't sure which is which. Also i wasn't aware the existence of the boost binary library. Now i see. Thanks for the help. I think this would be helpful for other boost-rookie guys like me. – spiralfuzet Apr 03 '13 at 12:15