我想构建一个可供任何c++
和c#
应用程序可以使用的dll
。我在 mac 上安装了cLion
并启动了一个新项目作为c++ library
.
我在头文件中写了一些代码行。由于我想导出我的函数,所以我使用了__fdeclspec
但它抛给我错误'__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes
我的代码是
#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();
如何修复此错误以便构建解决方案?
可以使用以下预处理器检查为导出语义创建跨平台宏:
#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
因此,在您的代码中,它将是:
#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();