Hunk
Hunk

Reputation: 485

Link Boost in Qt Linux

I want to link boost in my pro file and found following answer:

Boost with Qt Creator and Linux

I build a minimal example:

My pro File

QT       += core
QT       -= gui

TARGET = threadtest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
INCLUDEPATH +=/path/boost/
LIBS += -L/path/boost/lib/ -lboost_thread -lboost_system

SOURCES += main.cpp

My main:

#include <QtCore/QCoreApplication>

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

void woker(){
    boost::posix_time::seconds t(10);
    std::cout <<" working" << std::endl;
    boost::this_thread::sleep(t);
}

int main(int argc, char *argv[])
{
    std::cout <<" r" << std::endl;

    QCoreApplication a(argc, argv);
    boost::thread w(woker);
    w.join();
    std::cout <<" d" << std::endl;

    return a.exec();
}

I get the undefined reference to boost error. I don't understand this...

In Windows I only did:

LIBS += "-LC:/boost_1_55_0/lib32-msvc-10.0/"

However, this is also not working in Linux.

Does anyone have any ideas?


Edit

14:41:04: Führe Build-Schritte für Projekt threadtest aus...
14:41:04: Starte "/usr/bin/qmake-qt4" /home/user/threadtest/threadtest.pro -r -spec linux-g++
14:41:04: Der Prozess "/usr/bin/qmake-qt4" wurde normal beendet.
14:41:04: Starte "/usr/bin/make" -w
make: Entering directory `/home/user/threadtest-build-desktop-Qt_4_8_1_in_Pfad__System__Release'
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../threadtest -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I../boost -I. -I../threadtest -I. -o main.o ../threadtest/main.cpp
g++ -Wl,-O1 -o threadtest main.o    -L/usr/lib/x86_64-linux-gnu -L/home/user/boost/lib/ -lQtCore -lpthread 
main.o: In function `woker()':
make: Leaving directory `/home/user/threadtest-build-desktop-Qt_4_8_1_in_Pfad__System__Release'
main.cpp:(.text+0x2aa): undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
main.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
main.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x8): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
main.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
main.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED0Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0xc): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
main.o: In function `boost::detail::thread_data<void (*)()>* boost::detail::heap_new_impl<boost::detail::thread_data<void (*)()>, void (*&)()>(void (*&)())':
main.cpp:(.text._ZN5boost6detail13heap_new_implINS0_11thread_dataIPFvvEEERS4_EEPT_T0_[boost::detail::thread_data<void (*)()>* boost::detail::heap_new_impl<boost::detail::thread_data<void (*)()>, void (*&)()>(void (*&)())]+0x52): undefined reference to `vtable for boost::detail::thread_data_base'
main.o: In function `main':
main.cpp:(.text.startup+0x60): undefined reference to `boost::thread::start_thread()'
main.cpp:(.text.startup+0x6a): undefined reference to `boost::thread::join()'
main.cpp:(.text.startup+0x92): undefined reference to `boost::thread::~thread()'
main.cpp:(.text.startup+0xc6): undefined reference to `boost::thread::~thread()'
main.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status
make: *** [threadtest] Error 1
14:41:06: Der Prozess "/usr/bin/make" wurde mit dem Rückgabewert 2 beendet.
Fehler beim Erstellen des Projekts threadtest(Ziel: Desktop)
Bei der Ausführung von Build-Schritt 'Make'

EDIT 2:

cmake_minimum_required(VERSION 2.8)

project(app_project)

set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT path/boost)
find_package(Boost 1.55 REQUIRED COMPONENTS thread filesystem)
if(BOOST_FOUND)
  message("boost found")
endif()
add_executable(test main.cpp)

if(TARGET test)
  target_link_libraries(test
    -lboost_thread
    -lboost_system
)
endif()

I wrote a small example with cmake. This is running well, however on cmake i have the command ignore boost system path. You know something similar to qt ?

Upvotes: 1

Views: 4919

Answers (2)

Pratham
Pratham

Reputation: 1713

When I tried using only INCLUDEPATH += /path/to/boost/directory/ worked on both Windows and Linux. No need to use LIBS += .... .

Upvotes: 0

DannyK
DannyK

Reputation: 1552

For VS2008, Boost 1_55, Qt4 4.8.6, I was able to get your code to compile with the following qmake file.

QT       += core
QT       -= gui

TARGET = threadtest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
win32{
INCLUDEPATH += d:/gnu/vs2008/x64/include
LIBS += -Ld:/gnu/vs2008/x64/lib 
LIBS += -lboost_thread-vc90-mt-1_55
LIBS += -lboost_system-vc90-mt-1_55

}
unix{
LIBS += -L/usr/lib64
LIBS += -L/usr/local -L/usr -lboost_thread-mt -lboost_system-mt
}

SOURCES += main.cpp

With the same qmake pro file, i was also able to get your code to compile on RHEL6 with Qt4 4.8.6 and the stock Boost

Upvotes: 2

Related Questions