C++ Cross Platform Dynamic Libraries; Linux and Windows

后端 未结 3 1878
孤街浪徒
孤街浪徒 2020-12-12 21:16

I need some help on writing cross-platform code; not an application, but a library.

I am creating a library both static and dynamic with most of the development done

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 21:53

    Maybe it's better if you Add extern "C" !!!,

    /* file CMakeLists.txt */

     SET (LIB_TYPE SHARED)
     ADD_LIBRARY(MyLibrary ${LIB_TYPE} MyLibrary.h)
    

    /* file MyLibrary.h */

    #if defined(_WIN32) || defined(__WIN32__)
    #  if defined(MyLibrary_EXPORTS) // add by CMake 
    #    define  MYLIB_EXPORT extern "C" __declspec(dllexport)
    #  else
    #    define  MYLIB_EXPORT extern "C" __declspec(dllimport)
    #  endif // MyLibrary_EXPORTS
    #elif defined(linux) || defined(__linux)
    # define MYLIB_EXPORT
    #endif
    
    MYLIB_EXPORT inline int Function(int a) {
        return a;
    }
    

提交回复
热议问题