4

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

gdebast
  • 43
  • 1
  • 4
  • Did you build boost? Although boost contains header only libraries it does also contain libraries that need to be compiled. – drescherjm Jun 13 '20 at 13:53
  • This should help: [https://stackoverflow.com/questions/20265879/how-do-i-build-boost-1-55-with-mingw](https://stackoverflow.com/questions/20265879/how-do-i-build-boost-1-55-with-mingw) Note to build the current version not 1.55 that is many years old. – drescherjm Jun 13 '20 at 13:56

1 Answers1

2

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.

kenba
  • 4,303
  • 1
  • 23
  • 40
  • You updated your question while I posted my answer, amazing timing! You only built `boost-filesystem`, you also need to build `boost-system`. – kenba Jun 14 '20 at 08:36
  • So are my step ok so far? in the meantime, I have undo everything and follow exactly your steps. I'll see what it brings. I do not mind doing the undo-do-undo-do loop until it works :-D – gdebast Jun 14 '20 at 08:52
  • it does not work. In Qt creator, I got: "error: cannot find -lboost_system-mgw73-mt-x64-d-1_73" – gdebast Jun 14 '20 at 08:58
  • Yes, your steps are ok so far, And, no don't undo everything, just build `boost-system`: b2 toolset=gcc system and then try to link your program. – kenba Jun 14 '20 at 08:58
  • I have built 'system' and added ''LIBS += -LD:/Development/Boost/stage/lib -l boost_system-mgw73-mt-x64-d-1_73". Now the error is "error: cannot find -lboost_system-mgw73-mt-x64-d-1_73". Any thought? – gdebast Jun 14 '20 at 09:10
  • Yes, read the last paragraph of my answer and think it through! – kenba Jun 14 '20 at 09:13
  • I think that I begin to understand : b2 creates dlls and I should refer to those files in the .pro file. So when looking in the stage/lib directory, I found all those dll there. Before, stage/lib was not there. I have added "LIBS += -LD:/Development/Boost/stage/lib libboost_system-mgw8-mt-x64-1_73", but now I have the same error (error: undefined reference to `boost::system::generic_category()). What did I miss this time? – gdebast Jun 14 '20 at 10:06
  • The problem is gone by simply reediting the cpp calling FileSystem... thanks and sorry for upsetting you. – gdebast Jun 14 '20 at 12:42