I'm trying to create a really simple DLL using Visual Studio 2015 and CMake.
To avoid possible DLL symbol export complications, CMake has the export of symbols enabled for Windows:
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS on)
My DLL contains a single pair of files (header and cpp):
file.h
#include <array>
#include <string>
class MyDLL
{
static const std::array<std::string, 2> MY_ARRAY;
};
file.cpp
const std::array<std::string, 2> MyDLL::MY_ARRAY =
{{
"one",
"two"
}};
But this always leads to LNK2001 Unresolved external symbol for MyDLL::MY_ARRAY
I've read
but AFAIK, all those weird MS macros (dllexport/dllimport) are already covered by the CMake settings. Actually, dllimport doesn't seem to be a required feature, but a helper for the compiler instead.
These look pretty similar, but with no answer so far:
Use both CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS and GENERATE_EXPORT_HEADER of CMake with both MSVC and GCC
http://www.cplusplus.com/forum/general/185807/
In fact, the own MS documentation suggests:
https://msdn.microsoft.com/en-us/library/f6xx1b1z.aspx
"Attempting to reference functions or data that don't have external linkage can cause LNK2001. In C++, inline functions and const data have internal linkage unless explicitly specified as extern."
but modifying the header to:
class MyDLL
{
extern static const std::array<std::string, 2> MY_ARRAY;
};
leads to a whole new plethora of issues :-)
I also tried the inline definition of MyDLL::MY_ARRAY in the header:
class MyDLL
{
static const std::array<std::string, 2> MY_ARRAY =
{{
"one",
"two"
}};
}
But then it triggers a C2864 error: a static data member with an in-class initializer must have non-volatile const integral type (in my case, a std::array).
Any idea of what I'm missing?
Thanks a lot in advance.
Side note: This works fine without modification in both Linux + gcc 4.8 and MacOSX + AppleClang 9.1.0.9020039