当我调用 CMake 时如何定义变量,以便 qtcreator 知道它已定义?

发布于 2024-12-04 07:40:44 字数 610 浏览 2 评论 0原文

我有一段代码根据 #define 有条件地激活,如下所示:

#ifdef VARIABLE
  code.function();
#endif

cmake 脚本有一个“选项”命令,它设置如下所示的变量:

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")

我使用 cmake 来构建项目,并使用 qtcreator 作为 IDE。我的问题是 qtcreator 认为 VARIABLE 没有定义,所以我的代码没有突出显示,但是当我在控制台上构建它时,VARIABLE 被定义了。那么,我应该向 qtcreator 传递哪些参数来运行 cmake,以便它知道 VARIABLE 已定义并突出显示我的代码?有办法做到这一点吗?

Ps:我只是使用 qtcreator 来编辑文件,构建部分是通过控制台命令完成的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宫墨修音 2024-12-11 07:40:44

另一种选择是使用配置的头文件,并仅在需要定义的地方包含它:

# 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})

// 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

最后,

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

不定期使用 QtCreator,所以我不知道这种技术在语法突出显示方面是否有效,但我我认为会这样,因为他们必须读取头文件才能正确完成它......

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...

深居我梦 2024-12-11 07:40:44

看一下 add_definitions() 。它向编译器添加了一个预处理器定义,并且可以像任何普通的#define 一样使用:

add_definitions( -DVARIABLE )

但是...我不确定这是否会让 qtcreator 将其作为“已知变量”。

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

add_definitions( -DVARIABLE )

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

顾铮苏瑾 2024-12-11 07:40:44

您还可以使用project_name.config 文件。它专门用于满足您的需要。您可以添加仅由 QtCreator 解释的定义以突出显示和自动完成。

该文件通常位于项目的根文件夹中。

You can also use the project_name.config file. It is specifically used for your need. You can add defines which are only interpreted by QtCreator for highlighting and auto-completion.

This file is normally in the root folder of your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文