从python调用openMP共享库时,未定义opnMP函数

Undefined opnMP function when calling a openMP shared library from python

本文关键字:未定义 opnMP 函数 python 调用 openMP 共享      更新时间:2023-10-16

我想用openMP编写一个小的c++代码,将其编译为.so,并使用从python调用它。c++源是-

from ctypes import *
import ctypes
c_lib = cdll.LoadLibrary("libtest.so")
c_lib.openmp_test()

cpp文件如下-

#include<iostream>
#include<omp.h>
void openmp_test()
{
std::cout<<"in c++";
int threads = omp_get_max_threads();
std::cout<<threads;
}

我使用-创建.so文件

g++ -c -fPIC -fopenmp test.cpp -o test.o
g++ test.o -shared -o libtest.so

然而,运行python文件会出现错误-

undefined symbol: omp_get_max_threads

我做错了什么?提前感谢

您应该将库omp添加到程序的库依赖项中。

g++ test.o -shared -lomp  -o libtest.so

我为您提供完整的解决方案:

#include<iostream>
#include<omp.h>
extern "C"
{
void openmp_test()
{
std::cout<<"in c++";
int threads = omp_get_max_threads();
std::cout<<threads;
}
}

之后你应该与共享库

g++ -c -fPIC -fopenmp test.cpp -o test.o
g++ test.o -shared  -lomp -o libtest.so

然后运行您的python脚本,当然您应该提供库的完整路径。