0

I was building some Cython extensions, and have to link it against a static library (it has CUDA code in them, so have to be static):

running build_ext
building 'k3lib' extension
gcc -pthread -B /home/kelvin/anaconda3/envs/torch/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/kelvin/repos/tools/include -I/home/kelvin/anaconda3/envs/torch/include/python3.8 -c main.cpp -o build/temp.linux-x86_64-3.8/main.o -O3 -march=native
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
g++ -pthread -shared -B /home/kelvin/anaconda3/envs/torch/compiler_compat -L/home/kelvin/anaconda3/envs/torch/lib -Wl,-rpath=/home/kelvin/anaconda3/envs/torch/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.8/main.o /home/kelvin/repos/tools/include/libk2.a -L/home/kelvin/repos/tools/include -lk2 -o build/lib.linux-x86_64-3.8/k3lib.cpython-38-x86_64-linux-gnu.so -static -Wl,-Bstatic -flinker-output=exec

However, Cython's g++ compile command includes the options -shared -fPIC by default. I tried a number of options at the end of the command via this setup file (the static library is at $(LOCAL_INCLUDE)/libk2.a):

includes = [os.getenv("LOCAL_INCLUDE")]

ext_modules = [
    Extension("k3lib", sources=["main.pyx"],
              libraries=["k2"], include_dirs=includes, library_dirs=includes, language="c++",
              extra_compile_args=["-O3", "-march=native"], extra_objects=[f"{includes[0]}/libk2.a"],
              extra_link_args=['-static', '-Wl,-Bstatic', '-flinker-output=exec'])
]
#extra_objects=[f"{includes[0]}/libk2.a"]
#extra_link_args=['-static']
setup(name="k3lib", ext_modules=cythonize(ext_modules, language_level="3"))

Still, g++ thinks that I want to build a shared library, and thus the error message. Is there a way to override the -shared option? I'm planning to go into Cython's files and edit them myself, but was wondering is there a simpler way?

Context: I was following this question on SO but can't replicate their success.

157 239n
  • 349
  • 3
  • 15
  • The extension has to be a shared library to be loaded into Python. You should be able to link static libraries into your shared library. That isn't exclusive. One thing to watch is that the static library has to have been built with `-fPIC` – Zan Lynx May 21 '21 at 18:12
  • @ZanLynx Didn't know that. Will try it out – 157 239n May 21 '21 at 18:14
  • @ZanLynx Hey it works! Thanks a lot. Would you mind typing out a real answer so I can accept? – 157 239n May 21 '21 at 18:20

0 Answers0