doume1301 2019-07-31 18:11
浏览 274
已采纳

导入cython生成的c共享库以与cgo一起使用

I wanna import a c-shared-library to go that generated by Cython in python 3.7, try do it by cgo.

in this case:

go version go1.12.7 linux/amd64

Python 3.7.3

Cython version 0.29.12

os: Manjaro 18.0.4

Kernel: x86_64 Linux 5.1.19-1

I will continue: make a python file vim pylib.pyx:

#!python
cdef public void hello():
     print("hello world!")

and run python -m cython pylib.pyx for generate the c-shared-library, I have two files, pylib.c and pylib.h. now, try import these to golang, so make a go file vim test.go:

package main

/*
#include </usr/include/python3.7m/Python.h>
#include "pylib.h"
*/
import "C"
import "fmt"

func main() {
   C.hello()
   fmt.Println("done")
}

finaly, I run go run test.go: I have the following output:

# command-line-arguments
/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_51159acd5c8e_Cfunc_hello':
/tmp/go-build/cgo-gcc-prolog:48: undefined reference to `hello'
collect2: error: ld returned 1 exit status

I try import it to c too but I encountered a similar output like this:

undefined reference to `hello'
ld returned 1 exit status

I don't know what to do, help me, please. :(

  • 写回答

1条回答 默认 最新

  • doujia1988 2019-07-31 20:50
    关注

    I run go run test.go: I have the following output:

    # command-line-arguments
    /usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_51159acd5c8e_Cfunc_hello':
    /tmp/go-build/cgo-gcc-prolog:48: undefined reference to `hello'
    collect2: error: ld returned 1 exit status
    

    We can generate an equivalent error message with the following code.

    package main
    
    /*
    #include <math.h>
    */
    import "C"
    import "fmt"
    
    func main() {
        cube2 := C.pow(2.0, 3.0)
        fmt.Println(cube2)
    }
    

    Output:

    $ go run cube2.go
    # command-line-arguments
    /usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_f6c6fa139eda_Cfunc_pow':
    /tmp/go-build/cgo-gcc-prolog:53: undefined reference to `pow'
    collect2: error: ld returned 1 exit status
    $ 
    

    In both cases, ld (the linker) can't find a C function after looking in the usual places: undefined reference to 'pow' or undefined reference to 'hello'.

    Let's tell cgo where to find the C pow function in the C math library: m.

    For cgo, using ld flags,

    #cgo LDFLAGS: -lm
    

    GCC: 3.14 Options for Linking

    -llibrary
        Search the library named library when linking.
    

    Updating the previous code,

    package main
    
    /*
    #cgo LDFLAGS: -lm
    #include <math.h>
    */
    import "C"
    import "fmt"
    
    func main() {
        cube2 := C.pow(2.0, 3.0)
        fmt.Println(cube2)
    }
    

    Output:

    $ go run cube2.go
    8
    $
    

    This illustrates a basic cgo principle: include a C header file for your C library and point to the location of the C library.


    References:

    Cgo and Python : Embedding CPython: a primer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调