For my python extension, I have both C (from an embedded library) and C++ files and they are compiled and linked together. Only the C++ part interfaces with Python (via SWIG). This works both in windows with VS2015 and gcc under linux. However, with gcc, the C++ files need a different set of compiler flags (eg. -std=c++11, -Wno-reorder) than the C files, to avoid warnings concerning the improper flags in C.
Is there a way in setuptools / distutils to change compiler flags for each file seperatly, eg. based on the file extension?
I use already a custom build step from https://stackoverflow.com/a/36293331/3032680.
The main problem is, that distutils.ccompiler
does not check the file extension for C or C++ and is running everything with $CC. Even defining CXXFLAGS does not help. I will stand the warnings, neither with export
nor a definition using os.eniviron
in the setup.py file.
On macOS with CLang 8.0.0 the situation gets worse: Trying to compile a .c file with -std=c++11 is not a warning but an error.
Since distutils goes a long way to ensure that all files get compiled with the same compiler flags regardless of their file extension .c or .cpp. Hence even using CFLAGS and CXXFLAGS are not taken into account but gcc and CLang still handles them differently. Visual Studio just compiles everything as C++.
I got my problem resolved by accepting that C is in most cases still a subset of C++ and renamed the C-Source files to .cpp, even if the files contain C. This solution is ugly but I got rid of the warnings in gcc and the errors with CLang - especially since this solution blurs again the language barrier between C and C++.
A second solution I adopted later is to create a static library from the C Code outside distutlis and link the Python extension to that static library.