Thread: Compiling same file twice with different #include's with MSBUILD?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Compiling same file twice with different #include's with MSBUILD?

    In the code below, constantset_a.h and constantset_b.h define the same set of global constants but sets the constants with different values. Compiled myprogram.exe is to behave differently depending on the global constant values they were compiled with.

    My question is, is there any way that I can use MSBUILD to set CONSTANTSET_A and build, and then set CONSTANTSET_B and then build again, and for the compiled binaries to have different names? I need this to be done in a single compile pass (i.e. compiling two different binaries with one build /c /z command), since my codes will be compiled along with other people's codes in the team-wide automated build process.

    So probably in the "sources" build configuration file, I could do
    Code:
    ...
    TARGETNAME=MyProgramA;MyProgramB
    C_DEFINES=-DCONSTANTSET_A -DCONSTANTSET_B
    ...
    ? I know this is wrong, but I just put it there so that you would have a better idea as to what I want to achieve.

    Code:
    // myprogram.h
    #if defined CONSTANTSET_A
    #include <constantset_a.h>
    #elif defined CONSTANTSET_B
    #include <constantset_b.h>
    #endif
    ...
    Last edited by chiefmonkey; 06-01-2009 at 12:13 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The exact changes depend on how your makefile is written, but basically you need to have a separate make target for each of the binairies, and a common build target to build both.

    Something like (except using tabs):
    Code:
    targetA: constantset_a.h source.c otherheader.h
        g++ -DCONSTANTSET_A -o targetA 
    
    targetB: constantset_b.h source.c otherheader.h
        g++ -DCONSTANTSET_B -o targetB
    
    all: targetB targetA
    And your myprogram.h is correct.

    EDIT: Just realized you are using MSBUILD not make. The principle is the same, but the syntax will be different. You still need 2 build targets, plus enable the default build target to build them both.
    Last edited by King Mir; 06-02-2009 at 12:35 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM