Boost on windows with codeblocks - error: undefined reference to boost - c++

Tons of these errors are popping up whenever I try to use boost in windows with codeblocks. On linux it works fine.
Even though I did add the boost .lib files and the include path to the GCC compiler in codeblocks.
I first compiled using bootstrap.bat + b2.exe but apperently that's wrong. A user in another forum said he fixed it but when I try his method I get:
So to clarify; what I did was:
. Add "C:\Program Files (x86)\CodeBlocks\MinGW\bin" to PATH
. Run bootstrap.bat which makes bjam.exe
. Run cmd.exe, cd to boost folder, enter bjam --build-dir=C:\boost --build-type=complete gcc stage
But then I get that error.
Why does compiling/using C++ libraries on windows always net these kind of errors that take hours to solve, while on linux it's nearly always flawless -.-
Who knows how to fix this one?

The technique that I use is as follows:
Set up the PATH environmental variable to include the bin directory of your GCC installation.
Extract boost somewhere.
Open a comand prompt and cd into the root of boost (the folder named boost_x_xx_x which has boost, doc, libs etc... subdirectories).
To compile boost, run:
bootstrap.bat
b2 toolset=gcc variant=release link=static threading=multi install
This will install boost to C:\Boost. If you want to install it elsewhere use the --build-dir= option when running b2.

Related

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.
executing the following command in the command prompt in the directory (D:\Development\Boost): bootstrap gcc
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
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.

Boost.Python not the .lib

I think I have built it properly. I have other boost libraries working. I am getting the following error
Error: SNK1104 cannot open file 'boost_pythonPY_MAJOR_VERSIONPY_MINOR_VERSION-vc141-mt-x32-1_67.lib'.
I'm on windows using Visual Studios, with boost 1.67
boost-python is notoriously hard to install. Make sure you specify with-python when building from source. Something like this:
Download and configure boost
cd boost_1_55_0\tools\build\v2\engine
build.bat mingw
Add this to your path
C:\boost_1_55_0\tools\build\v2\engine\bin.ntx86
Build from source
bjam toolset=gcc --with-python link=shared
Add this to your path
C:\boost_1_55_0\stage\lib

CLion Boost library

I have installed Boost library following these commands:
I have extracted downloaded boost_1_67_0.7zfile
Then I have executed command bootstrap.bat gcc
Then I executed command b2 install --prefix=c:/Boost toolset=gcc
Then I restared my computer, just in case
Then I have opened CLion (newest version) and tried to include boost library in my program with find_package(Boost), but that doesn't work for me. Am I doing something wrong here or there is some other problem?
CMake file
Source code for my program
Folder hierarchy
Also I tried to say CMake whereis Boost installed with command
set(BOOST_ROOT C:/Boost/include/boost-1_67)
Then CMake can detect Boost, but it doesn't want to compile because it
can't find any file, for example boost/thread.hpp.

Boost libraries - build only what I need

