0

(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.

Olórin
  • 3,367
  • 2
  • 22
  • 42
  • On Linux there ain't attributes called dllexport, they use something with 'visibility' in the name – JVApen Jul 23 '20 at 06:37
  • See https://stackoverflow.com/a/52742992/2466431 – JVApen Jul 23 '20 at 06:38
  • 1
    @JVApen So basically you are saying that `MYLIB_API ` definition should (under linux, forget about windows) be modifed with `MYLIB_API __attribute__((visibility("default")))` replacing `#define MYLIB_API __declspec(dllexport)` and `#define MYLIB_API` replacing `` and that during for the compilation I have to add the options 1) `-fvisibility=hidden` to hide all MyLib's functions that don't have the MYLIB_API "flag" in the code (see https://gcc.gnu.org/wiki/Visibility) and 2) `-D LIBPRICER_EXPORT` for the "MYLIB_API attribute((visibility("default")))" directive to be activated in the code ? – Olórin Jul 23 '20 at 08:43
  • If this is what you are saying, I still have the undefined reference error to functions of MyLib wherever I used them in my main. – Olórin Jul 23 '20 at 08:45
  • Yes, I think so, for both the import and export, I'm not expert on this. I wouldn't know what the issue with main is giving – JVApen Jul 23 '20 at 16:19

0 Answers0