(I may have not been optimal for the tags of the question.)
I have a huge C++ library, perfectly tested and working under windows x86 and x64. (On these plateforms visual studio (2019) is used for the compilation.)
I wanted to give a try on linux, with WSL2 that I just installed.
I managed to create *.o files out of the code and to write a little main.cpp
from which I build an executable as follows :
$(CXX) -I$(IDIR) $(CXXFLAGS) main.cpp -fpermissive
$(CXX) $(CXXFLAGS2) -o main main.o $(OBJDIR)*.o -lpthread
As expected the executable has a big size, but it does what I expect it to do. Hence I want to stop using *.o files and rather build a shared library that I will link to while compiling my main.
Hence after having built the *.o files, I do a :
$(CXX) -shared -o MyLibrary.so $(OBJDIR)/*.o
Then I compile my main as follows :
$(CXX) -I$(IDIR) $(CXXFLAGS) main_so.cpp -fpermissive
$(CXX) $(CXXFLAGS2) -o main_so main_so.o -L$(SODIR)MyLibrary.so -lpthread
and the second line triggers an "undefined reference" at each call, in the main, of a function from the library. Classic, they mustn't be correctly exported as in am under WSL2. (Yes, I tested, with How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?) The library functions, in the c++, are exported as follows :
MYLIB_API void setToto1(/* signature */) ;
// ...
MYLIB_API void setTotoN(/* signature */) ;
where MYLIB_API is defined as follows :
#ifdef MYLIB_EXPORT
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif
and where MYLIB_EXPORT is added to the preprocessor definitions of the visual studio project of the library. Hence I replaced the previous preprocessor directives with :
#ifdef MYLIB_EXPORT
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define MYLIB_API __declspec(dllexport)
#elif __linux__
#define MYLIB_API __attribute__((dllexport))
#else
#error "Unknown compiler"
#endif
#else
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define MYLIB_API __declspec(dllimport)
#elif __linux__
#define MYLIB_API __attribute__((dllimport))
#else
#error "Unknown compiler"
#endif
#endif
and added the preprocessor option -D MYLIB_EXPORT -D __linux__
to the compilation of the *.o files and the assembling of the shared library file.
Nevertheless I keep having (whatever I try to tweak) these warnings at compilation :
warning: ‘dllexport’ attribute directive ignored [-Wattributes]
I didn't have these warnings in the first place before tweaking the preprocessor directive. And if I try to compile my main again (linkind dynamically to the shared library) I got the undefined references again.
I am sure I am missing something, but can't figure out what.