how to redefine preprocessor directives of c++ libraries in an c++ application program -
i give following example illustrate question. suppose creating c++ library, , head file , implementation file follows:
// head.h class __declspec(dllexport) myclass { public: int obtain_value(); }; // head.cpp int myclass::obtain_value() { #ifndef define_five return 6; #else return 5; #endif }
in library, if define define_five
when compiling library, obtain 5 when invoking obtain_value
function; otherwise, obtain 6. let's assume when compiled library, did not define define_five
. in c++ program call library, when voking obtain_value
function, expect have 6. question is, can in c++ program invokes library output of calling function 5. thanks.
the preprocessor directives evaluated @ compile time, cannot change code in runtime. if compile library , define_five not defined, preprocessor in first pass replace code following:
// head.cpp int myclass::obtain_value() { return 6; }
as can see, parse , remove preprocessor directives. then, real compiling stage starts machine code generation.
if want dynamic values in library, have change interface, example:
// head.h class __declspec(dllexport) myclass { public: int obtain_value(bool give_me_five=false); }; // head.cpp int myclass::obtain_value(bool give_me_five) { if (!give_me_five) return 6; else return 5; }
Comments
Post a Comment