-2

I'm fairly new to cmake and I'm trying to use the mongodb driver for C++ by using this tutorial: https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/. The tutorial says you need C++11, so I tried the recommended way which is using CXX_STANDARD property. But it didn't work. I finally gave up and used add_definitions() and it worked!

The cmake file shows both approaches I tried. ie, add_definitions and the set property approach in the comment (which I tried first but failed to work).

It works with add_definitions() now, but everywhere I turn people recommend using CXX_STANDARD or let cmake use C++11 automatically by requesting a C++11 feature.

So, will this be a problem later on for some reason? And why didn't it work the first time around?

My cmake file:

cmake_minimum_required(VERSION 3.2)

project(testproj CXX)

file(GLOB SRC src/*.cpp)

add_library(testproj SHARED ${SRC})

add_definitions(--std=c++11)
#set(TARGET testproj PROPERTY CXX_STANDARD 11)
#set(TARGET testproj PROPERTY CXX_STANDARD_REQUIRED ON)

# include directories
include_directories(/usr/local/include/mongocxx/v_noabi)
include_directories(/usr/local/include/libmongoc-1.0)
include_directories(/usr/local/include/bsoncxx/v_noabi)
include_directories(/usr/local/include/libbson-1.0)

# library path
link_directories(/usr/local/lib)
target_link_libraries(testproj mongocxx bsoncxx)
Raghuveer
  • 1,737
  • 20
  • 27

1 Answers1

-1

It would be better to define the c++11 flag to the whole project.

To do so, you can use the variable CMAKE_CXX_FLAGS to set it as a global flag, like this :

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

Since it´s a global call, I recommand you to put it just beneath the project line.

bl4ckb0ne
  • 1,097
  • 2
  • 15
  • 30