fredley
fredley

Reputation: 33921

Can I use #ifdef in a .def?

Can I use #ifdef sections in a .def file for a dll? E.g.:

LIBRARY "mydll"
EXPORTS
  checkRequirements
  createDevice
  installDriver
  isPastVersionInstalled
  removeDevice
  #ifdef myVar
  doSomethingElse
  #endif

Upvotes: 4

Views: 2238

Answers (3)

Jan Ringoš
Jan Ringoš

Reputation: 141

I recently needed to solve exactly the same issue. I needed only small differences in otherwise large .def file; differences that change per architecture and between release/debug build. I found the regular C preprocessor unsuitable (and unusable without modifications) for this purpose.

So I ended up writing my own, much simpler, preprocessor for the .def files: github.com/tringi/defprep

Usage example is in the middle of the page.

The syntax is simple: Rows annotated with #ABC or #ABC=def at the end of the line will make it into output only if there's ABC, or ABC=def respectively, on the command line. I usually pass just a couple of Visual Studio macros like $(ProcessorArchitecture) and $(Configuration).

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942050

No, not possible, it is not a file that's pre-processed. Although I supposed you could by running the preprocessor to generate another .def file and link that one.

The more effective approach is to eliminate the need for a .def file completely. Use __declspec(dllexport) in your code on the functions that should be exported. Then any #ifdef in that code will automatically ensure that a function is neither compiled nor exported.

Upvotes: 3

ams
ams

Reputation: 25599

I don't know if .def files can have that exactly, but you can certainly create a file named (for example) mydll.def.in and then use the compiler preprocessor to generate the .def file at build time.

On Linux that would be done like this:

cpp -DmyVar=1 mydll.def.in > mydll.def

I imagine you're doing this on Windows, and I can't help you with that, but it'll be similar.

The only gotcha is that, depending on your compiler, you might need the rest of the file to consist of valid C tokens, and you mustn't have any symbols in the file that can be confused with macros. Gcc has an option --traditional-cpp to get around this sort of trouble, and your compiler might have the same sort of thing.

Upvotes: 1

Related Questions