Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a section of code that is conditionally activated depending on a #define, like this:

#ifdef VARIABLE
  code.function();
#endif

The cmake script has an 'options' command that sets the VARIABLE like this:

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'm using cmake to build the project and qtcreator as IDE. My problem is that qtcreator thinks that VARIABLE is not defined so my code has is not highlighted, but when I build it on a console, VARIABLE is defined. So, what parameters should I pass to qtcreator to run cmake, so that it knows VARIABLE is defined and highlights my code? Is there a way to do this?

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

like image 957
Salsa Avatar asked Sep 13 '11 20:09

Salsa


People also ask

How do you use CMake Qtcreator?

Qt Creator uses the default CMake if it does not have enough information to choose the CMake to use. To set the selected CMake executable as the default, select Make Default. To remove the selected CMake executable from the list, select Remove. Select the Kits tab to add the CMake tool to a build and run kit.

Where is Qt6 config CMake?

This time, I found a Qt6Config. cmake there (inside C:\Qt\6.3. 1\msvc2019_64\lib\cmake\Qt6).

What is difference between Qmake and CMake?

Both are build systems, but they're not very similar at all. If your project uses Qt, you're probably best off using qmake. CMake is more generic, and fits pretty much any type of project. Both qmake and CMake generate a Makefile, which is read by make to build the project.

Does Qt come with CMake?

Introduction. CMake can find and use Qt 4 and Qt 5 libraries. The Qt 4 libraries are found by the FindQt4 find-module shipped with CMake, whereas the Qt 5 libraries are found using "Config-file Packages" shipped with Qt 5.


1 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 do not use QtCreator regularly, so I don't know if this technique works in terms of their syntax highlighting, but I'd assume it would, since they must read header files in order to do a proper job of it...

like image 101
DLRdave Avatar answered Sep 22 '22 18:09

DLRdave