Use variable from CMAKE in C++

后端 未结 3 1071
感动是毒
感动是毒 2021-01-27 03:16

I want to use a value declared in my CMakeLists.txt in my C++ code. I\'ve tried to do like that :

ADD_DEFINITIONS( -D_MYVAR=1 )

and

<         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 03:59

    Thanks to πάντα ῥεῖ

    His solution works for my first question, and I've could do that :

    CMakeLists.txt:

    ADD_DEFINITIONS( -D_VAR=\"myValue\" )
    

    main.cpp:

    #include 
    
    #ifdef _VAR
        #define TXT _VAR
    #else
        #define TXT "nobody"
    #endif
    
    int main(){
        std::cout << "hello " << TXT << " !" << std::endl;
        return 0;
    }
    

提交回复
热议问题