我正在尝试按照http://snowsyn.net/2016/09/11/creating-shared-libraries-in-go/
中的说明操作我的项目有点简单。该库有一个println测试功能。正如标题所说,我得到'找不到'。
我正在运行Ubuntu zesty并进入1.7.4
ls -l </ p>
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和test.so生成时使用:go build -o test.so -buildmode = c-shared test.go
调用gcc失败如下:
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
原始示例使用clang但googling表示调用也适用于gcc。
一些额外的评论:
go func test() {}
中的函数名称将以nm _test
显示在nm中,但应在C中声明为extern void test();
由于某种原因,调用go build -buildmode=c-shared
不会在OSX上生成头文件,但会在Linux上生成。
答案 0 :(得分:1)
请注意您所说的说明中的go build命令行之间的区别 你关注的是:
class ConcreteClass : KotlinAbstractClass() {
override val string: String
get() = BufferedReader(FileReader("file.txt")).readText()
...
}
和你自己的go build命令:
go build -o libimgutil.so -buildmode=c-shared imgutil.go
+++^^^^^^^^^^ ^^^^^^^^^^
根据the documentation of the linker
option -l | --library
go build -o test.so -buildmode=c-shared test.go
^^^^^^^ ^^^^^^^
这将向您显示您的go build命令必须是:
-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.) ...
答案 1 :(得分:0)
尝试gcc -o test test.c -L. -l:test.so
链接库。