我试图在Python中使用go
函数。我正在使用标准的cgo
方法和pybind11
相结合,但无法使其工作。我正在利用这个demo,它可以独立工作,但在尝试链接go
代码时却不行。同样地,我可以从C调用go
代码,但不能从Python调用。你知道吗
更新
我已经确认,这个问题肯定是在链接步骤中使用的共享库不是从Go编译的。该错误很可能出现在共享库定义或CMake文件设置中。你知道吗
我在go
中有以下文件:
package main
import "C"
//export Sum
func Sum(a, b int) int {
return a + b
}
func main() {}
我正在编译成一个C共享库hello.so
,通过:
go build -buildmode=c-shared -o hello.so hello.go
然后添加C++文件^ {CD9>}:
#include <pybind11/pybind11.h>
#include "hello.h"
int add(int i, int j) {
long long other_sum = Sum(2, 3);
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(example, m) {
// optional module docstring
m.doc() = "pybind11 example plugin";
// define add function
m.def("add", &add, "A function which adds two numbers");
}
然后,将pybind11
的完整源代码放入名为pybind11
的文件夹中,运行以下操作:
mkdir build && cd build && cmake .. && make
它成功地在build
文件夹中创建了一个Python库。你知道吗
但是,当调用Python CLI cd build && python3
,然后用from example import add
导入add
时,我遇到了以下问题:
ImportError: Symbol not found: _Sum
这看起来像是一个库链接问题,但它不会随着以下CMakeLists.txt
配置而消失:
cmake_minimum_required(VERSION 2.8.12)
project(example)
find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_DIRS})
add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
# first attempt to link
include_directories(.)
link_directories($PWD)
# second attempt to link
add_library(hello SHARED IMPORTED)
set_property(TARGET hello PROPERTY IMPORTED_LOCATION "./hello.so")
你有没有想过为什么链接会失败?你知道吗
更新
hello.h
的代码:
/* Code generated by cmd/cgo; DO NOT EDIT. */
/* package command-line-arguments */
#line 1 "cgo-builtin-export-prolog"
#include <stddef.h> /* for ptrdiff_t below */
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
#endif
/* Start of preamble from import "C" comments. */
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern GoInt Sum(GoInt p0, GoInt p1);
#ifdef __cplusplus
}
#endif
目前没有回答
相关问题 更多 >
编程相关推荐