字符串化 CMake 预处理器令牌

Stringify CMake preprocessor token

我正在使用 CMake 编译这个示例程序:

CMakeLists.txt:

cmake_minimum_required (VERSION 3.0.0)
set(PROJECT_NAME Main)
project(${PROJECT_NAME})

add_definitions(-DSTRING=“test”)

add_executable(${PROJECT_NAME} main.c)

main.c:

#include <stdio.h>

int main(void)
{
    puts(STRING);
}

编译出现以下错误:

error: expected expression
    puts(STRING);
         ^
<command line>:1:16: note: expanded from here
#define STRING "test"

如何在不修改的情况下将此预处理器标记字符串化main.c

您使用的字符 在 C:

中无效
error: expected expression
        puts(STRING);
         ^
<command line>:1:16: note: expanded from here
#define STRING “test”
           ^
1 error generated.

您必须在 CMakeLists.txt 中为 " 更改 并且您的程序将编译。