我有一個完全用C語言編寫的程序,它使用多個對象(.o)
文件。這些文件全部打包在歸檔文件(.a)
內,而該文件又在程序的主要文件(.c)
的編譯時使用。在現有C項目中使用Go
我想在Go中爲這個項目編寫一個新文件。我的想法是寫這.go
文件,然後從它創建一個對象(.o)
文件。之後,我想把這個目標文件放在已經提到的檔案(.a)
文件中。
這基本上意味着我想從C程序調用Go函數。我讀過this question,雖然它告訴我,通過GCCGO我想要的是什麼,但如何做到這一點還不是100%清楚。
即使是最基本的測試,我在鏈接階段也會遇到錯誤。更具體地講,這裏的一些基本例子之一:
printString.go
package main
import
(
"fmt"
)
func PrintString(buff string) int {
fmt.Printf(buff)
return 1
}
c_caller.c
#define _GNU_SOURCE
#include <stdio.h>
extern int PrintString(char*) __asm__ ("print.main.PrintString");
int main() {
char *string_to_pass= NULL;
asprintf(&string_to_pass, "This is a test.");
int result= PrintString(string_to_pass);
if(result) {printf("Everything went as expected!\n");}
else {printf("Uh oh, something went wrong!\n");}
return result;
}
編譯
爲了編譯轉到文件,我用這個命令:
gccgo -c printString.go -o printString.o -fgo-prefix=print -Wall -Werror -march=native
爲了編譯整個事情,我用這個命令:
gccgo -o main c_caller.c printString.o -Wall -Werror -march=native
我得到的返回消息:
/usr/lib64/libgo.so.4.0.0: undefined reference to `main.main'
/usr/lib64/libgo.so.4.0.0: undefined reference to `__go_init_main'
collect2: error: ld returned 1 exit status
這意味着GCCGO期待Go文件中的主函數而不是C函數。
使用第二命令--static-libgo
,-static
和-Wl,-R,/path/to/libgo.so's_folder
選擇產生不同的結果:
/usr/bin/ld: cannot find -lgo
collect2: error: ld returned 1 exit status
這是沒有意義的,因爲我有LD_LIBRARY_PATH環境變量正確指向libgo.so的文件夾中。
我意識到我可能在這裏做錯了什麼,但我不知道那是什麼。沒有GCCGO的例子和它與C的交互的例子,我能找到的唯一參考是this page,我個人覺得這是不夠的。
我對此事提出了一些建議,並感謝您的時間。 :)
恥辱現在不會使用它,但很高興知道該選項將在不久的將來。感謝您的詳細解答! –