如何使用 Dev-C++ IDE 保存预处理器输出?

How do I save preprocessor output using the Dev-C++ IDE?

我希望能够查看预处理器输出以确保我的预处理器指令 运行 正确。 Dev-C++ 在 Tools > Compiler Options... > General 中有一个选项可以在调用编译器时添加命令,我添加了命令 -E C:\Personal\preprocessed.cpp。我收到一个编译器错误,指出该文件不存在,但在这种情况下,编译器不应该只创建该文件吗?我创建了文件,但现在出现此错误:cannot specify -o with -c, -S or -E with multiple files.

为什么我使用 Dev-C++ 而不是 Visual Studio?由于我还在学习,我希望能够只测试几行代码而不必创建 an entire new project.

是的,我看过 this question 但没有给出充分的答案。请不要将其标记为重复。

在此先感谢您的帮助!

I've added the command -E C:\Personal\preprocessed.cpp. I got a compiler error saying the file didn't exist, but shouldn't the compiler just create the file in that case?

不,因为 -E option 不接受参数、文件名或其他。它只是指示 编译器只做预处理。预处理代码写入标准输出。因此:

因此:

g++ -E C:\Personal\preprocessed.cpp foo.cpp

告诉编译器你想要 运行 g++ -E 和一对输入文件 C:\Personal\preprocessed.cppfoo.cpp, 正如您所发现的那样,这是不允许的。

你想做的简单事情对于你的 IDE 选择来说非常困难。假设 您要预处理的源文件是 C:\Personal\foo.cppg++ 在您的 PATH 中, 只需在 C:\Personal 和 运行:

中打开命令 window
g++ -E foo.cpp > foo.ii

我建议使用输出文件 foo.ii - 尽管您可以随意调用它 - 因为 g++ 将扩展名 .ii 识别为表示已经过预处理的 C++ 源代码。你可以 运行:

g++ -Wall -o prog foo.ii

foo.ii将被编译并链接为程序prog,无需再次预处理。