I'm trying to compile C++ code with python embedded in it. The compiler does not compile my code with '-fPIE' flag even though I've added it in CMakeList.txt. Here's my CMakeList:
cmake_minimum_required(VERSION 3.3)
project(TestCython)
add_executable(main main.cpp)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-6QL2k7/python3.8-3.8.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -fPIE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-6QL2k7/python3.8-3.8.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -fPIE")
# Eigen
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
# Embedded Python Includes
include_directories(/usr/include/python3.8
/usr/include/eigen3/ )
# Embedded Python Linker
link_directories(/usr/lib/python3.8/config-3.8-x86_64-linux-gnu
/usr/lib)
target_link_libraries( main
python3.8
crypt
pthread
dl
util
m )
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
If I run the generated make file, it will report a lot of similar errors and ask me to recompile with '-fPIE', like this one:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libpython3.8.a(call.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
The compilation succeeds if I don't use cmake:
g++ -I/usr/include/python3.8 -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-6QL2k7/python3.8-3.8.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -fPIE main.cpp -o main -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -L/usr/lib -lpython3.8 -lcrypt -lpthread -ldl -lutil -lm
Cmake version is 3.16.3. Python version is 3.8.2. System is Ubuntu 20.04
How can I solve those errors?
Note that most of the time this error has nothing to do with the PIE flag but is rather something else screwy about your build, unfortunately it’s a bit hard to tell because your CMake config is a bit screwy.
This particular error however is probably because you’re setting your properties incorrectly; Setting CMAKE_CXX_FLAGS and similar variables does nothing if you do it after defining your target with add_executable. This can be avoided entirely by using a more modern CMake style (out of scope for this answer, just google Modern CMake for some examples), in this particular case instead of setting flags on those variables you could for example do
target_compile_options(main PRIVATE -fpie)
I had similar error when I linked my library env2048 (having a file called ppo_buffer) to my bindings module env2048bindings:
[build] FAILED: python/env2048bindings.cpython-37m-x86_64-linux-gnu.so
[build] : && /usr/bin/g++-7 -fPIC -g -shared -o python/env2048bindings.cpython-37m-x86_64-linux-gnu.so python/CMakeFiles/env2048bindings.dir/env2048/bindings.cpp.o src/libenv2048.a -pthread && :
[build] /usr/bin/ld: src/libenv2048.a(ppo_buffer.cpp.o): relocation R_X86_64_PC32 against symbol `_ZSt3minIlERKT_S2_S2_' can not be used when making a shared object; recompile with -fPIC
[build] /usr/bin/ld: final link failed: Bad value
I had to set the properties for the library to make it work with:
set_target_properties(env2048 PROPERTIES POSITION_INDEPENDENT_CODE ON)
From these, I would make the guess that you might need this:
set_target_properties(python3.8 PROPERTIES POSITION_INDEPENDENT_CODE ON)
Related
I have a cythonised application which until yesterday was compiling and running completely fine.
Now, suddenly, when trying to compile (Ubuntu 20 server running on DigitalOcean) I get this:
creating build/temp.linux-x86_64-3.8
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/var/www/anagami_console/anagami/anagamienv/include -I/usr/include/python3.8 -c event.c -o build/temp.linux-x86_64-3.8/event.o
x86_64-linux-gnu-gcc: fatal error: Killed signal terminated program cc1
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
The same code compiles and runs perfectly on my windows desktop using python 3.9.
The error message I get is not particularly informative, so I am really not sure where to even look
Way to fix it was to delete every single .c and .pyd file and recompile fresh. I'm still not sure what was causing the original error - but deleting + doing a completely fresh recompile fixed it
I built IPOPT from source at /usr/local/
Then I tried to install ipopt for python python setup.py install
However the installer complained (error) that it could not locate IpStdCInterface.h
I then modified the content of setup.py file as follows: (line 1 and 3 had wrong paths)
IPOPT_ICLUDE_DIRS=['/usr/local/include/coin']
IPOPT_LIBS=['ipopt', 'coinhsl', 'coinlapack', 'coinblas', 'coinmumps', 'coinmetis']
IPOPT_LIB_DIRS=['/usr/local/lib/']
IPOPT_DLL=None
I modified the first and third line to point to the correct directory.
Now, the lib directory path (in the third line above) contains a library file named libipopt.so, libipopt.so.0, libipopt.la (same name, different extensions).
The setup is now not complaining about IpStdCInterface.h but it is unable to find the libs
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/cyipopt.o -L/usr/local/lib/ -lipopt -lcoinhsl -lcoinlapack -lcoinblas -lcoinmumps -lcoinmetis -o build/lib.linux-x86_64-2.7/ipopt/cyipopt.so
/usr/bin/ld: cannot find -lcoinhsl
/usr/bin/ld: cannot find -lcoinlapack
/usr/bin/ld: cannot find -lcoinblas
/usr/bin/ld: cannot find -lcoinmumps
/usr/bin/ld: cannot find -lcoinmetis
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Those libs should be provided by the libipopt.so file? Or do I need to install some other package?
Well, i have a same problem as you. My solution was add the following commands in .bashrc:
export IPOPT_HOME="/opt/CoinIpopt"
export PATH="${PATH}:${IPOPT_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${IPOPT_HOME}/lib"
/opt/CoinIpopt is the path which the IpOpt was installed.
I'm trying to install pjsip's Python binding but am running into a build error that I feel is environmental but am just not able to figure out what's wrong.
I'm able to build pjsip without issue but run into a problem when trying to build the python bindings -- I'm getting an error from ld about a bad value in one of the static libraries.
Any thoughts?
root#0fcbc7b108af:/src/pjproject-2.5.5/pjsip-apps/src/python# python setup.py install
running install
running build
running build_py
running build_ext
building '_pjsua' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DPJ_AUTOCONF=1 -I/src/pjproject-2.5.5/pjlib/include -I/src/pjproject-2.5.5/pjlib-util/include -I/src/pjproject-2.5.5/pjnath/include -I/src/pjproject-2.5.5/pjmedia/include -I/src/pjproject-2.5.5/pjsip/include -I/usr/include/python2.7 -c _pjsua.c -o build/temp.linux-x86_64-2.7/_pjsua.o
_pjsua.c: In function 'py_pjsua_enum_transports':
_pjsua.c:1202:17: warning: variable 'status' set but not used [-Wunused-but-set-variable]
pj_status_t status;
^
_pjsua.c: In function 'py_pjsua_conf_get_port_info':
_pjsua.c:2338:9: warning: variable 'status' set but not used [-Wunused-but-set-variable]
int status;
^
_pjsua.c: In function 'py_pjsua_get_snd_dev':
_pjsua.c:2714:9: warning: variable 'status' set but not used [-Wunused-but-set-variable]
int status;
^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/_pjsua.o -L/src/pjproject-2.5.5/pjlib/lib -L/src/pjproject-2.5.5/pjlib-util/lib -L/src/pjproject-2.5.5/pjnath/lib -L/src/pjproject-2.5.5/pjmedia/lib -L/src/pjproject-2.5.5/pjsip/lib -L/src/pjproject-2.5.5/third_party/lib -lpjsua-x86_64-unknown-linux-gnu -lpjsip-ua-x86_64-unknown-linux-gnu -lpjsip-simple-x86_64-unknown-linux-gnu -lpjsip-x86_64-unknown-linux-gnu -lpjmedia-codec-x86_64-unknown-linux-gnu -lpjmedia-x86_64-unknown-linux-gnu -lpjmedia-videodev-x86_64-unknown-linux-gnu -lpjmedia-audiodev-x86_64-unknown-linux-gnu -lpjmedia-x86_64-unknown-linux-gnu -lpjnath-x86_64-unknown-linux-gnu -lpjlib-util-x86_64-unknown-linux-gnu -lsrtp-x86_64-unknown-linux-gnu -lresample-x86_64-unknown-linux-gnu -lgsmcodec-x86_64-unknown-linux-gnu -lspeex-x86_64-unknown-linux-gnu -lilbccodec-x86_64-unknown-linux-gnu -lg7221codec-x86_64-unknown-linux-gnu -lyuv-x86_64-unknown-linux-gnu -lpj-x86_64-unknown-linux-gnu -lm -lrt -lpthread -lasound -o build/lib.linux-x86_64-2.7/_pjsua.so
/usr/bin/ld: /src/pjproject-2.5.5/pjsip/lib/libpjsua-x86_64-unknown-linux-gnu.a(pjsua_acc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/src/pjproject-2.5.5/pjsip/lib/libpjsua-x86_64-unknown-linux-gnu.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
I was able to find an answer in an old mailing list entry (http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2008-August/004456.html).
Set CFLAGS=-fPIC and rebuild the PJSIP library (make clean && ./configure && make dep && make), then follow the directions for making the Python library.
I have Matplotlib 1.3.1 installed on my Ubuntu 14.04 system and thought it was time to upgrade to 1.4.2. I never get the latest version for compatibility reasons (some packages that use Matplotlib might stop working). I then downloaded the tarball, uncompressed its contents into a folder and executed "sudo python setup.py build". However, I got the following error (last few lines only):
c++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/ft2font.o build/temp.linux-x86_64-2.7/src/mplutils.o build/temp.linux-x86_64-2.7/extern/CXX/cxxsupport.o build/temp.linux-x86_64-2.7/extern/CXX/cxx_extensions.o build/temp.linux-x86_64-2.7/extern/CXX/IndirectPythonInterface.o build/temp.linux-x86_64-2.7/extern/CXX/cxxextensions.o -L/usr/local/lib -L/usr/local/lib64 -lfreetype -lstdc++ -lm -o build/lib.linux-x86_64-2.7/matplotlib/ft2font.so
/usr/bin/ld: /usr/local/lib64/libstdc++.a(si_class_type_info.o): relocation R_X86_64_32S against `_ZTVN10__cxxabiv120__si_class_type_infoE' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib64/libstdc++.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
error: command 'c++' failed with exit status 1
I then tried the same with Matplotlib 1.4.3 (why not?) and got the same results.
What's going on here?
Thanks so much for any help.
Fausto
I am trying to build Python directly from source code, with no admin rights, and after running:
export CPPFLAGS='-I/opt/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include
-I/opt/tcl8.6.0/include/'
export LDFLAGS='-L/opt/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/
-L/opt/tcl8.6.0/lib/ ./configure --prefix=/path_to_python-2.7.4 --enable-shared'
and then
make
I get the following:
building '_tkinter' extension
gcc -pthread -fPIC -fno-strict-aliasing
-g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11/include -I. -IInclude -I./Include -I/opt/sqlite/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include -I/usr/local/include -I/opt/python/src/Python-2.7.4/Include -I/opt/python/src/Python-2.7.4 -c /opt/python/src/Python-2.7.4/Modules/_tkinter.c -o build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/_tkinter.o
gcc -pthread -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3
-Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11/include -I. -IInclude -I./Include -I/opt/sqlite/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include -I/usr/local/include -I/opt/python/src/Python-2.7.4/Include -I/opt/python/src/Python-2.7.4 -c /opt/python/src/Python-2.7.4/Modules/tkappinit.c -o build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/tkappinit.o
gcc -pthread -shared -L/opt/sqlite/sqlite-3.7.16.2/lib
-L/opt/tk8.6.0/lib/ -L/opt/tcl8.6.0/lib/ -L/opt/sqlite/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/ -L/opt/tcl8.6.0/lib/ -I. -IInclude -I./Include -I/opt/sqlite/sqlite-3.7.16.2/include -I/opt/tk8.6.0/include -I/opt/tcl8.6.0/include build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/_tkinter.o
build/temp.linux-x86_64-2.7/opt/python/src/Python-2.7.4/Modules/tkappinit.o
-L/usr/X11/lib -L/opt/sqlite/sqlite-3.7.16.2/lib -L/opt/tk8.6.0/lib/ -L/opt/tcl8.6.0/lib/ -L/usr/local/lib -L. -ltk8.6 -ltcl8.6 -lX11 -lpython2.7 -o build/lib.linux-x86_64-2.7/_tkinter.so
* WARNING: renaming "_tkinter" since importing it failed: libtk8.6.so: cannot open shared object file: No such file or directory
The odd thing is that I can see libtk8.6.so. It is actually right there under /opt/tcl8.6.0/lib as I specified with LDFLAGS.
Why did that compilation fail?
This problem takes place during installation in setup.py, when Python tries to import _tkinter. If you look at the function build_extension, there is a block that says:
imp.load_dynamic(ext.name, ext_filename)
This line tries a dynamic load of _tkinter (which uses the dynamic shared library libtk8.6.so) . So even though the compilation/linking worked, when Python tests the module, it uses the dynamic library, and I didn't have tcl/lib nor tk/lib in LD_LIBRARY_PATH. Once I added these, it all worked fine.
In summary:
I had to add the following include paths through CPPFLAGS
/path_to/sqlite3/include
/path_to/tcl/include
/path_to/tk/include
the following lib paths through LDFLAGS
/path_to/sqlite3/lib
/path_to/tcl/lib
/path_to/tk/lib
and the following lib paths through LD_LIBRARY_PATH:
/path_to/sqlite3/lib
/path_to/tcl/lib
/path_to/tk/lib
with all this, everything worked.