提问人:sdbbs 提问时间:10/9/2023 更新时间:10/9/2023 访问量:30
让 CMake 在通过 Makefile 生成预处理的 C 文件时使用 -dD -dI 开关?
Making CMake use -dD -dI switches when generating preprocessed C files via Makefile?
问:
请考虑以下 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) shell,所以我使用“MSYS Makefiles”——但我想讨论也适用于“Unix Makefiles”;无论如何,我使用以下命令构建此项目:bash
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 自动生成用于预处理文件的 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
...其中 和 在预处理器选项(使用 GNU 编译器集合 (GCC)))中进行了描述-dD
-dI
无论如何,当我在我的示例中尝试这样做时:
$ 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
答:
1赞
KamilCuk
10/9/2023
#1
最简单的是:
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)
评论