0

I am compiling a C++ code on mac using swig and making a shared .so library. 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:

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

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

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"
Abolfazl
  • 1,047
  • 2
  • 12
  • 29
  • how did you install python? using brew? – Marc Stroebel Nov 16 '22 at 09:08
  • I have python3.8 at `/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 ` and also at `/Users/tng/opt/anaconda3/bin/python3 `, I tried both of them. Once deactivate conda from .bashrc file to make sure which python version is being used. I think the first one was installed by brew. I don't remember. – Abolfazl Nov 16 '22 at 11:22
  • option `-I` add include location for headers, try to add `-L /Library/Frameworks/Python.framework/Versions/3.8...... `, – Marc Stroebel Nov 16 '22 at 11:31
  • I don't see any directory with `libpython`, but `.../3.8/lib` did not work. – Abolfazl Nov 16 '22 at 13:41

1 Answers1

0

Install python via brew or check your installation for python3-config tool, then launch e.g.

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

and set the output as linker flags to your cmdline, e.g.

-L/opt/homebrew/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin -ldl -framework CoreFoundation
Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21