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

C++ 在源文件中使用宏生成二进制文件,c++,g++,window,C++,G++,Window,我正在尝试使用源文件中的宏生成输出文件。 无论宏名是什么,都要使用宏名生成最终的.exe文件 #include <iostream> #define Apple //#define Banana //#define Mango int main() { ... } #包括 #定义苹果 //#定义香蕉 //#定义芒果 int main() { ... } 如何生成像Apple.exe这样的输出文件名 编译程序:g++ OS:windows您无法从源代码中控制最终链接器工件(在您

我正在尝试使用源文件中的宏生成输出文件。 无论宏名是什么,都要使用宏名生成最终的.exe文件

#include <iostream>

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

int main()
{
...
}
#包括
#定义苹果
//#定义香蕉
//#定义芒果
int main()
{
...
}
如何生成像Apple.exe这样的输出文件名

编译程序:g++
OS:windows

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

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

你不能。输出名称由链接器调用确定。你需要它做什么?我在main.cpp文件中有几个GUI,它将使用宏名构建。我希望每个宏都有一个单独的构建,并反过来有一个单独的.exe。这不只是在构建相应的东西时设置宏名称的问题吗?例如
g++-o Banana main.cpp-DNAME=Banana
g++-o Apple main.cpp-DNAME=Apple
嗯,我想这是对的