D7EAD / liboai

A C++17 library to access the entire OpenAI API.
https://openai.com/api/
MIT License
366 stars 64 forks source link

Proper support for BUILD_SHARED_LIBS on Windows #80

Open aschet opened 2 months ago

aschet commented 2 months ago

Describe the feature or improvement you're requesting

It would be great if you could add proper support for the compilation of the library as DLL on Windows via the BUILD_SHARED_LIBS CMake variable. Currently the declspec(dllexport) attribute is applied to many methods in the codebase. If there is no intention to support DLL compilation, please remove the declspec(dllexport) attribute since it is not required for static libraries. I would even consider this an error as it is applied now. To support DLL compilation, something like this has to be implemented:

liboai\include\core\response.h:

#if defined(__linux__) || defined(__APPLE__)
    #define LIBOAI_EXPORT
#else
    #ifdef LIBOAI_DLL
        #ifdef LIBOAI_BUILD
            #define LIBOAI_EXPORT __declspec(dllexport)
        #else
            #define LIBOAI_EXPORT __declspec(dllimport)
        #endif
    #else
        #define LIBOAI_EXPORT
    #endif
#endif

liboai\CMakeLists.txt:

if(BUILD_SHARED_LIBS)
  target_compile_definitions(${PROJECT_NAME}
    PRIVATE
      LIBOAI_BUILD
    PUBLIC
      LIBOAI_DLL
  )
endif()

Furthermore, the LIBOAI_EXPORT macro has to be applied to all methods that are used by a client.

Additional context

No response

richinseattle commented 2 days ago

I'm not sure if this fixes your specific issue, but after fixing the MSVC/vcpkg build configuration in https://github.com/D7EAD/liboai/pull/84 I tried

cmake .. -DBUILD_SHARED_LIBS:BOOL=ON

and was able to build DLLs. There may be some extra exports there, but its usable.