Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringify CMake preprocessor token

I'm using CMake to compile this example program:

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

Compiling gives me the following error:

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

How can I stringify this preprocessor token without modifying main.c?

like image 584
syb0rg Avatar asked Jul 14 '26 07:07

syb0rg


1 Answers

You are using the characters and that are not valid in C:

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

You must change and for " in your CMakeLists.txt and your program will compile.

like image 67
chema989 Avatar answered Jul 18 '26 13:07

chema989