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 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题