Search code examples
c++windowsboostg++boost-random

Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc


My OS is Windows 7 64-bit and C++ compiler I'm using is:

g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0

And I installed Boost version 1.60 using:

bootstrap.bat mingw
b2 install target=gcc

Then I tested is it working, using examples from Boost.Random tutorial.

With the first two everything was fine, but the third one gave linker errors about boost::random::random_device. I minimized the code to have only this:

// Compiled with:
// g++ -IC:/Boost/include/boost-1_60
// -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
// main.cpp

#include "boost/random/random_device.hpp"

int main() {
    boost::random::random_device rng;
}

And I get the following errors:

C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x15):
undefined reference to `boost::random::random_device::random_device()'
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x20):
undefined reference to `boost::random::random_device::~random_device()'
collect2.exe: error: ld returned 1 exit status

Here, on SO, I found that someone with similar problem added -lboost_system to flags, but for me it didn't helped.

Does anyone have any idea, why it isn't working? I checked, and I have random_device.hpp header in my Boost folder, with declarations of random_device() and ~random_device() in it.


Solution

  • I found what was wrong - the g++ command syntax, that I wanted to use to compile and link my code.

    As I wrote in my question, I do this that way:

    g++ -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60 main.cpp
    

    While the correct one is with main.cpp (or any other source code file(s), that we want to include in compiling process) before the -L and -l flags. For example:

    g++ -IC:/Boost/include/boost-1_60 main.cpp -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
    

    or even

    g++ main.cpp -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
    

    Hope it will help anyone, who will make such silly mistake too.