Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost with Qt Creator

I was trying to use boost/filesystem in my C++ project made with QtCreator. The problem was that when building, I got the following error:

"error: undefined reference to `boost::system::generic_category()'"

To use boost, I had performed the following actions:

  • download boost library boost_1_73_0.7z file
  • unzip it in my computer (under D:\Development\Boost)
  • in .pro file, I have added the following option

    INCLUDEPATH += D:/Development/Boost

  • in my .cpp file, I have added the following include

    #include "boost/filesystem.hpp"

  • At this point, when compiling, I had the following error in Qt creator IDE

    "error: undefined reference to `boost::system::generic_category()'"

The root cause is the following : FileSystem needs to be built. Therefore, I have built this boost library by :

  • adding gcc and g++ to the path variable (it is succesful as I could call 'g++' and 'gcc' from the command prompt).
  • opening Qt command prompt (I used Qt 5.15.0 (MinGW 73.0 64-bit) ) and by navigating to the repository where boost is installed.

enter image description here

  • executing the following command in the command prompt in the directory (D:\Development\Boost): bootstrap gcc

enter image description here

  • executing the following command in the command prompt in the directory where I had unzipped Boost: b2 toolset=gcc link=shared threading=multi --build-type=complete stage. This action has created a the directory D:\develoment\Boost\Stage\lib with all the dll, including 'libboost_filesystem-mgw8-mt-d-x64-1_73.dll'.

  • Now it's time to link the library in Qt creator. I have thus added the following in my .pro file:

    LIBS += -LD:/Development/Boost/stage/lib libboost_filesystem-mgw8-mt-d-x64-1_73

When compiling, the error is gone.

Thanks for your help. Gatien

like image 641
gdebast Avatar asked Sep 08 '25 09:09

gdebast


1 Answers

As @drescherjm commented, you need to build the boost libraries.
They are not in the D:/Development/Boost/libs directory.

You appear to be using Windows and have boost installed on your "D:" drive.
I assume your using the MinGw compiler that comes with Qt Creator, not Visual Studio.

To build boost with MinGw, first open the relevant Qt Command prompt, e.g. Qt 5.12.3 (MinGW 7.3.0 64-bit) and type the following:

D:
cd \Development\Boost
bootstrap.bat gcc
b2 toolset=gcc link=shared threading=multi --build-type=complete stage

This will build the MinGw boost libraries in your directory: D:\Development\Boost\stage\lib.
Then change the link command to:

LIBS += -LD:/Development/Boost/stage/lib -l boost_system-mgw73-mt-x64-d-1_66

Note: the precise name of the boost_system library depends upon how boost named it in your version.
See Boost Getting Started on Windows: library naming. the answer here: mingw-w64 cannot find -lboost_filesystem and the filenames you built in the D:\Development\Boost\stage\lib directory.

like image 60
kenba Avatar answered Sep 09 '25 21:09

kenba