c++randomcmakeabseil

Trouble with Abseil random


I'm trying to use some functions from ABSEIL random with Cmake. (like absl::Uniform()) I made all like in this instruction. And all work. Then i want more and started to test absl::Uniform(). ABSEIL RANDOM

My hello_word.cc is like

#include <iostream>
#include <string>
#include <vector>
#include "absl/strings/str_join.h"
#include "absl/random/random.h"


int main() {
  std::vector<std::string> v = {"foo","bar","baz"};
  std::string s = absl::StrJoin(v, "-");

  absl::BitGen bitgen;
  double fraction = absl::Uniform(bitgen, 0, 1.0);


  std::cout << "Joined string: " << s << "\n";
}

and CmakeLists.txt is:

cmake_minimum_required(VERSION 3.5)
project(my_project)

# Pick the C++ standard to compile with.
# Abseil currently supports C++11, C++14, and C++17.
set(CMAKE_CXX_STANDARD 11)

add_subdirectory(abseil-cpp)

add_executable(hello_world hello_world.cc)

target_link_libraries(hello_world absl::base absl::synchronization absl::strings)

architecture

And then cmake gives that: ERROR

/usr/bin/ld: CMakeFiles/hello_world.dir/hello_world.cc.o: in function `absl::random_internal::randen_engine<unsigned long>::randen_engine<absl::random_internal::NonsecureURBGBase<absl::random_internal::randen_engine<unsigned long> >::Seeder&, void>(absl::random_internal::NonsecureURBGBase<absl::random_internal::randen_engine<unsigned long> >::Seeder&)':
hello_world.cc:(.text._ZN4absl15random_internal13randen_engineImEC2IRNS0_17NonsecureURBGBaseIS2_E6SeederEvEEOT_[_ZN4absl15random_internal13randen_engineImEC5IRNS0_17NonsecureURBGBaseIS2_E6SeederEvEEOT_]+0x22): undefined reference to `absl::random_internal::Randen::Randen()'
/usr/bin/ld: CMakeFiles/hello_world.dir/hello_world.cc.o: in function `void absl::random_internal::Randen::Absorb<unsigned int, 60ul, unsigned long, 32ul>(unsigned int const (&) [60ul], unsigned long (&) [32ul]) const':
hello_world.cc:(.text._ZNK4absl15random_internal6Randen6AbsorbIjLm60EmLm32EEEvRAT0__KT_RAT2__T1_[_ZNK4absl15random_internal6Randen6AbsorbIjLm60EmLm32EEEvRAT0__KT_RAT2__T1_]+0x33): undefined reference to `absl::random_internal::RandenHwAes::Absorb(void const*, void*)'
/usr/bin/ld: hello_world.cc:(.text._ZNK4absl15random_internal6Randen6AbsorbIjLm60EmLm32EEEvRAT0__KT_RAT2__T1_[_ZNK4absl15random_internal6Randen6AbsorbIjLm60EmLm32EEEvRAT0__KT_RAT2__T1_]+0x48): undefined reference to `absl::random_internal::RandenSlow::Absorb(void const*, void*)'
/usr/bin/ld: CMakeFiles/hello_world.dir/hello_world.cc.o: in function `void absl::random_internal::NonsecureURBGBase<absl::random_internal::randen_engine<unsigned long> >::Seeder::generate_impl<unsigned int*>(std::integral_constant<bool, true>, unsigned int*, unsigned int*)':
hello_world.cc:(.text._ZN4absl15random_internal17NonsecureURBGBaseINS0_13randen_engineImEEE6Seeder13generate_implIPjEEvSt17integral_constantIbLb1EET_SA_[_ZN4absl15random_internal17NonsecureURBGBaseINS0_13randen_engineImEEE6Seeder13generate_implIPjEEvSt17integral_constantIbLb1EET_SA_]+0x80): undefined reference to `absl::random_internal::RandenPool<unsigned int>::Fill(absl::Span<unsigned int>)'
/usr/bin/ld: CMakeFiles/hello_world.dir/hello_world.cc.o: in function `void absl::random_internal::Randen::Generate<unsigned long, 32ul>(unsigned long (&) [32ul]) const':
hello_world.cc:(.text._ZNK4absl15random_internal6Randen8GenerateImLm32EEEvRAT0__T_[_ZNK4absl15random_internal6Randen8GenerateImLm32EEEvRAT0__T_]+0x32): undefined reference to `absl::random_internal::RandenHwAes::Generate(void const*, void*)'
/usr/bin/ld: hello_world.cc:(.text._ZNK4absl15random_internal6Randen8GenerateImLm32EEEvRAT0__T_[_ZNK4absl15random_internal6Randen8GenerateImLm32EEEvRAT0__T_]+0x4a): undefined reference to `absl::random_internal::RandenSlow::Generate(void const*, void*)'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/hello_world.dir/build.make:104: hello_world] Error 1
make[2]: *** [CMakeFiles/Makefile2:581: CMakeFiles/hello_world.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:588: CMakeFiles/hello_world.dir/rule] Error 2
make: *** [Makefile:164: hello_world] Error 2

And question: How to make it work? Thank you in advance.


Solution

  • Thank all.

    I haven't seen here "Available Abseil CMake Public Targets" that for random shoud be absl::random_random

    CMakeLists.txt should be like:

    cmake_minimum_required(VERSION 3.5)
    project(my_project)
    
    # Pick the C++ standard to compile with.
    # Abseil currently supports C++11, C++14, and C++17.
    set(CMAKE_CXX_STANDARD 11)
    
    add_subdirectory(abseil-cpp)
    
    add_executable(hello_world hello_world.cc)
    
    target_link_libraries(hello_world absl::base absl::synchronization absl::strings absl::random_random)