c++ - How to control #define directive in different projects ...? -


my question quite straight forward. intended know #define directive in c++ controllable on different project files? elaborately, have header file , cpp file 1 project. codes of files follows:

myheader.h

#ifndef __my_header_h__  #include <cstring> using namespace std;  #ifdef _header_export_ #define header_api __declspec(dllexport) #else #define header_api __declspec(dllimport) #endif  #ifdef __cplusplus extern "c" { #endif  class header_api myheader { public:     myheader();     ~myheader(); #ifdef _header_display_     void __cdecl parseheader(); #elif defined (_header_return_)     string __cdecl parseheader(); #endif };  #ifdef __cplusplus } #endif  #define __my_header_h__ #endif 

myheader.cpp

#ifndef __my_header_h__ #include "myheader.h" #endif  myheader::myheader() { } myheader::~myheader() { }  #ifdef __cplusplus extern "c" { #endif  #ifdef _header_display_     header_api void __cdecl myheader::parseheader()     {         fputs(string("displaying...").c_str(), stdout);     } #elif defined (_header_return_)     header_api string __cdecl myheader::parseheader()     {         string retval("returning...");         return retval;     } #endif  #ifdef __cplusplus } #endif 

in project headerimpl.cpp file has been implemented following code.

headerimpl.cpp

#include "stdafx.h"  #define _header_display_ // display message  // #define _header_return_ // return message string #include "myheader.h"  int main(int argc, char* argv[]) {     myheader header;     myheader.parseheader(); // display message or return string     return 0; } 

now, wanted know how can use #define directive in headerimpl.cpp file control parseheader method myheader.cpp file? has been noted myheader.h file doing need for; i.e. controlling parseheader method upon declaring #define directive, accordingly.

you can't. each c++ source file compiled independently, , settings in 1 cannot affect another. you'll have on project level.

one way set different project (and solution) configurations different values of macro. instead of usual debug , release, add debug-display, debug-return etc. can define macros in project settings each configuration. make sure link correctly built version of library.


as side note, you're using illegal names in code. name contains double underscores, or starts underscore followed uppercase letter, reserved compiler & standard library. user code not allowed use such names own purposes.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -