# C++. Being able to see if a linked file exists and then do something about it.

This topic is 3527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm wondering if this is possible: I have a tool that uses certain files. I want to know if that file exists, and if not, do not use it's functions. Example:
/*main.h*/
#include <foo.h>
#include <faa.h>

bool can_use_foo = false;
bool can_use_faa = false;

#ifdef _FOO_INCLUDED_
can_use_foo = true;
#endif

#ifdef _FAA_INCLUDED_
can_use_faa = true;
#endif

void useFooDoSomething( void )
{

if( can_use_foo )
{

fooDoSomething();

}
else
{

}

}

{

if( can_use_faa )
{

}
else
{

}

}

/*main.cpp*/
#include "main.h"

int main()
{

useFooDoSomething();
return 0;

}


So if the linked file wasn't found, instead of giving tons of errors with the compiler, just ignore those foo depended functions. Is this possible? Thanks for your help! ~PCN

##### Share on other sites
Yes, with use of the preprocessor -- #define a preprocessor symbol in the 'optional' file, wrap access to that file's functions with #ifdef calls. You'll have to use a stub file when the optional one doesn't exist, otherwise the #include directive will fail.

Macros can alleviate the pain of this somewhat, but it's still to be a disgusting maintainability problem. In other words, this is an extremely bad idea. Why do you think you need this? It's going to make your program brittle and your code ugly.

##### Share on other sites
I thought it might be better than making your compiler spit out over 200 errors and the program merely say: "Your file wasn't included" so you can fix it that way. Also, my tool uses more than one of these files, so if one fails ( or if you don't have it ) the program will still run with the exception of that/those particular functions not working.

So would you say it would be a bad idea?