简体   繁体   中英

Using CFFI to call C-function with OpenMP from Python

I am using CFFI to call a C function with OpenMP from Python. My code works on one of my computers, but not the other with a very similar setup.

import os
from cffi import FFI

# test 
os.system("gcc -fopenmp -c test.c -o test.o")
os.system("gcc -o test.exe test.o -fopenmp")
os.system("test.exe")

# gateway
ffi = FFI()
os.system("gcc -o test.so test.c -shared -fopenmp")
ffi.cdef(r''' int main(); ''')        
lib = ffi.dlopen(r'''test.so''')
lib.main()

The error is

OSError: cannot load library test.so: error 0x45a

I am using Python 3.5 (the latest Anaconda distribution) and TDM-GCC 5.1.0. The test runs on both computers. What can explain the difference behavior?

Short version - you define your header (.h) and source files (.c) as usual with cffi. For openmp you have to include extra compile and linker arguments as follows:

import cffi

ffi = cffi.FFI()    
ffi.cdef(header_string)    
ffi.set_source(
    '_my_module_name',
    source_string,
    extra_compile_args=['-fopenmp'],
    extra_link_args=['-fopenmp'],
)
ffi.compile()
import _my_module_name

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