/*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
{
cout << "Foo was not found. Cannot use Foo's fooDoSomething() function.\n";
}
}
void useFaaDoSomething( void )
{
if( can_use_faa )
{
faaDoSomethingDifferent();
}
else
{
cout << "Faa was not found. Cannot use Faa's faaDoSomethingDifferent() function.\n";
}
}
/*main.cpp*/
#include "main.h"
int main()
{
useFooDoSomething();
useFaaDoSomething();
return 0;
}
C++. Being able to see if a linked file exists and then do something about it.
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:
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
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.
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.
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?
So would you say it would be a bad idea?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement