Search code examples
c++

symbol of global map not "visible": how to share a global map from a shared library?


I have a library with several global maps:

in header (header0):

extern std::map<AVPixelFormat, std::string> AVPixelFormatMap;
extern std::map<AVMediaType, std::string> AVMediaTypeMap;
extern std::map<AVCodecID, std::string> AVCodecIDMap;

in cpp (cpp0):

std::map<AVPixelFormat, std::string> AVPixelFormatMap
{
    {AV_PIX_FMT_NONE, "AV_PIX_FMT_NONE: None"},

etc.

In another library, I include the header0 in the cpp file, and link to the file library0.lib, but the compiler complains with Error LNK2001:

> Severity  Code    Description Project File    Line    Suppression State   Details
> Error LNK2001 unresolved external symbol "class std::map<enum
> AVPixelFormat,class std::basic_string<char,struct
> std::char_traits<char>,class std::allocator<char> >,struct
> std::less<enum AVPixelFormat>,class std::allocator<struct
> std::pair<enum AVPixelFormat const ,class
> std::basic_string<char,struct std::char_traits<char>,class
> std::allocator<char> > > > > AVPixelFormatMap"
> (?AVPixelFormatMap@@3V?$map@W4AVPixelFormat@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@W4AVPixelFormat@@@3@V?$allocator@U?$pair@$$CBW4AVPixelFormat@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@3@@std@@A)  library0    path/to\library0.obj    1

I tried extern __declspec(dllexport) std::map< the error remains.

I would like to use those maps in a shared library and not have to inline them in the other libraries using it.

What am I doing wrong ?


Solution

  • You might want to use this macro:

    #ifdef UTIL_EXPORTS 
    #define DLLEXPORT __declspec(dllexport)  
    #else
    #define DLLEXPORT __declspec(dllimport)  
    #endif
    

    and declare your maps like this:

    extern DLLEXPORT std::map<AVPixelFormat, std::string> AVPixelFormatMap;