Search code examples
c++g++window

Generate binaries using macros inside a source file


I am trying to generate output file by the use of macro in source file. Whatever a macro name is, generate the final .exe file by using the macro name.

#include <iostream>

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

int main()
{
...
}

How could i generate an output file name like Apple.exe ?

compiler: g++ OS: windows


Solution

  • You cannot steer the name of the final linker artifacts (executable in your case) from within the source code.
    This needs to be done using the -o <filename> linker flag, thus in your case

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

    To control this more easily you can define these as variables in a makefile, e.g. like

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