简体   繁体   English

在 mac 上使用 swig 编译 C++ 代码返回 -lpython 库未找到

[英]compiling C++ code with swig on mac return -lpython library not found

I am compiling a C++ code on mac using swig and making a shared .so library.我正在使用 swig 在 mac 上编译 C++ 代码并创建一个共享的.so库。 I have found a similar question here but I get an error for the last line:我在这里发现了一个类似的问题,但最后一行出现错误:

ls 
sl.i sl.hpp

swig -c++ -python sl.i
clang -O2 -fPIC -c sl_wrap.cxx -I /Library/Frameworks/Python.framework/Versions/3.8/include/python3.8
clang -lpython -dynamiclib sl_wrap.o -o _sl.so

ls
sl.py sl_wrap.o sl.wrap.cxx sl.i sl.hpp

error:错误:

ld: library not found for -lpython
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

My machine:我的机器:

ProductName:        macOS
ProductVersion:     13.0

Homebrew clang version 15.0.2
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin


SWIG Version 4.0.2
Compiled with clang++ [x86_64-apple-darwin21.1.0]
Configured options: +pcre

A toy example:玩具示例:

sl.hpp: sl.hpp:

#include <cmath>
#include <string>
#include <iostream>

class My_Class
{
private:
    int N;    
    
public:   
    int add(int a, int b)
    {
        return (a+b);
    }
};

sl.i: sl.i:


%module sl

%{
#include "sl.hpp"
%}

%include stl.i
%include "std_string.i"
/* instantiate the required template specializations */
namespace std {
    %template(IntVector)     vector<int>;
    %template(DoubleVector)  vector<double>;
    %template(DoubleVector2) vector<vector<double> >;
    %template(SingleVector)  vector<float>;
    %template(SingleVector2) vector<vector<float> >;
}

%include "sl.hpp"

Install python via brew or check your installation for python3-config tool, then launch eg通过brew安装 python 或检查你安装的 python3-config 工具,然后启动例如

/opt/homebrew/bin/python3-config --ldflags

and set the output as linker flags to your cmdline, eg并将 output 设置为 linker 标志到您的命令行,例如

-L/opt/homebrew/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin -ldl -framework CoreFoundation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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