0

I am currently working to get some intuition about interfacing C-Code to Python. For interfacing C-code to Python, I am using ctypes module and following these implementations such that:

As we know that

The ctypes module provides C compatible data types and functions to load DLLs so that calls can be made to C shared libraries without having to modify them.

I have just implemented a simple function using C and generated " Shared Library " of the file using these commands on Cygwin:

For Linux, 
- cc -fPIC -shared -o libfunc.so function.c
- gcc -shared -Wl,-soname,adder -o adder.so -fPIC function.c

On Windows, assuming that you have GCC installed: 
1 ~ $ gcc -std=c11 -Wall -Wextra -pedantic -c -fPIC function.c -o libfunc.o
2 ~ $ gcc -shared libfunc.o -o libfunc.dll

Then I placed all the files in the same directory. Following are given files:

  • function.c
  • libfunc.so
  • Testing.py

function.c

int func(int num) 
{
    if (num == 0)
        return 0;
    else
        return 1;
}

Testing.py

import os
import ctypes
from ctypes.util import find_library


num = 16
# To verify that Library is available in the specified path
pathToWin32Environment = os.getcwd()
pathToDll = pathToWin32Environment + "\\libfunc.so"
if not os.path.exists(pathToDll):
    raise Exception('Could not locate ' + pathToDll)

curr_dir_before = os.getcwd()
os.chdir(pathToWin32Environment)

# print("Available library:", find_library('libfunc.so'))


Lib_func = ctypes.cdll.LoadLibrary("F:/PythonCodeBase/NetEQ/libfunc.so")
# Lib_func = ctypes.CDLL(F:/PythonCodeBase/NetEQ/libfunc.so")
# Lib_func = ctypes.windll.LoadLibrary("libfunc.so")

Lib_func.func.argtypes(ctypes.c_int)
ret_val = Lib_func.func(num)

print(ret_val)

I have tried many times, either I'm giving the full Specified path or simple (.so) filename. Every time, I just got these types of errors.

- FileNotFoundError: Could not find module 'F:\PythonCodeBase\NetEQ\libfunc.so'. Try using the full path with constructor syntax.
- FileNotFoundError: Could not find module 'libfunc.so'. Try using the full path with constructor syntax.

I have done multiple attempts using PyCharm IDE as well as Command Prompt but the same error appears. Please assist me accordingly.

Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43
  • What's the environment you're using? Cause it looks like a *Win* machine, but details are *Nix* specific. – CristiFati Mar 31 '20 at 07:36
  • @CristiFati Yes. I am using the Win machine (Microsoft Windows 10 Pro). But I am compiling the *Shared Library* using [cygwin](https://www.cygwin.com/) on windows. – Muhammad Usman Bashir Mar 31 '20 at 07:43
  • Then try: ***/cygdrive/f/PythonCodeBase/NetEQ/libfunc.so*** (or *./libfunc.so* - relative). Also I assume that you launched the *Python* that comes with *Cigwin*, not some regular *Win* one. – CristiFati Mar 31 '20 at 07:55
  • @CristiFati, I have tried all of these paths: 1. `./libfunc.so`, 2. `/cygdrive/f/PythonCodeBase/NetEQ/libfunc.so`, 3. `F:\\PythonCodeBase\\NetEQ\\libfunc.so` . Apart from these, if my library is compiled then I think, after providing the library location, *Ctypes* should work on windows. As in the documentation of *ctypes*, they haven't told about something like use *the compiled platform related python stuff*. Secondly, I have tried this using cygwin python. But It didn't work as well. – Muhammad Usman Bashir Mar 31 '20 at 08:20
  • loool, just took a closer look. You name your file ***libfun.so***, but you're searching for *libfun**c**.so*. – CristiFati Mar 31 '20 at 08:25
  • @CristiFati, Just spelling mistake. *I have corrected it*. But fortunately, I am using *libfunc.so* and I also have compiled it with this name *libfunc.so*. – Muhammad Usman Bashir Mar 31 '20 at 08:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210656/discussion-between-muhammad-usman-and-cristifati). – Muhammad Usman Bashir Mar 31 '20 at 11:23

0 Answers0