0

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:

  1. 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.
  2. 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.cpp are retained.
  3. I am using meson+ninja to build the extension.
  4. 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.
7
  • by not adding the same source file/ object file in multiple libraries ... Commented Aug 29, 2024 at 15:39
  • @AhmedAEK can you elaborate? The sublibs do not compile unless functions.cpp is included in their build, because they use the same function from this file. Is there a way to create static sublibs where some function is in another lib? Commented Aug 29, 2024 at 15:46
  • 1
    you can compile a static library with missing symbols, those symbols should only be "not-missing" when you finally build the .dll or .pyd or .exe file, by linking the only static library that contains that symbol. Commented Aug 29, 2024 at 15:47
  • 1
    You could also make basecode a header only library (if suitable) Commented Aug 29, 2024 at 15:48
  • 1
    also on MSVC when building a dll you should use __declspec(dllexport) or MSVC won't export them, (note: .pyd files are really .dll files, just renamed), gcc is so kind as to ignore those specifiers and not error on them, but you can do the common macro tricks Commented Aug 29, 2024 at 15:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.