0

I have static library libA-static that defines function privateFunction and is linked into library libB-static. libB-static implements a lot of stuff but exports only one function publicFunction. libB-static is then linked into shared library libC-shared that re-exports publicFunction. As suggested here and here, I compile all libraries with -fvisibility=hidden, label public with default visibility and link libB-static to libC-shared as whole archive.

My main program depends on libC-shared and libA-static. The setup works fine on Linux, Mac (and on Windows too), but on MinGW linking fails with multiple definitions of symbol privateFunction. The second definition comes from the import library libC-shared.dll.a. The size of import library is ridiculous for a single exported symbol (10MB) and it looks like it contains at least entire libB-static.

I tried linking libC-shared with -Wl,--exclude-libs,All, and the size of import library in this case is 1 KB, but publicFunction is then unresolved. I also tried linking libC-shared with -Wl,--exclude-libs,libB-static, but in this case linking of libC-shared fails to resolve privateFunction.

This is the linking command generated by CMake, where libC-shared=GraphicsEngineOpenGL-shared, libB-static=GraphicsEngineOpenGL-static and libA is all other static libs:

C:\PROGRA~1\MINGW-~1\X86_64~1.0-P\mingw64\bin\G__~1.EXE    -shared -o GraphicsEngineOpenGL-shared.dll -Wl,--out-implib,libGraphicsEngineOpenGL-shared.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -Wl,--whole-archive CMakeFiles\GraphicsEngineOpenGL-shared.dir/objects.a -Wl,--no-whole-archive -Wl,--whole-archive libGraphicsEngineOpenGL-static.a -Wl,--no-whole-archive ../GLSLTools/libGLSLTools.a ../HLSL2GLSLConverterLib/libHLSL2GLSLConverterLib.a ../GraphicsEngine/libGraphicsEngine.a ../GraphicsAccessories/libGraphicsAccessories.a ../../Common/libCommon.a ../../Platforms/Win32/libWin32Platform.a ../../Platforms/Basic/libBasicPlatform.a -lShlwapi ../../Primitives/libPrimitives.a ../../External/glew/libglew-static.a -lopengl32 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 

My main problem is obviously linking error, but ridiculous size of the import library is probably the key to the answer.

Egor
  • 779
  • 5
  • 20
  • Which language, C or C++? They are distinct languages. For example, the C++ allows overloading of functions and some compilers use *name mangling* to manage the overloading. This may interfere with libraries built by compilers that use different schemes of name mangling. – Thomas Matthews Jan 23 '19 at 16:46
  • The language is c++. In either case I don't understand why definition of a hidden function could end up in the import library. – Egor Jan 23 '19 at 16:47
  • You should update your tags. – Thomas Matthews Jan 23 '19 at 16:48
  • @ThomasMatthews Removed c tag – Egor Jan 23 '19 at 16:53

0 Answers0