登录
首页 >  Golang >  Go问答

解决cgo中的重复定义问题

来源:stackoverflow

时间:2024-03-28 14:57:22 294浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《解决cgo中的重复定义问题》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

package main

/*
int add(int a, int b) {
    return a + b;
}
*/
import "c"
import "fmt"

func main() {}

func test1() {
    fmt.println(c.add(1, 3))
}

//export test2
func test2() {

}

编译程序:

dingrui@dingrui-PC:~/Projects/gotest/array$ go build -o libtest.so -buildmode=c-shared main.go 
# command-line-arguments
/tmp/go-build043762604/b001/_x002.o: In function `add':
./main.go:5: multiple definition of `add'
/tmp/go-build043762604/b001/_x001.o:/tmp/go-build/main.go:5: first defined here
collect2: error: ld returned 1 exit status

如果我删除“//export test2”行,则编译成功。


解决方案


此行为已记录在案 here

到这里,我们也就讲完了《解决cgo中的重复定义问题》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>