Title: Override -shared option in g++
Tags: c++;g++;cython
Question: 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(&quot;LOCAL_INCLUDE&quot;)]
ext_modules = [
Extension(&quot;k3lib&quot;, sources=[&quot;main.pyx&quot;],
libraries=[&quot;k2&quot;], include_dirs=includes, library_dirs=includes, language=&quot;c++&quot;,
extra_compile_args=[&quot;-O3&quot;, &quot;-march=native&quot;], extra_objects=[f&quot;{includes[0]}/libk2.a&quot;],
extra_link_args=['-static', '-Wl,-Bstatic', '-flinker-output=exec'])
#extra_objects=[f&quot;{includes[0]}/libk2.a&quot;]
#extra_link_args=['-static']
setup(name=&quot;k3lib&quot;, ext_modules=cythonize(ext_modules, language_level=&quot;3&quot;))
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.
Comment: 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`

Comment: @ZanLynx Didn't know that. Will try it out

Comment: @ZanLynx Hey it works! Thanks a lot. Would you mind typing out a real answer so I can accept?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:05/21/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><g++><cython>', 'Title': 'Override -shared option in g++', 'ViewCount': 62, 'id': 67641890, 'user_AboutMe': None, 'user_AccountId': 13264688.0, 'user_DisplayName': '157 239n', 'user_Reputation': 319.0, 'user_Views': 58.0, 'user_id': 9577975.0}}