0
votes

I have installed on MacOSX 10.15 boost using brew, all working fine, beside random_device.

This is what i have written:

#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/discrete_distribution.hpp>
#include <boost/random/random_device.hpp>

int main() {
    boost::random::random_device rand_dev;
    boost::mt19937 gen(rand_dev());
    double probabilities[]{0, 0.99, 0.01, 0};
    boost::random::discrete_distribution<> dist(probabilities);
    std::cout << dist(gen);

    return 0;
}

And this is what i have got from compiler:

Undefined symbols for architecture x86_64:

"boost::random::random_device::random_device()", referenced from: _main in main.cpp.o

"boost::random::random_device::~random_device()", referenced from: _main in main.cpp.o

"boost::random::random_device::operator()()", referenced from: _main in main.cpp.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am linking it using CMake. And also i have installed it on the Ubuntu 18 and got same linking errors.

This is a part of my CMake:

find_package(Boost 1.72)  

if(Boost_FOUND)
     include_directories(${Boost_INCLUDE_DIRS})
     target_link_libraries(test_boost ${Boost_LIBRARY_DIR}) 
endif()
2
"And this is what i have got from compiler:" These are the errors from the linker, not compiler. How are you linking your application? Did you link with the relevant boost library?Algirdas Preidžius
I am linking it using CMake. And also i have installed it on the Ubuntu 18 and got same linking errors. This is a part of my CMake: find_package(Boost 1.72) if(Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) target_link_libraries(test_boost ${Boost_LIBRARY_DIR}) endif() dikiidog
All information necessary to answer a question, must be present in the question, and not in the comments. Please edit your question to add said information.Algirdas Preidžius

2 Answers

0
votes

Edit your CMakeLists.txt with the following code:

find_package(Boost 1.72 COMPONENTS random)
if(Boost_FOUND) 
    include_directories(${Boost_INCLUDE_DIRS}) 
    target_link_libraries(test_boost ${Boost_LIBRARIES}) 
endif()
0
votes

Add -lboost_random to linker inputs as compiler command line argument:

g++  -o  test  test.cpp  -lboost_random