gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > include if exists?

#78868 - gauauu - Mon Apr 10, 2006 8:47 am

Is there any sort of extension or trick you can do with gcc's preprocessor to have some sort of #include that includes the file if it exists, and just ignores the line if the file doesn't exist?

If not, I guess I can write my own custom pre-preprocessor to do it.....

#78880 - gauauu - Mon Apr 10, 2006 10:47 am

I guess one way to do it would be to go ahead and unconditionally include the file, but have a makefile dependancy and target that will create an empty text file if none exists....

(so it ends up using more than just the C preprocessor, but is faster and cleaner than trying to make a script to pre-preprocess my files)

#78884 - sgeos - Mon Apr 10, 2006 11:51 am

Code:
#define INCLUDE 1

#if INCLUDE
        #include <stdio.h>
#endif

int main(void)
{
        #if INCLUDE
                printf("Hello World!\n");
        #endif
        return 0;
}

You probably want to use #if instead of #ifdef.

Preprocessor constants can be supplied from the command line. You might want to have a DEBUG constant that changes program behavior.

-Brendan

#78908 - gauauu - Mon Apr 10, 2006 3:24 pm

Thanks, although actually, doing the makefile trick of creating the file blank if it doesn't exist is closer to what I need.

In case anyone is curious, basically, I have a config file that has all sorts of basic stuff, including which room is the start room for my game. Although for testing, I need to change the start room a lot. Which gets annoying, because CVS keeps seeing that the config file is modified, but I NEVER want to check these temporary config changes in. So I thought it'd be a lot easier if I could have an extra file that I could put temporary config changes in that wouldn't be in CVS.

Only problem with that is that I want my project to be able to build with just a single cvs checkout. So this works perfectly....if I check out and build, the dummy temp config file is created. I can change that to my heart's content, and never have to mess with rolling it back or checking it in.

#78919 - waruwaru - Mon Apr 10, 2006 4:35 pm

How about just #define the room number, and pass the #define in in your makefile/build-script? Then when you need to start in a different room just change your script or compile with a different parameter? Maybe even pass in the name of your config file, so you can just invoke the build with different config files?
_________________
DSLua - a scripting language for your DS!

#78921 - KayH - Mon Apr 10, 2006 4:35 pm

You can use an ".cvsignore" file (setting) in your folder that do the trick ;-)

#79004 - gauauu - Tue Apr 11, 2006 2:07 am

waruwaru wrote:
How about just #define the room number, and pass the #define in in your makefile/build-script? Then when you need to start in a different room just change your script or compile with a different parameter? Maybe even pass in the name of your config file, so you can just invoke the build with different config files?


The makefile and build scripts are also stored in cvs. I don't my frequently changed configuration stuff to be in a file controlled by cvs.

Quote:
You can use an ".cvsignore" file (setting) in your folder that do the trick ;-)

Now that I have a separate configuration file, that's what I'm doing. The original problem was figuring out a way to include that ignored file, but not having things break if I did a fresh checkout and it didn't exist.