Search code examples
c++eclipsecmakerapidjson

Add a definition in project properties (eclipse) with cmake


I'm using rapidjson and I want to use std::string's with it. Then I have to define this -DRAPIDJSON_HAS_STDSTRING.

My CMakeLists.txt have now:

add_definitions(-DRAPIDJSON_HAS_STDSTRING)

And it builds ok.

The problem is that in Eclipse CDT I want the indexer to recognize that define. Then it will not mark some functions as errors.

How can I do it with CMakeLists.txt ? I tried this without luck:

set(CMAKE_CXX_COMPILER_ARG1 "-std=c++11 -DRAPIDJSON_HAS_STDSTRING=1" CACHE STRING "Compiler Args" FORCE)

Solution

  • Since RapidJSON is a header-only library, You can define the macros you need, just before including rapidjson headers.

    If you do not have this kind of global configuration header in your project, you can create a local header like myrapidjson.h:

    #pragma once
    #define RAPIDJSON_HAS_STDSTRING 1
    #include "rapidjson/rapidjson.h"
    // other headers if you always need them
    

    And then when your header/implementation file needs rapidjson, just include this header first.