Thread: Change build directory with #ifdef

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    98

    Change build directory with #ifdef

    Is it possible to add a special pragma or something to tell the compiler which folder to build to based on #ifdef #else
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No, but you can usually do it on the command line.

    Eg
    gcc src/prog.c -o obj/prog.o
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    Oh well. I thought that would be the case.
    Thanks for replying.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    So, I can build to different folders now.
    And it is "relatively" simple to do.
    (ignoring all the trial-and-errors and endless Google searches)

    I copy-n-pasted my project vcxproj file to a new folder
    (current project is ProVersion, new is TrialVersion)

    I edited the new Trial vcxproj file <ItemGroup> <ClCompile Include> paths, etc.
    to use the original ProVesrion source code (a.k.a. link).

    I opened my Pro project (in VS2019) and clicked: Tools > Create GUID
    which I then pasted into the Trial vcxproj file <ProjectGuid>

    I also opened the Pro project Properties > Configuration > C/C++ > Preprocessor
    and clicked Preprocessor Definitions <edit> and added PROVERSION

    Close the Pro and then double-click the Trial vcxproj file
    which opens in VS2019 and is ready to compile since
    my source code is already full of #ifdef PROVERSION #else

    Anyway, just thought I'd share.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if there were ever a good example of an XY problem, then this is it.

    1. Create new build targets in your existing project.
    How to: Create and edit configurations - Visual Studio (Windows) | Microsoft Learn

    In this example, I cloned DebugPro from Debug, and ReleasePro from Release.

    Change build directory with #ifdef-screenshot-2022-10-01-08-57-09-png

    You can then edit the C++ preprocessor options of the two 'Pro' versions to include the PROVERSION macro definition.

    2. To build every permutation of things, just use Build->Batch build...

    Change build directory with #ifdef-screenshot-2022-10-01-09-05-54-png

    Moments later, I have 8 executable files in 8 separate directories configured how I want.

    No need to copy/edit .vcproj files, something which is both fragile and likely to break - especially at upgrade time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    Wow, you're right.
    I bet you see the XY problem once a week here.
    I would even say that is one of the biggest nemeses of programmers.
    The other being cut-n-paste without completely editing the paste.

    As for your solution...
    I appreciate the education. I've been telling myself for years
    that I should learn more about all of the features of VS.
    Oh well. I'm going to leave it alone though.
    It's already check-marked.

    BTW, have you ever run across the Hacker Laws?
    GitHub - dwmkerr/hacker-laws: 💻📖 Laws, Theories, Principles and Patterns that developers will find useful. #hackerlaws
    (there's an easier to read pdf there)

    Thanks again.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    Oh, and one last nemesis related to #ifdef and #endif
    Code:
    #ifdef PROVERSION
    x=123;														\
    #endif
    Try banging your head on the wall fixing that one.
    (shh... don't tell anyone: there tabs and a backslash)

    Have a good day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream directory change
    By george7378 in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2013, 10:19 PM
  2. fopen - change directory with a variable
    By predator887 in forum C Programming
    Replies: 2
    Last Post: 05-03-2011, 05:41 AM
  3. Poll directory to detect a change?
    By brooksbp in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 12:39 AM
  4. change active directory
    By gandalf_bar in forum Linux Programming
    Replies: 3
    Last Post: 03-31-2005, 01:35 AM
  5. Change Directory as an Internal Command!
    By slevytam in forum C Programming
    Replies: 4
    Last Post: 01-31-2005, 05:36 PM

Tags for this Thread