I downloaded the Boost libraries and now I want to build only a few of the libraries. What would be the right command for this? Apparently the build-type=complete option gives me too much.
I am using Windows XP and want to use Bjam to compile Boost and MinGW to finally use it. At the moment I think I need the libraries Boost.filesystem, Boost.ProgramOptions and Boost.System.
Another question: Where do I put the header-only libraries?
In step 5.2.4 of Getting Started you can instruct b2 which libraries to build:
./b2 --with-program_options --with-filesystem --with-system
Alternatively, use ./b2 --show-libraries to see a list of all libraries that are not header-only.
Following is an excerpt from the page:
In particular, to limit the amount of time spent building, you may be interested in:
reviewing the list of library names with --show-libraries
limiting which libraries get built with the --with-<library-name> or --without-<library-name> options
choosing a specific build variant by adding release or debug to the command line.
Note: b2 command depends upon boost version so use following commands as per your boost version(Also, in this case use --with-libraries=<comma-seperated-library-names> version instead of --with-<library-name>):
./configure for 1.38.0 and earlier
./bootstrap.sh for 1.39.0 onwards till 1.46.0
These answers didn't work for me. On Windows, this worked for me:
b2.exe --with-LIBRARY
For example,
b2.exe --with-test
The BCP utility is a tool for extracting subsets of Boost. It's useful for Boost authors who want to distribute their library separately from Boost, and for Boost users who want to distribute a subset of Boost with their application.
The current version of Boost (1.50.0) uses Boost.Build. The new workflow for building BCP is as follows:
From the root Boost directory, type:
bootstrap.bat
Then, once Boost.Build has been built, type:
b2 tools/bcp
To extract, for example interprocess only, you could use:
$ mkdir /tmp/interprocess #bcp needs this
$ bcp interprocess /tmp/interprocess
This copies interprocess and its dependencies to /tmp/interprocess.
I had the same problem. But I found a way to create the necessary files.
Steps to follow:
If you have Microsoft Visual Studio 2010 then open the Microsoft Visual Studio command prompt (2010) in administrator mode.
First enter the code:
bootstrap.bat
Then enter the following code to generate lib files:
b2.exe link=static runtime-link=static --with-chrono --with-date_time --with-filesystem --with-program_options --with-system --toolset=msvc-10.0 define=BOOST_USE_WINAPI_VERSION=0x0500
Library files will be created in stage folder.
My last build attempt for the Boost 1.55 libraries was a dissappointment. All attempts to build several libraries and only them has ended up with total mess in output.
BJam either tries to build everything or build only the requested, but in the "bin.v2/[long-random-path]" folders (library per directory) with crap in them which is a headache to copy only the link libraries to somethere else.
I have accidentally found the right way to build-and-collect only the libraries I want in one place without any other crap:
bjam ... --with-[library1] --with-[library2] stage
the "stage" option is required to build and collect libraries in one folder: /stage/lib
the "--with-[library]" option is required to build only the library you want.
An additional note for anyone who sees the error message:
"error: wrong library name '<name of library>' in the --with-<library> option.".
If you try to be clever, as I did, and only extract the boost sub-directory of the download onto your system to minimise space, b2 will not be able to find the source code and build options for those libraries that are not header-only. I.e. you need the lib sub-directory too (and tools).

Building Boost for static linking (MinGW)

I'm building Boost (I'm using System and FileSystem) for MinGW using bjam:
bjam --toolset=gcc stage
And it builds fine, but I want to be able to statically link to it (I have to have a single file for the final product) so I tried:
bjam --link=static --toolset=gcc stage
But I get the same output. Any ideas?
edit second question in a row I've answered moments after posting :p guess I'll leave this up here for others though.
bjam --build-type=complete --toolset=gcc stage
Will build both dynamic and static for sure.
I think link is a property as opposed to an option for bjam. That means that there should be no -- before it.
This is my command line for building only static libraries (visual c++ though):
bjam install --toolset=msvc variant=release link=static threading=multi runtime-link=static
Mapping that to your original build command I would say it should look something like this:
bjam --toolset=gcc link=static stage
or perhaps:
bjam stage --toolset=gcc link=static
Try running
bjam --help
for more info on properties and options for bjam.
Just want note that with the newer boost (Feb 2011) you need to build bjam as well now.. for some reason the current downloadable bjam doesn't work cleanly.
So first:
cd ...\boost_1_45_0\tools\build\v2\engine\src
build.bat mingw
Youll need to add bjam to the PATH (from control panel, not just on the cmd prompt). Then
cd ...\boost_1_45_0\
bjam --build-type=complete --toolset=gcc stage
My setup is Vista, Boost 1.45, MinGW 4.5, and building from cmd.exe not msys.
http://code-slim-jim.blogspot.com/2011/02/boost-in-vista-using-mingw-and-cmdexe.html
I have not built this myself in MinGW but I believe your first build will output both dynamic and static libraries and the second only static ones, so if you did not clean the build directories in between builds it will probably look the same.
When you write "I have to have a single file..." do you mean you need a single library containing all boost libraries? If so, I suspect you may have to 'ar' them together yourself.
I use the following command line to build both the statically linked as well as the dynamically linked versions of boost:
bjam "-sBUILD=debug release <runtime-link>static/dynamic <threading>multi" stage
This is done with visual c++ on windows YMMV.