golang 과 C 는 서로 예제 를 호출 합 니 다.
1603 단어 기타
1.c 언어 호출 golang 라 이브 러 리
1.main.go
package main
import "C"
func main() {}
//export Hello
func Hello() string {
return "Hello"
}
//export Test
func Test() {
println("export Test")
}
동적 라 이브 러 리 생 성 명령
go build -buildmode=c-shared -o libhello.so main.go
a 정적 라 이브 러 리 생 성
# libhello.a
go build -buildmode=c-archive -o libhello.a main.go
go 컴 파일 러 는 libhello.h 파일 을 자동 으로 생 성 합 니 다.
2.main.c
#include
#include "libhello.h"
void main()
{
GoString str;
str = Hello();
Test();
printf("%d
",str.n);
}
컴 파일 명령
gcc main.c -o t1 -I./ -L./ -lhello -lpthread
# $LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
#
./t1
2.golang 호출 c 라 이브 러 리
1.toto.h
int x(int);
2.toto.c
int x( int y ) { return y+1; }
컴 파일 명령
# .a
gcc -O2 -c toto.c
ar q libgb.a toto.o
# .so
gcc -shared -o libgb.so toto.c
3.test.go
package main
import "fmt"
// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -L. -lgb
// #include
import "C"
func main() {
fmt.Printf("Invoking c library...
")
fmt.Println("Done ", C.x(10) )
}
명령:
go build test.go
./test #
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Github 프로필 Badge로 꾸미기앞으로 열심히 할 예정이니 많은 관심 부탁드립니다 😍😍 나의 머리로는 이해할 수 없는 코드들을 보며 머리가 꺠지려 할 때에, 학교 친구들의 github가 눈에 들어왔습니다. 인터넷에 나온 거로 많이 해 봤고 탬플릿도...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.