通过 Makefile 生成预处理的 C 文件时,让 CMake 使用 -dD -dI 开关?

问题描述 投票:0回答:1

考虑以下 CMake 示例:

cmake_minimum_required(VERSION 3.13)

if (NOT(EXISTS "${CMAKE_SOURCE_DIR}/main.c" AND NOT IS_DIRECTORY "${CMAKE_SOURCE_DIR}/main.c"))
  message("${Cyan}Creating ${CMAKE_SOURCE_DIR}/main.c${ColourReset}")
  # https://stackoverflow.com/questions/7637539/how-to-split-strings-across-multiple-lines-in-cmake
  # https://stackoverflow.com/questions/69574555/cmake-filegenerate-output-output-file-how-to-write-content-on-multiple
  # https://stackoverflow.com/questions/67674998/cmakelists-create-string-with-new-lines
  # https://stackoverflow.com/questions/26193171/cmake-how-to-create-a-file-with-make-command # example for file(write
  FILE(WRITE ${CMAKE_SOURCE_DIR}/main.c
    "#include <stdio.h>\n"
    "#define MYDEFINE 1\n"
    "\n"
    "int main() {\n"
    "  printf(\"Hello from mytest main .exe (MYDEFINE: %d)!\\n\", MYDEFINE);\n"
    "  return 0;\n"
    "}\n"
  )
endif() # NOT(EXISTS "${CMAKE_SOURCE_DIR}/main.c"

project(mytest C)
set(CMAKE_C_STANDARD 11)
set(${PROJECT_NAME}_sources
    main.c
)

add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_sources})

我在 Windows 10 的 MINGW64 (MSYS2)

bash
shell 中,所以我使用“MSYS Makefiles” - 但我想讨论也适用于“Unix Makefiles”;不管怎样,我用以下方法构建这个项目:

mkdir build
cd build/
cmake  ../ -DCMAKE_BUILD_TYPE=Debug -G "MSYS Makefiles"

然后我可以运行

make
,一切都会构建:

$ make
make[1]: Entering directory '/c/tmp/mytest/build'
make[2]: Entering directory '/c/tmp/mytest/build'
Scanning dependencies of target mytest
make[2]: Leaving directory '/c/tmp/mytest/build'
make[2]: Entering directory '/c/tmp/mytest/build'
[ 50%] Building C object CMakeFiles/mytest.dir/main.c.obj
[100%] Linking C executable mytest.exe
make[2]: Leaving directory '/c/tmp/mytest/build'
[100%] Built target mytest
make[1]: Leaving directory '/c/tmp/mytest/build'

$ ./mytest.exe
Hello from mytest main .exe (MYDEFINE: 1)!

仅在cmake中运行C预处理器?中提到的:

CMake 自动生成用于预处理文件的 make 目标。对于项目中的每个 foo.c 都有一个 foo.i make 目标,该目标将仅运行预处理器(带有所有相关的 -D 和 -I 标志等)。

嗯,事实上,我希望这些标志来自 如何知道(在 GCC 中)何时声明给定的宏/预处理器符号? :

# shows preprocessed source kept with macros and include directives 
g++ -E -dD -dI -P file.cc  

... 其中

-dD
-dI
预处理器选项(使用 GNU 编译器集合 (GCC))

中进行了描述

无论如何,当我在我的例子中尝试这个时:

$ make VERBOSE=1 main.c.i
make -f CMakeFiles/mytest.dir/build.make CMakeFiles/mytest.dir/main.c.i
make[1]: Entering directory '/c/tmp/mytest/build'
Preprocessing C source to CMakeFiles/mytest.dir/main.c.i
/C/msys64/mingw64/bin/gcc.exe   -g   -std=gnu11 -E /C/tmp/mytest/main.c > CMakeFiles/mytest.dir/main.c.i
make[1]: Leaving directory '/c/tmp/mytest/build'

...我只能看到

-g
并且使用了
-E

如何更改我的 CMakeLists.txt,以便当我发出命令

make main.c.i
来获取预处理输出时,运行的命令是
gcc -g -std=gnu11 -dD -dI -E ...
,也就是说,它还会包含
-dD -dI
选项?

c gcc cmake makefile c-preprocessor
1个回答
0
投票

最简单的是:

add_compile_commands(-dD -dI)

正确的方法可能是在

project()
调用之后添加:

set(CMAKE_C_CREATE_PREPROCESSED_SOURCE "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -dD -dI -E <SOURCE> > <PREPROCESSED_SOURCE>" CACHE "STRING" "" FORCE)
© www.soinside.com 2019 - 2024. All rights reserved.