A python C++ extension has the following structure:
/myextension
|_____basecode
| |__header.h
| |__functions.cpp
|
|_____utilities
| |______utilities.h
| |______utilities.cpp
|
|_____builtins
|______builtins.h
|______builtins.cpp
Both utilities.cpp and builtins.cpp has functions that call a function func from functions.cpp. The current build system first creates statically linked sub-libraries inside each directory: basecode/basecode.a, utilities/utilities.a, and builtins/builtins.a, and then links them as a shared library _myextension.pyd. Probably because func is defined in all three static libs, the linker complains about multiple definition, and we work around that using '-Wl,--allow-multiple-definition' for g++ and '/FORCE:MULTIPLE', '/WHOLEARCHIVE' option for MS Visual Studio Build tools.
However, MS linker gives a warning LNK4088: image being generated due to /FORCE option; image may not run.
Is there a way to organize the code/build so as to avoid this multiple definition issue?
Some points on the current design:
- The real project is quite large with many subdirectories, each having dozens of C++ files defining classes. All of them do not fit in a single command line for the compiler.
- Unless the sublibraries are statically linked, on Windows MSVC, all the unused classes and functions are excluded in the final shared library. Only those used in a
main.cppare retained. - I am using meson+ninja to build the extension.
- The reason I want to avoid multiple definitions is because I suspect MSVC linker may be messing up virtual inheritance under this condition, where in a diamond-shaped inheritance a virtual function is failing to get called.
.dllor.pydor.exefile, by linking the only static library that contains that symbol..pydfiles are really.dllfiles, just renamed), gcc is so kind as to ignore those specifiers and not error on them, but you can do the common macro tricks