How to define a variable when I call CMake so qtcreator knows that it is defined?

I have a section of code that conditionally activates depending on #define, for example:

#ifdef VARIABLE code.function(); #endif 

The cmake script has the "options" command, which sets the VARIABLE value as follows:

 option(VARIABLE "Want to use VARIABLE?" ON) if(VARIABLE) message(STATUS "VARIABLE") set(VARIABLE_FLAG "-DVARIABLE") endif() set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VARIABLE_FLAG} -Wall") 

I use cmake to create a project and qtcreator as an IDE. My problem is that qtcreator believes that VARIABLE is not defined, so my code is not highlighted, but when I create it on the console, VARIABLE is detected. So, what parameters should qtcreator pass to run cmake so that it knows that VARIABLE is defined and highlights my code? Is there any way to do this?

Ps: I just use qtcreator to edit files, part of the build is done using console commands.

+6
source share
3 answers

Another alternative would be to use a configured header file and include it only where you need the definition:

 # in CMakeLists.txt configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/my_defs.h.in ${CMAKE_CURRENT_BINARY_DIR}/my_defs.h ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) 

and

 // in my_defs.h.in #cmakedefine VARIABLE // configure_file converts #cmakedefine of a named CMake variable // into a C++ #define of a C++ pre-processor symbol 

and finally

 // in various C++ source or header files, but only as needed: #include "my_defs.h" #ifdef VARIABLE doSome_VARIABLE_SpecificStuff(); #endif 

I don’t use QtCreator regularly, so I don’t know if this method works in terms of syntax highlighting, but I would suggest that this happens because they have to read the header files in order to do the job right ...

+5
source

Take a look at add_definitions () . It adds a preprocessor definition to the compiler and can be used like any regular #define :

 add_definitions( -DVARIABLE ) 

However ... I'm not sure if qtcreator will make it pick it up as a "known variable".

+3
source

You can also use the project_name.config file. It is specially used for your needs. You can add definitions that are only interpreted by QtCreator to highlight and autocomplete.

This file is usually located in the root folder of your project.

+1
source

Source: https://habr.com/ru/post/897266/


All Articles