It is sometimes difficult to decide what flags to set for the compiler and the best advice is to use the same flags that the version of Python you are using was compiled with. Here are a couple of ways to do that.
19.1. From the Command Line¶In the Python install directory there is a pythonX.Y-config executable that can be used to extract the compiler flags where X is the major version and Y the minor version. For example (output is wrapped here for clarity):
$ which python3 /Library/Frameworks/Python.framework/Versions/3.13/bin/python3 $ python3 -VV Python 3.13.0b3 (v3.13.0b3:7b413952e8, Jun 27 2024, 09:57:31) [Clang 15.0.0 (clang-1500.3.9.4)] $ /Library/Frameworks/Python.framework/Versions/3.13/bin/python3-config --cflags -I/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13 -I/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13 -fno-strict-overflow -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -O3 -Wall -arch arm64 -arch x86_64 -g19.2. Programmatically from Within a Python Process¶
The sysconfig
module contains information about the build environment for the particular version of Python:
1>>> import sysconfig 2>>> sysconfig.get_config_var('CFLAGS') 3'-fno-strict-overflow -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -O3 -Wall -arch arm64 -arch x86_64 -g' 4>>> import pprint 5>>> pprint.pprint(sysconfig.get_paths()) 6{'data': '/Library/Frameworks/Python.framework/Versions/3.13', 7 'include': '/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13', 8 'platinclude': '/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13', 9 'platlib': '/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages', 10 'platstdlib': '/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13', 11 'purelib': '/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages', 12 'scripts': '/Library/Frameworks/Python.framework/Versions/3.13/bin', 13 'stdlib': '/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13'} 14>>> sysconfig.get_paths()['include'] 15'/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13'19.3. From the Command Line using
sysconfig
¶
This very verbose output will give you a complete picture of your environment:
1$ python3 -m sysconfig 2Platform: "macosx-10.13-universal2" 3Python version: "3.13" 4Current installation scheme: "posix_prefix" 5 6Paths: 7 data = "/Library/Frameworks/Python.framework/Versions/3.13" 8 include = "/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13" 9 platinclude = "/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13" 10 platlib = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages" 11 platstdlib = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13" 12 purelib = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages" 13 scripts = "/Library/Frameworks/Python.framework/Versions/3.13/bin" 14 stdlib = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13" 15 16Variables: 17 ABIFLAGS = "" 18 AC_APPLE_UNIVERSAL_BUILD = "0" 19 AIX_BUILDDATE = "0" 20 AIX_GENUINE_CPLUSPLUS = "0" 21 ALIGNOF_LONG = "8" 22 ALIGNOF_MAX_ALIGN_T = "8" 23 ALIGNOF_SIZE_T = "8" 24 ALT_SOABI = "0" 25 ANDROID_API_LEVEL = "0" 26 AR = "/usr/bin/xcrun ar" 27 ...19.4. Setting Flags Automatically in
setup.py
¶
The sysconfig module allows you to create a generic setup.py
script for Python C extensions, something along these lines:
1from distutils.core import setup, Extension 2import os 3import sysconfig 4 5_DEBUG = False 6# Generally I write code so that if DEBUG is defined as 0 then all optimisations 7# are off and asserts are enabled. Typically run times of these builds are x2 to x10 8# release builds. 9# If DEBUG > 0 then extra code paths are introduced such as checking the integrity of 10# internal data structures. In this case the performance is by no means comparable 11# with release builds. 12_DEBUG_LEVEL = 0 13 14# Common flags for both release and debug builds. 15extra_compile_args = sysconfig.get_config_var('CFLAGS').split() 16extra_compile_args += ["-std=c++11", "-Wall", "-Wextra"] 17if _DEBUG: 18 extra_compile_args += ["-g3", "-O0", "-DDEBUG=%s" % _DEBUG_LEVEL, "-UNDEBUG"] 19else: 20 extra_compile_args += ["-DNDEBUG", "-O3"] 21 22setup( 23 name = '...', 24 version = '...', 25 author = '...', 26 author_email = '...', 27 maintainer = '...', 28 maintainer_email = '...', 29 description = '...', 30 long_description = """... 31""", 32 platforms = ['Mac OSX', 'POSIX',], 33 classifiers = [ 34 '...', 35 ], 36 license = 'GNU Lesser General Public License v2 or later (LGPLv2+)', 37 ext_modules=[ 38 Extension("MyExtension", 39 sources=[ 40 '...', 41 ], 42 include_dirs=[ 43 '.', 44 '...', 45 os.path.join(os.getcwd(), 'include'), 46 sysconfig.get_paths()['include'], 47 ], 48 library_dirs = [os.getcwd(),], # path to .a or .so file(s) 49 extra_compile_args=extra_compile_args, 50 language='c++11', 51 ), 52 ] 53)19.5. Getting C/C++ Flags from a CMake Build¶
If your project can be built under CMake (such as this one can be) then the compiler flags can be obtained by looking at the generated file cmake-build-debug/CMakeFiles/<PROJECT>.dir/flags.make
(debug) or cmake-build-release/CMakeFiles/<PROJECT>.dir/flags.make
(release).
For example, for this project the file is cmake-build-debug/CMakeFiles/PythonExtensionPatterns.dir/flags.make
.
This looks something like this (wrapped for clarity and replaced user with <USER>) :
1# CMAKE generated file: DO NOT EDIT! 2# Generated by "Unix Makefiles" Generator, CMake Version 3.28 3 4# compile C with \ 5# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc 6# compile CXX with \ 7# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ 8C_DEFINES = 9 10C_INCLUDES = -I/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13 \ 11 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/debugging/XcodeExample/PythonSubclassList/PythonSubclassList \ 12 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/cpy \ 13 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/cpy/Containers \ 14 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/cpy/Watchers 15 16C_FLAGSarm64 = -g -std=gnu11 -arch arm64 -isysroot \ 17 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk \ 18 -fcolor-diagnostics -Wall -Wextra -Wpedantic -Werror -Wfatal-errors \ 19 -Wno-unused-variable -Wno-unused-parameter -fexceptions \ 20 -Wno-c99-extensions -Wno-c++11-extensions -O0 -g3 -ggdb \ 21 -Wno-unused-function 22 23C_FLAGS = -g -std=gnu11 -arch arm64 -isysroot \ 24 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk \ 25 -fcolor-diagnostics -Wall -Wextra -Wpedantic -Werror -Wfatal-errors \ 26 -Wno-unused-variable -Wno-unused-parameter -fexceptions \ 27 -Wno-c99-extensions -Wno-c++11-extensions -O0 -g3 -ggdb \ 28 -Wno-unused-function 29 30CXX_DEFINES = 31 32CXX_INCLUDES = -I/Library/Frameworks/Python.framework/Versions/3.13/include/python3.13 \ 33 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/debugging/XcodeExample/PythonSubclassList/PythonSubclassList \ 34 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/cpy \ 35 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/cpy/Containers \ 36 -I/Users/<USER>/CLionProjects/PythonExtensionPatterns/src/cpy/Watchers 37 38CXX_FLAGSarm64 = -g -std=gnu++17 -arch arm64 -isysroot \ 39 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk \ 40 -fcolor-diagnostics -Wall -Wextra -Wpedantic -Werror -Wfatal-errors \ 41 -Wno-unused-variable -Wno-unused-parameter -fexceptions \ 42 -Wno-c99-extensions -Wno-c++11-extensions -O0 -g3 -ggdb \ 43 -Wno-unused-function 44 45CXX_FLAGS = -g -std=gnu++17 -arch arm64 -isysroot \ 46 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk \ 47 -fcolor-diagnostics -Wall -Wextra -Wpedantic -Werror -Wfatal-errors \ 48 -Wno-unused-variable -Wno-unused-parameter -fexceptions \ 49 -Wno-c99-extensions -Wno-c++11-extensions -O0 -g3 -ggdb \ 50 -Wno-unused-function
This would be fairly easy to parse, perhaps by setup.py
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4