0

Is it possible to define this as a preprocessor definition? I'm trying to do this:

#if defined _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

so that I can build this library for both Mac and Windows. There's a function which needs a __declspec(dllexport) when it's on Windows, but I don't need it when I'm building on Mac. So this is what I tried doing.

As I suspected, it gives me an error when building because it doesn't like the parenthesis, yet I've seen other people do it. See here or here. Is there something I'm doing wrong or another way to achieve what I'm trying to do?

Edit: here's the error I get when trying to build on Windows (haven't tried on Mac yet).

 error: expected constructor, destructor, or type conversion before '(' token
   38 | #define DLL_EXPORT __declspec(dllexport)
      |                          ^
note: in expansion of macro 'DLL_EXPORT'
   94 | extern "C" DLL_EXPORT int process(const float* inputData, float* outputData)
      |            ^~~~~~

And I use mingw gcc to compile.

  • 1
    *"it gives me an error"*. where? on Mac? on Windows? – Jarod42 Apr 22 '21 at 13:22
  • do you use visual studio, gcc or clang – ma1169 Apr 22 '21 at 13:25
  • Depending in the problem (which is currently unclear), maybe this is the solution: [https://stackoverflow.com/questions/28166565/detect-gcc-as-opposed-to-msvc-clang-with-macro](https://stackoverflow.com/questions/28166565/detect-gcc-as-opposed-to-msvc-clang-with-macro) – drescherjm Apr 22 '21 at 13:27
  • I've added the error in the post, and I'm cross compiling from ubuntu to windows using mingw-w64 gcc. – Luis Angel Urena Lopez Apr 22 '21 at 13:32
  • So your real problem is using `__declspec(dllexport)` with `mingw` – drescherjm Apr 22 '21 at 13:33
  • This may be a solution although it says mingw supports this: https://stackoverflow.com/questions/22285240/mingw-use-declspecdllexport-or-attribute-visibilitydefault – drescherjm Apr 22 '21 at 13:34
  • @drescherjm That's not the problem since we've always had __declspec(dllexport). It's just recently that we're doing a build for Mac, so we're trying to remove it for when we're building with Mac. Or maybe you're telling me that it was never the right way to do it using mingw even if it works...? – Luis Angel Urena Lopez Apr 22 '21 at 13:36
  • @drescherjm I'm not sure about that last link, he's doing something similar to me but it's not working on my side. – Luis Angel Urena Lopez Apr 22 '21 at 13:43
  • You probably need to back away from your current project and create a small minimal example code that reproduces the issue. For example does just adding `#define DLL_EXPORT __declspec(dllexport)` to the top of a "Hello World" program give you the same error when compiled using `mingw`. If you find something this may help your question get solved. – drescherjm Apr 22 '21 at 13:45
  • Seems like just adding the definition is fine, it builds correctly. The problem is with my usage of it then. I think it's unclear how I'm using so here: `DLL_EXPORT int myFunction(const float* input)`. Just in case I also tried with `DLL_EXPORT(dllexport) int myFunction(const float* input)`, but it gives me the same error. – Luis Angel Urena Lopez Apr 22 '21 at 13:56
  • The error messages refer to a macro named `EXTERN` which you have not described. Odds are, you're trying to be a bit more (ummm) clever with macros than you have shown in your question. And some of that cleverness that you haven't shown contributes to the effects you are seeing. Rather than providing only code that you think is relevant and forcing others to guess about code you haven't shown, try providing a [mcve]. – Peter Apr 22 '21 at 14:00
  • That was my fault when adding the error output, originally it was called `EXTERN` but changed it to `DLL_EXPORT` to make the question easier to understand, sorry about that! I just fixed it. – Luis Angel Urena Lopez Apr 22 '21 at 14:06
  • `__declspec(dllexport)` is a msvc stuff, not a win32 stuff. You shouldn't have `__declspec(dllexport)` for gcc on windows. – Jarod42 Apr 22 '21 at 14:08
  • @Jarod42 this doesn't work since we're using this .dll with an app we build using msvc later on. If I remove `__declspec(dllexport)` I get an error about there being no entry point to the function I need to put `__declspec(dllexport)` when I try to run the application that depends on this library. – Luis Angel Urena Lopez Apr 22 '21 at 17:36
  • You need `__declspec(dllimport)` msvc side when compiling with. (gcc export all by default). – Jarod42 Apr 22 '21 at 17:57
  • Are you trying to export class member functions? – ssbssa Apr 23 '21 at 12:02

0 Answers0