TransWikia.com

link error when using random_device from boost

Stack Overflow Asked by dikiidog on November 7, 2021

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 Answers

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()

Answered by MatzZze on November 7, 2021

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

g++  -o  test  test.cpp  -lboost_random

Answered by Ali Askari on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP