doujie7497 2016-07-05 11:51
浏览 351

cgo(golang):错误:未定义对'hello'的引用

I just write a very simple demo to test load the shared library with cgo(golang).the code as follow:

xxx.h

#pragma once
void myprint(const char *str);

xxx.c

#include "xxx.h"
#include <stdio.h>
void myprint(const char *str) {
    printf("%s
",str);
}

build shared library:

gcc -fPIC -shared xxx.c -o libxxx.so

ok,everything is ok from here.

now ,use the cgo load the libxxx.so,and use the myprint function:

package main

/*
#include <stdio.h>
#cgo linux CFLAGS: -I../../include
#cgo linux LDFLAGS: -L../../lib/linux -lxxx
#include "xxx.h"
*/
import "C"

funct main() {
    C.myprint(C.CString("xxx"))
}

then, build the go demo:

go build test.go

as my title show:

error: undefined reference to 'myprint'

i ensure the path of lib/head file is right, Who can help me find the reason? thx.

  • 写回答

1条回答 默认 最新

  • duanfei7508 2016-07-05 13:09
    关注

    Relative paths don't work in the build context, because the build happens in a different directory from your source files.

    You have a few choices to provide absolute paths:

    • You can use absolute paths in your source
    • You can use pkg-config to provide absolute paths
    • You can use the CGO_CFLAGS and CGO_LDFLAGS environment variables
    • You can use the ${SRCDIR} variable in the #cgo lines in your source.

    See the cgo documentation for more details

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题