How to compile python source code with some specific parameters?

I’m trying to compile python from source to generate a static library so the end-user doesnt need python in his system!

I’m using:

./configure --enable-shared=no --enable-optimizations

it generates the static library, but when linking inside my dynamic library the following error appears:

/bin/ld: ./libs/libpython3.11.a(pythonrun.o): warning: relocation against `_Py_UnhandledKeyboardInterrupt' in read-only section `.text.unlikely'
/bin/ld: ./libs/libpython3.11.a(pylifecycle.o): relocation R_X86_64_PC32 against symbol `_PyRuntime' can not be used when making a shared object; recompile with -fPIC

I tried compiling python with -fPIC but not sure if the argument worked, but I want to compile it with the following arguments:

-CC=g++ 
--CXX=g++ 
--CPPFLAGS=-fPIC
--CXXFLAGS=-std=c++17

so any idea?

Try using a C compiler not a C++ compiler. Does that change the outcome?
Python is C program not a C++ program.

I’m not changing the compiler, as I said the arguments I’ve passed doesnt work, I’m loking for the right way to pass compiler arguments!

I am confused where does the g++ come from? Are you saying that is not your script?
I would expect to see gcc not g++.

nope!

do you see in the part I'm using: above, thats my code to compile!

the:

-CC=g++ 
--CXX=g++ 
--CPPFLAGS=-fPIC
--CXXFLAGS=-std=c++17

are the arguments I WANT to use, but I dont know how to pass them to the configure script

because these arguments above, are the same I’m using in my dynamic library! Or at least how to pass -fPIC

Oh i see your code is C++ but you need python to be built with -fPIC.
Check configure script to be sure, but the usual method is

CFLAGS=-fPIC ./configure --enable-shared=no --enable-optimizations
1 Like

thank you very much, it worked xD