I want to include a header file only if a certain function body is called?
Is this possible or recommended in C开发者_开发百科++?
No.
You've got it a bit wrong; #include
is not processed at run-time, at all. It's not possible to #include
a file based on a program's execution characteristics; once the program executes its source is fixed (since it's already compiled).
Possible, yes; recommended no, not usually.
#include
is process and an early stage of parsing so works in many places with no regard for the language context at the point of the include.
Note that the include will happen regardless of whether the function is called so it probably isn't going to solve the problem that you are trying to solve. The file included will be placed directly inside the function body so the include file would have to be designed to be included at such a point in the source file.
You're obviously biased by higher-level languages, such as Python, in which is possible to do things like:
if ( a ):
from whatever import whatever_I_need
else:
from whatever_else import whatever_I_need
However, in C++ this is evaluated at compile time (well, actually, #include is a preprocessor directive and it is even evaluated before compile time). Putting that inside a block of an if-else construction will only lead to compilation errors. Just take into account that #include is just a way to dump the contents of a file (normally a header (interface) file) into another one that needs its declarations. Very low level. It will be included anyway.
You can #include inside a function body, but this does not mean that the header file is only included when the function body is called.
The content of the header file will be included by the preprocessor at compile time, and thus the code will always be present in the function.
Obviously, the code is only executed when the function is called.
While you can put a #include
inside a function body, it is not what you want to do. There is no way to include a header file only if a certain function is called because the include happens before compile time, but the function call happens at runtime.
Okay, the question was a little off the track, but the actual matter is important.
If for a portion of your methods you actually need to depend on a library that most users won't care about, it's perfectly reasonable to be willing to spare them this unneeded dependency.
You can do this by manipulating preprocessor tokens.
/// myClass.h
/// In order to use BigDep, you need to:
/// - define `MYPROJECT_BIGDEP_USE` before included this header
/// - have `<bigdep-install-path>/include` in your include path
/// - link with `libbigdep.so`
class MyClass
{
public:
void foo() const;
#ifdef MYPROJECT_BIGDEP_USE
void bigDep() const;
#endif // MYPROJECT_BIGDEP_USE
};
/// myClass.cpp
#include "blah.h"
#include "bar.h"
#ifdef MYPROJECT_BIGDEP_USE
#include <bigdep.h>
#endif // MYPROJECT_BIGDEP_USE
void MyClass::foo() const { /**/ }
#ifdef MYPROJECT_BIGDEP_USE
void MyClass::bigdep() { /**/ }
#endif // MYPROJECT_BIGDEP_USE
This way, you have to compile mode, depending on whether the symbol MYPROJECT_BIGDEP_USE
is defined or not.
On gcc, you can define symbol on the command line using -D
like in -DMYPROJECT_BIGDEP_USE
. On Visual Studio it's with /D
like in /DMYPROJECT_BIGDEP_USE
.
Another option for you user is to wrap your header:
// myProjectMyClass.h
#define MYPROJECT_BIGDEP_USE
#include <myClass.h>
And only ever include "myProjectMyClass.h" and never your header directly.
The use of these preprocessor directives is quite common. For example, when installing the cx_Oracle
python module, the installer checked for the version of oracle I used from my environment variables and disabled a number of methods that were not available for it directly at compilation time.
In one program, that I need to use in my app (using Qt) there's:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
using namespace PL;
"#"include "res/przypisania_txt.h"
MainWindow w;
w.show();
...
and when I paste the include before main, it won't compile, so i gues You can use INCLUDE inside a function ;)
精彩评论