We built the library on both Linux (gcc) and Windows (Visual Studio), and, as expected, we found minor, but not significant differences between what is needed to get a clean compilation on both platforms.
Today I changed the flag of the gcc compiler to use -fPIC (to enable the creation of a shared library). When we tested the linking of the program with the library, we started getting errors (for the first time), with undefined reference up to 2 static constants that are declared and initialized in the header file (but not in the .cpp file).
I found an https://stackoverflow.com/a/312690/16950/... which seemed to address the problem, explaining that even if the static const initialized in the header file, it still needs to be defined in the code file. And making this change fixed the gcc linker error.
Visual Studio, however, did not like this change and generated multiple definition errors. We had to wrap the definition needed for the preprocessor to make Visual Studio compile easily.
Can someone enlighten me about what the difference is here? (Code excerpt below.)
msg.h
class msg { public: static const int EMPTY_INT_VALUE = INT_MAX; static const char EMPTY_STRING_VALUE = '\002';
msg.cpp
#include "msg.h" const double msg::EMPTY_DOUBLE_VALUE(DBL_MAX); #ifndef _WIN32
source share