登录
首页 >  Golang >  Go问答

cgo 调用共享库:找不到库或函数?

来源:stackoverflow

时间:2024-04-15 10:09:36 110浏览 收藏

哈喽!今天心血来潮给大家带来了《cgo 调用共享库:找不到库或函数?》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我使用go编程语言第13章的示例代码如下:

$ cat bzip2.c
#include 

int bz2compress(bz_stream *s, int action,
                char *in, unsigned *inlen, char *out, unsigned *outlen) {
    s->next_in = in;
    s->avail_in = *inlen;
    s->next_out = out;
    s->avail_out = *outlen;
    int r = bz2_bzcompress(s, action);
    *inlen -= s->avail_in;
    *outlen -= s->avail_out;
    s->next_in = s->next_out = null;
    return r;
}

$ cat usebzip2.go
// package bzip provides a writer that uses bzip2 compression (bzip.org).
package main
import "c"

import (
    "io"
    "log"
    "os"
    "testing"
    "unsafe"
)

type writer struct {
    w      io.writer // underlying output stream
    stream *c.bz_stream
    outbuf [64 * 1024]byte
}

// close flushes the compressed data and closes the stream.
// it does not close the underlying io.writer.
func (w *writer) close() error {
    if w.stream == nil {
        panic("closed")
    }
    defer func() {
        c.bz2_bzcompressend(w.stream)
        c.bz2free(w.stream)
        w.stream = nil
    }()
    for {
        inlen, outlen := c.uint(0), c.uint(cap(w.outbuf))
        r := c.bz2compress(w.stream, c.bz_finish, nil, &inlen,
            (*c.char)(unsafe.pointer(&w.outbuf)), &outlen)
        if _, err := w.w.write(w.outbuf[:outlen]); err != nil {
            return err
        }
        if r == c.bz_stream_end {
            return nil
        }
    }
}

// newwriter returns a writer for bzip2-compressed streams.
func newwriter(out io.writer) io.writecloser {
    const blocksize = 9
    const verbosity = 0
    const workfactor = 30
    w := &writer{w: out, stream: c.bz2alloc()}
    c.bz2_bzcompressinit(w.stream, blocksize, verbosity, workfactor)
    return w
}

func main() {
    w := newwriter(os.stdout)
    if _, err := io.copy(w, os.stdin); err != nil {
        log.fatalf("bzipper: %v\n", err)
    }
    if err := w.close(); err != nil {
        log.fatalf("bzipper: close: %v\n", err)
    }
}

首先我编译 .c 文件:

gcc -i/usr/include -l/usr/lib -lbz2 --shared bzip2.c -fpic -o libbzip2.so

linux环境ld_library_path包含“.”,然后go build失败:

go build usebzip2.go
# command-line-arguments
/tmp/go-build677611698/b001/_x002.o: In function `_cgo_22d5d7fabfe4_Cfunc_bz2compress':
/tmp/go-build/cgo-gcc-prolog:118: undefined reference to `bz2compress'
collect2: error: ld returned 1 exit status

那么如何解决呢?我正在使用 ubuntu 18.04 lts。多谢。


解决方案


不要运行:

go build usebzip2.go

而是:

go build

(并且您不需要直接在 bzip2.c 上调用 gcc)。当您使用此过程时,您会收到更多(但不同)的错误,因为您没有在以下位置之前输入正确的指令:

import "c"

线。您需要一条注释(或一系列注释)告诉 cgo 您打算提供的功能,或内联提供这些功能,并指示链接阶段使用 -lbz2。特别是,您需要:

  • #include
  • 提供bz2alloc函数
  • 提供bz2free函数
  • 为您的 bz2compress 函数提供声明
  • ldflags 设置为包含 -lbz2

实际的 bz2allocbz2free 简短且简单,因此可以直接包含在此标头块中:

package main

/*
#cgo ldflags: -lbz2     
#include 
#include 
bz_stream *bz2alloc() { return calloc(1, sizeof(bz_stream)); }
int bz2compress(bz_stream *s, int action,
    char *in, unsigned *intlen, char *out, unsigned *outlen);
void bz2free(bz_stream* s) { free(s); }
*/      
import "c"

如果您插入此内容并运行 go build 您现在将看到一个不同且更有用的错误:

./usebzip2.go:60:2: cannot use w (type *writer) as type io.WriteCloser in return argument:
        *writer does not implement io.WriteCloser (missing Write method)

这当然是因为 type writer 没有实现 write

(练习 13.3 的完整版本(不是我的)位于 https://github.com/torbiak/gopl/tree/master/ex13.3。请注意,他们还增强了其版本以使用锁定,从而可以安全地同时从多个 goroutine 调用 write 函数。)

今天关于《cgo 调用共享库:找不到库或函数?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>