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


#include "test.h"

int main() {


package main

import "fmt"
import "C"

//export test
func 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
    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方
  • ¥15 关于生产日期批次退货退款,库存回退的问题
  • ¥15 手机应用的时间可以修改吗
  • ¥15 docker 运行OPEN-webui异常
  • ¥15 麒麟系统如何删除光盘刻录痕迹
  • ¥15 recipe通过gem协议传的是什么
  • ¥15 TS2307: Cannot find module 'cc'.
  • ¥15 100小时学会sap 书上pp章节5.22,标准成本计算逻辑?