dqyknf4423 2017-12-10 07:17
浏览 352
已采纳

Linux上的Golang C共享-ld找不到-ltest

I'm trying to follow the instructions in http://snowsyn.net/2016/09/11/creating-shared-libraries-in-go/

My project is somewhat simpler. The library has one test function with println. As the title says, I'm getting 'cannot find'.

I'm running Ubuntu zesty and go 1.7.4

ls -l

roy@roy-desktop:~/go/src/c$ ls -l total 2016 -rw-rw-r-- 1 roy roy 43 Dec 10 06:55 test.c -rw-rw-r-- 1 roy roy 1274 Dec 10 06:54 test.h -rw-rw-r-- 1 roy roy 2053664 Dec 10 06:54 test.so

test.c

#include "test.h"

int main() {
    test();
}

lib.go

package main

import "fmt"
import "C"


//export test
func test() {
    fmt.Println("test")
}

func main() {}

test.h and test.so were generated with: go build -o test.so -buildmode=c-shared test.go

invocation of gcc fails as follows:

roy@roy-desktop:~/go/src/c$ gcc -o test test.c -L. -ltest
/usr/bin/ld: cannot find -ltest
collect2: error: ld returned 1 exit status

The original example uses clang but googling indicates the invocation should work for gcc as well.

Post Solution

A couple of extra comments:

  1. A function name in go func test() {} will appear in nm as _test but should be declared in C as extern void test();

  2. For some reason, the invocation go build -buildmode=c-shared does not generate a header file on OSX but does on Linux.

  • 写回答

2条回答 默认 最新

  • dongsui8162 2017-12-10 09:46
    关注

    Note the difference between the go build commandline in the instructions you say you are following:

    go build -o libimgutil.so -buildmode=c-shared imgutil.go
                +++^^^^^^^^^^                     ^^^^^^^^^^ 
    

    and your own go build command:

    go build -o test.so -buildmode=c-shared test.go
                ^^^^^^^                     ^^^^^^^
    

    Consider this difference in the light of the documentation of the linker option -l | --library

    -l namespec
    --library=namespec
    
    Add the archive or object file specified by namespec to the list of files to link.
                                                ^^^^^^^^
    This option may be used any number of times. If namespec is of the form :filename,
                                                    ^^^^^^^^                +^^^^^^^^
    ld will search the library path for a file called filename, otherwise it will
                                                      ^^^^^^^^
    search the library path for a file called libnamespec.a.
                                              +++^^^^^^^^++
    On ... ELF and SunOS systems, ld will search a directory for a library called
    libnamespec.so before searching for one called libnamespec.a. (By convention,
    +++^^^^^^^^+++                                 +++^^^^^^^^++
    a .so extension indicates a shared library.) ...
    

    This will show you that your go build command needs to be:

    go build -o libtest.so -buildmode=c-shared test.go
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行