How to export `class static member variable` in windows .dll library using CMake

710 views Asked by At

I am building a C ++ library for Windows, Linux / Unix based on CMake. I create a shared library(.dll) on Windows, and I want to use a static library(.a) on Linux / Unix.

On Windows, I know how to do __declspec(dllexport/dllimport) for every function and class I want to export. Then I saw these articles recently. (https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/) I tried to build CMakeLists.txt without __declspec() in the library function as described in the article.

include(GenerateExportHeader)
generate_export_header(mylibrary)

According to the article, in order to export the static member variables in the class, it is necessary to create the export header according to the existing method and to declare the variables as below.

#include <mylibary_export.h>

class MyClass
{
    static mylibrary_EXPORT int GlobalCounter;
    ...
}

The library has been compiled successfully, but attempting to reference this GlobalCount variable in the downstream project results in a LINK error.

Do you have an idea on this issue?

0

There are 0 answers