Sign in to follow this  
nlbs

checking #ifdef from a different Compilation Unit

Recommended Posts

I've a macro say MY_MACRO which is defined in main.cpp is it possible to access this macro using #ifdef and friends from a different Compilatin unit that is included through Its header File. I dont see it as possible. Is it Possible with some tricks ??

Share this post


Link to post
Share on other sites
You can usually pass preprocessor macros to the compiler directly. Many libraries use this, for example they may have a USE_THREADING macro, or USE_FROB_LIB to use the frob library for any frobnication that is necessary.

Share this post


Link to post
Share on other sites
But What I've tried is
I defined a macro MY_MACRO in main.cpp
and tried to access it from libxyz.cpp
which failed cause that macro is not included by libxyz.cpp

Now as the macros will be used for configuration directive I've another plan which is more structured.

say I've made a Class named Config{.....} and There I'll put static member variable and their getter and setter there will be its forwartd declearation on both main.cpp and the libxyz.cpp and I can do Config::x = 2 before main() in the main.cpp and access Config::x from the libxyz.cpp

so how is the Plan ??
is it good or Impactrical or non standered or faulty ??

Share this post


Link to post
Share on other sites
session.h
class Session{
private:
string sessPath;
public:
Session();
........



session.cpp
//Calling SessionConfig::getSessPath() which is the static method of static Config Class
Session::Session():sessPath(SessionConfig::getSessPath()){
//Its possible to set a default value by SessionConfig::getSessPath().
//and gain if it gets called for second time the.
//previous value get overridden with the new one.
}
.........



main.cpp
....
class SessionConfig;

int main(int argc, char *argv[]){
SessionConfig::setSessPath("/tmp/cgi++/session");
Session session;
......
}



all forward declearation is done acordingly.

SessionConfig is the Config Class that holds static private variables along with there static getter and setters.

I've tested this way and it not only works rathar it ROCKS

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this