You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu18.04下SWIG编译时g++出现未识别命令行选项错误求助

问题解决:G++编译时的未知命令行选项错误

错误根源

你在Makefile的编译命令中直接写了-/home/zkx/.conda/envs/baseline/python3.7m,这是错误写法:G++不存在以-开头直接接路径的选项,此处你需要用-I(大写字母I)指定Python头文件的搜索目录,且原路径不完整——Python头文件通常存放在conda环境目录的include/pythonX.Xm子目录下,你漏写了include部分。

修改后的Makefile

compile:
    swig -c++ -python cords.i
    g++ -c -O3 -fPIC -std=c++11 -fopenmp cords.cpp cords_wrap.cxx -I/home/zkx/.conda/envs/baseline/include/python3.7m
    g++ -shared -O3 -std=c++11 -fopenmp -Wl,-soname,_cords.so -o _cords.so cords.o cords_wrap.o
    swig -c++ -python shrink_cpp.i
    g++ -c -O3 -fPIC -std=c++11 -fopenmp shrink_cpp.cpp shrink_cpp_wrap.cxx -I/home/zkx/.conda/envs/baseline/include/python3.7m
    g++ -shared -O3 -std=c++11 -fopenmp -Wl,-soname,_shrink_cpp.so -o _shrink_cpp.so shrink_cpp.o shrink_cpp_wrap.o

clean:
    rm *.o *.so *.cxx cords.py shrink_cpp.py

更可靠的优化写法(可选)

为避免手动写路径出错,可通过python3-config --includes命令自动获取Python头文件路径,修改后的Makefile如下:

PY_INCLUDES := $(shell python3-config --includes)

compile:
    swig -c++ -python cords.i
    g++ -c -O3 -fPIC -std=c++11 -fopenmp cords.cpp cords_wrap.cxx $(PY_INCLUDES)
    g++ -shared -O3 -std=c++11 -fopenmp -Wl,-soname,_cords.so -o _cords.so cords.o cords_wrap.o
    swig -c++ -python shrink_cpp.i
    g++ -c -O3 -fPIC -std=c++11 -fopenmp shrink_cpp.cpp shrink_cpp_wrap.cxx $(PY_INCLUDES)
    g++ -shared -O3 -std=c++11 -fopenmp -Wl,-soname,_shrink_cpp.so -o _shrink_cpp.so shrink_cpp.o shrink_cpp_wrap.o

clean:
    rm *.o *.so *.cxx cords.py shrink_cpp.py

该写法会自动读取当前conda环境下Python的头文件路径,兼容性更强。

内容的提问来源于stack exchange,提问作者North_HITer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 14:02:15