我正在嘗試使用cuda來并行化Go項目。我讀過Golang多次打電話給CUDA圖書館。我試圖在Windows上做同樣的事情,但遇到了麻煩。(我假設這個OP是因為文件而使用的是Linux).so我已經成功編譯/運行了以下涉及cuda代碼的測試程序,以確保我的CGO正常工作。測試.cppextern "C"{ int testfunc(){ return 1000; }}使用 g++ 編譯 g++ test.cpp -o test.dll --sharedcallC.gopackage main//int testfunc();//#cgo LDFLAGS: -L. -ltestimport "C"import "fmt"func main() { fmt.Println(C.testfunc())}打印 1000 - 太好了!現在我嘗試同樣的事情,我從ld那里得到一個錯誤,有一個未定義的引用到testfunc。nvcctest.cuextern "C"{ int testfunc() { return 1 + 1; }}編譯與 ...我總是得到:nvcc test.cu -o testcuda.dll --shared --compiler-options -fPICcl : Command line warning D9002 : ignoring unknown option '-fPIC'cl : Command line warning D9002 : ignoring unknown option '-fPIC'cl : Command line warning D9002 : ignoring unknown option '-fPIC'Creating library testcuda.lib and object testcuda.expcallCudaC.gopackage main//int testfunc();//#cgo LDFLAGS: -L. -ltestcuda//#cgo LDFLAGS: -LC:\temppath\CUDA\v11.2\ -lcudartimport "C"import "fmt"func main() { fmt.Println(C.testfunc())}運行此 go 代碼的結果 C:\Users\MICHAE~1\AppData\Local\Temp\go-build045526290\b001\_x002.o: In function `_cgo_701f531a6502_Cfunc_testfunc': /tmp/go-build/cgo-gcc-prolog:52: undefined reference to `testfunc' collect2.exe: error: ld returned 1 exit status你會注意到我的“cuda”代碼不涉及任何cuda,只涉及一個導出的函數,并且我嘗試將cuda安裝移動到一個更簡單的路徑,所以我實際上可以將其作為目錄傳遞給鏈接器 - 我不確定它是否需要,因為沒有使用cuda語法等。如果有人以前見過這個,將不勝感激調試提示或提示。
1 回答

米脂
TA貢獻1836條經驗 獲得超3個贊
多虧了@talonmies注釋的指示,我發現至少在簡單的情況下,我可以通過定義一個看起來像這樣的頭文件來調用cl.exe和nvcc.exe從cgo創建的dll:
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif
__declspec(dllexport) int testfunc();
#ifdef __cplusplus
}
#endif
此代碼是引用以下兩篇 MSDN 文章創建的:https://docs.microsoft.com/en-us/cpp/build/exporting-cpp-functions-for-use-in-c-language-executables?view=msvc-160
https://docs.microsoft.com/en-us/cpp/build/exporting-c-functions-for-use-in-c-or-cpp-language-executables?view=msvc-160
- 1 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消