0

I want to #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG so as to use nanosecond resolution in boost datetime.

I want to use this nanosecond across the whole shared library I'm going to compile.

So where should I put this line, #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG?

I previously wanted to define it in the CMakeLists.txt file with add_compile_definitions(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG).

However, it does not take effect. This is because within the library, cout << "nanoseconds per unit: " << pt::time_duration(0, 0, 0, 1).total_nanoseconds() << endl; still gives nanoseconds per unit: 1000

Here's the CMakeLists.txt,

cmake_minimum_required(VERSION 3.22)
project(project_name)

set(CMAKE_CXX_STANDARD 17)

add_compile_definitions(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)

find_package( Boost COMPONENTS filesystem iostreams REQUIRED )

include_directories( ${Boost_INCLUDE_DIR} )

include_directories( include )

file(GLOB SOURCES include/*.h src/*.cpp)

add_library(project_name SHARED library.cpp ${SOURCES})

set_target_properties(project_name PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(project_name ${Boost_LIBRARIES})
Max Wong
  • 694
  • 10
  • 18
  • 3
    What does "with no luck" mean? – Sam Varshavchik Oct 10 '22 at 14:59
  • 1
    That `add_compile_definitions` command should add the definition for all source files that are built. If you have problems it might be something else. So please [edit] your question to add more details about the problems you have. And if possible also please add a [mre] of the failing code to show us. – Some programmer dude Oct 10 '22 at 15:01
  • @Someprogrammerdude I just added the cmake text – Max Wong Oct 10 '22 at 15:14

1 Answers1

0

Try:

add_compile_definitions(-DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)

or

add_definitions(-DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
void
  • 338
  • 5
  • 19
  • Thanks for getting back to me. The cmake complains, `macro names must be identifiers` – Max Wong Oct 10 '22 at 15:40
  • However, `add_definitions` works find but still when I print `nanoseconds per unit`, I still get `1000` inside the library (when creating an object, in the constructor defined the library I print – Max Wong Oct 10 '22 at 15:44
  • Did you build both: boost and your own code after adding the above? – void Oct 10 '22 at 16:07
  • No, I installed `boost` by `sudo apt-get install libboost-all-dev` in the first place – Max Wong Oct 10 '22 at 17:18
  • So, if I need to recompile boost, how do I set `BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG` the first time I install it ( I dont need the microsecond-resolution version) – Max Wong Oct 10 '22 at 17:19
  • You would need to compile boost and and your own code as well. Refer documentation https://www.boost.org/doc/libs/1_80_0/more/getting_started/unix-variants.html – void Oct 11 '22 at 03:42