3

I want to build a dll which can be used by any c++ and c# application can use. I installed cLion on mac and started a new project as c++ library.

I wrote some lines of code in the header file. Since I want to export my functions, I used __fdeclspec

But it is throwing me the error '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes

My code is

#pragma once

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

extern "C" MATHLIBRARY_API void fibonacci_init(
        const unsigned long long a, const unsigned long long b);

extern "C" MATHLIBRARY_API bool fibonacci_next();

extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();

extern "C" MATHLIBRARY_API unsigned fibonacci_index();

How to fix this error so that I can build the solution?

Naanavanalla
  • 1,412
  • 2
  • 27
  • 52
  • Why are you trying to use Microsoft-specific stuff on a Mac? – Mat Dec 29 '19 at 11:16
  • __declspec is an MSVC extension. You don't need to specially declare exports to dylibs (macos dlls) when compiling with clang. – parktomatomi Dec 29 '19 at 11:16
  • I am trying to build a dll which can be used by windows application. Can't I use mac os for the development? – Naanavanalla Dec 29 '19 at 11:18
  • 2
    Use a `#ifdef _WIN32` ... `#endif` preprocessor directive to wrap the windows-specific stuff. You want to avoid that as much as possible for cross-platform development but in this case it's justified. Examples here: https://stackoverflow.com/questions/1235165/c-cross-platform-dynamic-libraries-linux-and-windows – parktomatomi Dec 29 '19 at 11:19
  • After using `#ifdef _WIN32` `Unknown type name 'MATHLIBRARY_API'` error is coming up. I am very new to c++. – Naanavanalla Dec 29 '19 at 11:22
  • See the link for an example. On non-windows platforms define `MATHLIBRARY_API` to be nothing so it still compiles. – parktomatomi Dec 29 '19 at 11:23
  • @parktomatomi The error went. On building it is giving `dylib` file instead of a `dll`. I am assuming, it is because of mac os. Can I use `dylib` similar to `dll`? – Naanavanalla Dec 29 '19 at 11:32
  • Yup! I'm not a Mac user though, so I can't tell you the specifics on how to link to it. – parktomatomi Dec 29 '19 at 12:02
  • Just adding a link here to [CMake's `GenerateExportHeader`](https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html) module. If you use CMake, you may find it helpful. – starball Aug 24 '22 at 17:45

1 Answers1

1

You can use the following preprocessor checks to make a cross-platform macro for the export semantics:

#if defined(_MSC_VER) 
    #define MY_LIB_API __declspec(dllexport) // Microsoft  
#elif defined(__GNUC__) 
    #define MY_LIB_API __attribute__((visibility("default"))) // GCC 
#else 
    #define MY_LIB_API // Most compilers export all the symbols by default. We hope for the best here. 
    #pragma warning Unknown dynamic link import/export semantics.
#endif

So, in your code it would be:

#pragma once

#if defined(_MSC_VER) 
    #define MATHLIBRARY_API __declspec(dllexport) // Microsoft  
#elif defined(__GNUC__) 
    #define MATHLIBRARY_API __attribute__((visibility("default"))) // GCC 
#else 
    #define MATHLIBRARY_API // Most compilers export all the symbols by default. We hope for the best here. 
    #pragma warning Unknown dynamic link import/export semantics.
#endif

extern "C" MATHLIBRARY_API void fibonacci_init(
        const unsigned long long a, const unsigned long long b);

extern "C" MATHLIBRARY_API bool fibonacci_next();

extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();

extern "C" MATHLIBRARY_API unsigned fibonacci_index();
Marko Papic
  • 1,846
  • 10
  • 23