在 Android 上导出共享库的类时出现问题
我有一个核心项目,正在将其构建为共享库。在其中一个标头中,我定义了一个简单的类,如下所示:
typedef pthread_mutex_t Mutex;
class CORE_API AutoLock
{
public:
AutoLock(Mutex& m);
~AutoLock();
private:
AutoLock();
AutoLock(const AutoLock&);
AutoLock& operator=(const AutoLock&);
Mutex m_Mutex;
};
其中 CORE_API 定义为:
#ifdef CORE_DLL
#define CORE_API __attribute__ ((dllexport))
#else
#define CORE_API __attribute__ ((dllimport))
#endif
在核心的 Android.mk 中,我在 LOCAL_CFLAGS 下定义了 CORE_DLL。但是,在构建时,我收到警告:
warning: 'dllimporot' attribute directive ignored
当 ndk-build 到达我想使用 AutoLock 类的其他项目时,我收到错误:
error: 'AutoLock::AutoLock()' is private
error: within this context
Why would the compilerignore the dllexport attribute?我希望一旦这个问题得到解决,我的其他项目应该可以构建并能够毫无问题地使用 AutoLock 类。
I have a core project which I'm building as a shared library. In one of the headers, I've defined a simple class shown below:
typedef pthread_mutex_t Mutex;
class CORE_API AutoLock
{
public:
AutoLock(Mutex& m);
~AutoLock();
private:
AutoLock();
AutoLock(const AutoLock&);
AutoLock& operator=(const AutoLock&);
Mutex m_Mutex;
};
where CORE_API is defined as:
#ifdef CORE_DLL
#define CORE_API __attribute__ ((dllexport))
#else
#define CORE_API __attribute__ ((dllimport))
#endif
In the Android.mk for core, I've defined CORE_DLL under LOCAL_CFLAGS. However, when building, I get the warning:
warning: 'dllimporot' attribute directive ignored
When ndk-build gets to the other project where I want to use the AutoLock class, I get the error:
error: 'AutoLock::AutoLock()' is private
error: within this context
Why would the compiler ignore the dllexport attribute? I would hope that once that's fixed, my other project should build and be able to use the AutoLock class without any problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Android (Linux) 上创建共享库的方式与在 Windows 上不同。
在 Windows 中,您有特殊的 dllimport 和 dllexport 指令,但在 Android (Linux) 上没有。
使用共享库时,您应该使用 -lYourLibraryName 进行编译,
这可能会帮助您: http ://www.adp-gmbh.ch/cpp/gcc/create_lib.html
Shared libraries are created differently on Android (Linux) than with Windows.
In Windows you have the special dllimport and dllexport directives but not on Android (Linux).
When using your shared library, you should just compile with -lYourLibraryName
This might help you: http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
查看 android-ndk 中提供的示例代码,特别是 Android.mk 文件,这可能会解决您的问题。
Have a look at the sample code available in the android-ndk, specially the Android.mk file this might solve your problem.