How-to specify the name of compiled output (*.exe) within C++ code?

Discussion in 'C++' started by Panarchy, May 9, 2009.

  1. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Hello

    How can I specify the name of the compiled output (the .exe) within my C++ code?

    I'm using Visual Studio 2008.

    Please tell me if this is possible, and if it is, how to do so.

    Thanks in advance,

    Panarchy
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Normally you would specify it in the project file. Why do you want to specify it in the code?

    What you could do if you want to set the executable name at runtime is to check argv[0], then if it's not correct you could copy that file to the filename you want, then launch that new executable, exit, and in the new executable check if this has just happened and delete the old executable.

    Anyway there doesn't seem to be anything here that suggests /OUT can be overridden by anything in the source:
    http://msdn.microsoft.com/en-us/library/8htcy933.aspx
     
  3. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Yes, I could very easily, manually, rename my .exe's to what I want them to be.

    However, since I am going to be recompiling this program, with slightly different content, over 250 times, I'd like to be able to create each *.exe in the fastest way possible.

    This means specifying everything in the code, then pressing the hotkey function for compiling.

    Nothing more should be done, for maximum speed.

    I can test 'em at some point as well... but for maximum speed, it'd be best to do it with the aforementioned method.

    Please tell me if argv can do this, and if it can, how I can make it do so.

    Thanks in advance,

    Panarchy

    EDIT: Those MSDN links seem alright, currently reading the documentation. Will tell you how it goes
     
    Last edited: May 10, 2009
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    It's a tough task, I think.

    Take a look at this : http://msdn.microsoft.com/en-us/library/6y6t9esh.aspx
    So, the linker uses the options and arguments defined in the "LINK" environment variable.

    So, I think this will work (not tested though):
    (1) Create a system environment variable called "LINK" with value "/OUT <default_filename>".
    (2) Inside your C++ code, change the LINK variable to "/OUT <default_filename_i>", where "i" is an index.
    (3) Compile, I think the LINK variable will take care of the rest :wink:

    Code:
    int main()
    {
           SetLINKVar(i);    // This func will change the LINK Env Var
           .
           .
           .
    }
     
  5. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    I was just thinking, I could have 1 command prompt window open, and 1 Visual Studio main.cpp (or equivalent) open.

    I could then make the changes to the code, Alt-Tab to the command-prompt window, then put in what I want the .exe to be via the /out parameter.

    Advantages;
    - No extra code needs to be added
    - So the size of my program won't enlarge

    :D

    Problem solved!

    Disadvantages;
    - Have to do an Alt-Tab :rolleyes:
    - Command-Prompt sucks, as I need to right-click, Paste :crazy:

    Though I still would like to know if this can be done programmatically, it is no longer a priority.

    If you know how I can get this to be done programmatically, please tell me.

    Thanks in advance,

    Panarchy

    Links:
    /OUT
    Output File
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sounds like a job for a script file. Just write a batch file, or a C program to generate a batch file, containing commands like
    Code:
    cl prog.c define=prog1 /out:prog1.exe
    cl prog.c define=prog2 /out:prog2.exe
    
    (nb: I'm not sure what the command line syntax for defines is) then in the code for prog1.exe you use a #ifdef prog1/#endif block etc.

    Or you could make it more complex, for each variation in code you can have
    Code:
    #ifdef variation1a
    // code
    #else if variation1b
    // code
    #endif
    
    then in the script file:
    Code:
    cl prog.c define=variation1a define=variation2a define=variation 3a /out:prog1.exe
    cl prog.c define=variation1a define=variation2b /out:prog2.exe
    
    etc. Basically you can make it as complicated as you want.
     
  7. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes I came across that but I didn't know if you were linking with the .Net library at all. A "String^" (note the circumflex) is a pointer to a managed String object so this is not applicable if you're not using .Net at all.
     
  9. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Ahaha... no wonder I'm confused. Well thanks anyways.

    If you, or anyone else thinks of a way I can get this to work, please don't hesitate to post your ideas.

    Thanks in advance,

    Panarchy
     
  10. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    I've worked it out, first I need to do a;

    cl /clr "test.cpp"

    Then

    Link "Test.obj" /out:"testing.exe"

    There is one problem with this though, that is it doesn't give me an icon.

    Doing it manually using the GUI, it does include an icon.

    So any ideas on how I can embed the icon via the CLI?

    Thanks,

    Panarchy
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Icons are resources so you need to add the compiled resource file (.res, created from .rc) containing the icon to the link line.
     
  12. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    I see.

    Pretty sure it's a .rc, could you please tell me how to include it? (resources.rc)

    Thanks in advance,

    Panarchy
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You need to compile it first with a resource compiler (look for rc.exe).

    Adding it to the link line is as simple as concatenating the filename to the line, for example suppose you want to add FOO.RES to LINK A.O, you get LINK A.O FOO.RES.
     
  14. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    LOL, that simple?

    I'll give it a go.

    Thanks
     
  15. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Thanks, however I couldn't work it out.

    Please tell me how do include an icon within the .exe, via the Command-Line.

    Thanks in advance,

    Panarchy
     
  16. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Do you have the icon in a .rc file?
     
  17. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
  18. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    So that needs compiling with the resource compiler to produce a .res file, then you can add the .res filename to the link line.
    The resource compiler used to be called rc.exe, but I can't find that under C:\Program Files\Microsoft Visual Studio 9.0. So MS must have renamed it to something else. I have an rc.exe in C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin but that seems like an odd place to keep it; anyway if you have that folder too then that might do the trick.
     
  19. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Nope, rc is still there.

    (all code tags are in order of how they are inputted)

    Code:
    [B]cl[/B] /clr "super.cpp"
    I've tried;
    Code:
    [B]rc[/B]    super-resource.rc
    And that outputs an .res.

    Then using the full command;
    Code:
    [B]link[/B]  "super.obj"  /ASSEMBLYRESOURCE:super-resource.res /out:"Not Super.exe"
    Compiles the program, with the same size as when I compile the .exe using the GUI, HOWEVER, no icons.

    Please tell me how to get this to work.

    Thanks in advance,

    Panarchy
     
    Last edited: May 14, 2009
  20. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Can you specify the icon in the shortcut properties (Change Icon button)?
    If so then it's just a case of checking the resource compiler syntax to find out how to specify which icon is the default.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice