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条)

报告相同问题?

悬赏问题

  • ¥20 管道轴向耦合水击问题
  • ¥60 补全networkx TODO部分。
  • ¥15 有内推吗,云计算linux运维方向
  • ¥30 sort cuteSV.vcf by bcftools用IGV可视化出现报错
  • ¥100 SOS!对STK中导出的天体图像进行质心提取有没有人做过啊
  • ¥15 python 欧式距离
  • ¥15 运行qteasy报错
  • ¥15 遗传算法解决有工序顺序约束的大规模FJSP问题
  • ¥15 企业消防水炮塔设计方案
  • ¥20 WORKBENCH网格划分