Search code examples
c++dlldllexport

Dll export symbol of function from static linked library


I am wrapping a static library in Dll to hide a lot of the implementation stuff since only 4-5 functions are needed and to avoid providing all third-party libraries and many header files. I seem to be having an issue with exporting a function to the dll from the static lib. The static lib has settings classes / structs similar to the one below

struct FooSettings
{
   bool Read(const std::string& file); // implemented in .cpp
   bool Write(const std::string& file); // implemented in .cpp
   // rest members, just plain types
};

In the Dll side

#include "FooSettings.h"

#if defined(WIN32) || defined(_WIN32)
    #if defined(LIB_EXPORT)
        // DLL Build, exporting symbols
        #define LIB_API __declspec(dllexport)
    #elif LIB_IMPORT
        // DLL use, importing symbols
        #define LIB_API __declspec(dllimport)
    #endif
#endif

#ifndef LIB_API
    #define LIB_API
#endif

class LIB_API LibSDK
{
public:
    LibSDK();
    ~LibSDK();

    FooSettings get() const noexcept;
    void set(const FooSettings& settings) const noexcept;

    void dummy() 
    {
        foo.Read("");
    }

private:
    // etc...
};

I can call dummy() on the "client" side without any issues but the code below leads to unresolved symbols

FooSettings foo;
foo.Read("");

I would have expected that the FooSettings:Read is at least exported since it is part of a dummy function. Am I missing something ? My preference is to export it without the dummy function but I dont seem to be able to make it work either way.


Solution

  • Coming back to all this, the answer was actually to #define LIB_EXPORT in the static library build as well which I did not expect.

    I did not think that such a thing would be needed for a static .lib files since all they are is just a bundle of object files so they did not need to be marked for export. Apparently, it was needed if you want to export the functions from the static lib to your wrapper dll.