1

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.

Toreno96
  • 1,541
  • 2
  • 14
  • 14
  • Off topic: if [`std::random_device`](http://en.cppreference.com/w/cpp/numeric/random/random_device) works intelligently in your version of mingw, you can discard boost for this. – user4581301 Apr 22 '16 at 23:20
  • 1
    @ user4581301 Nope, `std::random_device` isn't implemented nondeterministically in my version of mingw. – Toreno96 Apr 22 '16 at 23:23

1 Answers1

2

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.

Toreno96
  • 1,541
  • 2
  • 14
  • 14
  • For me, on Linux, it worked with `-lboost_random`, which is pretty easy to forget still – Doot Mar 15 '21 at 22:04
  • 1
    @Doot Yeah, Linux simplifies it a little bit, however the order (which was actual reason of the error) is still important. – Toreno96 Mar 17 '21 at 09:27