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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