2
votes

I am trying to export a function to shared object (.so) using NDK from c++ using terminal on macOS mojave . I have installed latest versions of command line tool and Xcode.

Here's my GetNumber() which i want to export

#include <iostream>
#define DllExport __declspec(dllexport)

extern "C" {
   DllExport int GetNumber() {
      return 5 ;
   }
} 

After compiling the code using NDK , it throws the following error ,

enter image description here

I tried out clang and Xcode compiler as well as on tried same patterns on Windows and Ubuntu . I get the same error .

Tutorial i followed to export shared library using ndk

2
Isn't __declspec a Visual C++ specific extension for helping creating DLLs? - Some programmer dude
__declspec is microsoft compiler specific, and the Android ndk probably uses clang.' - zzxyz
And on POSIX platforms (like Linux or macOS, and since Android uses Linux then that to) it's not needed anyway. All non-static symbols are by default exported. - Some programmer dude
clang supports the keyword when specifying -fms-extensions. - Swordfish
Yah i got the flag recommendation "-fms-extensions" while compiling in Xcode . But still Xcode throws error for dllexport attribute not found - Atik M.

2 Answers

4
votes

You should use JNIEXPORT instead of DllExport on Android platforms.

The referenced tutorial you provided in your question is too old for current Android NDK development.

0
votes

Typically if you want to use declspec on cross-platform builds, what you'll have is something loosely like this, given a project ABC:

Windows project doing the exporting declares like so during dll builds:

#define ABC_EXPORT

All non-Windows projects or projects that plan on interacting with a lib version of the project (and the lib version of the project itself) declare:

#define ABC_LIB

then you have in your project:

#ifdef ABC_LIB //or #if (defined(ABC_LIB) || !defined(WIN32))
  #define ABC_API
#else
  #ifdef ABC_EXPORT 
    #define ABC_API __declspec(dllexport)
  #else
    #define ABC_API __declspec(dllimport)
  #endif
#endif

Then all your functions are declared like:

ABC_API void foo();

Better yet, if you have an OS obstraction layer you can use another #define instead of __declspec to completely insure that POSIX builds never have to worry about this stuff.

I highly recommend using export files instead, because this is not fun.