在源文件中使用宏生成二进制文件

Generate binaries using macros inside a source file

我正在尝试通过在源文件中使用宏来生成输出文件。 不管宏名是什么,使用宏名生成最终的.exe文件。

#include <iostream>

#define Apple
//#define Banana
//#define Mango

int main()
{
...
}

如何生成像 Apple.exe 这样的输出文件名?

编译器:g++ OS: windows

您无法从源代码中控制最终链接器工件(在您的情况下可执行)的名称。
这需要使用 -o <filename> 链接器标志来完成,因此在您的情况下

> g++ -o Banana.exe main.cpp -DNAME=Banana

为了更轻松地控制它,您可以将它们定义为 makefile 中的变量,例如喜欢

# Comment the current, and uncomment a different definiton to change the executables
# name and the macro definition for NAME
FINAL_NAME = Banana
# FINAL_NAME = Apple
# FINAL_NAME = Mango

$(FINAL_NAME).exe : main.cpp
        g++ -o $(FINAL_NAME).exe main.cpp -DNAME=$(FINAL_NAME)