简体   繁体   中英

How can I check #ifdef in a DLL project, when the #define is in the executable project using the dll?

I have a C++ Visual Studio 2015 project consisting of 1 Windows API executable, 1 windows console executable, and 1 dll shared by both executables.

I have a #define USEWINDOWS in the Windows API main.cpp file

I don't have this defined in the console app

In the DLL, I would like to do an #ifdef USEWINDOWS statement, but the scope of the #define seems to be only valid to the Win32 executable, not in the dll.

How can I extend this #define to the DLL without having it affect the undefined USEWINDOWS in the console app?

Thanks

The DLL is shared by both executables, hence there is only one implementation available and therefore you can't use implementation specific compilation flags.

You'll need a runtime flag, ie some API parameter which tells the DLL code that it is OK to use "windows stuff". Eg

api.h

void someAPI(...., bool useWindows);

dll.c

#include "api.h"

void someAPI(...., bool useWindows) {
  ...
  if (useWindows) {
     // do "windows stuff" here
  }
  ...
}

app.c

#include "api.h"
...
someAPI(...., true);

console.c

#include "api.h"
...
someAPI(...., false);

(not tested by compilation, but you should get the drift...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM