简体   繁体   中英

How to change the linker command for building a C++ extension module in Python setup.py?

A python module build command

$ python setup.py build

shows the linker is "clang" as below but I want to change it to "ld." Using clang++ command for linking does not work on my Mac and changing it to "ld" works. I don't know how to change the link command in setup.py to use "ld". Could you let me know how to change the link command?

os.environ["CC"] = "clang"   # need this setting to use clang instead of gcc
os.environ["CXX"] = "clang"  # need this setting to use clang instead of gcc
...
...
clang++ -bundle -undefined dynamic_lookup -arch x86_64 -g build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/ext.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/GeodisTK/geodesic_distance.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/GeodisTK/geodesic_distance_2d.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/GeodisTK/geodesic_distance_3d.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/GeodisTK/geodesic_distance_interface.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/GeodisTK/util.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/filtering/bilateral/bilateralfilter_cpu.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/filtering/bilateral/bilateralfilter_cpu_phl.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/filtering/permutohedral/permutohedral.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/filtering/permutohedral/permutohedral_cpu.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/lltm/lltm_cpu.o build/temp.macosx-10.9-x86_64-3.8/Volumes/SSD256/Projects/MONAI.yoshi/monai/csrc/resample/pushpull_cpu.o -L/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/lib -lc10 -ltorch -ltorch_cpu -ltorch_python -o build/lib.macosx-10.9-x86_64-3.8/monai/_C.so

You can use the LDSHARED environment variable to set the linker command:

LDSHARED=/usr/bin/ld python setup.py build_ext

I suggest you read the source code of the distutils package to see how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM