[問題] CUDA: 無法解析的外部符號 _cuda_sample

看板C_and_CPP (C/C++)作者 (良)時間13年前 (2013/05/06 22:30), 編輯推噓0(007)
留言7則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 2010 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) CUDA 5.0 問題(Question): 剛接觸CUDA不久,想建立一個簡單的程式,模擬在 .c file中的main能呼叫.cu內的 vector add function。 main裡面只有一個printf和一個被呼叫的function,該function是CUDA的範例。 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): >C_FILE.obj : error LNK2019: 無法解析的外部符號 _cuda_sample 在函式 _main 中被 參考 >C:\Users\Yo\Desktop\D\R2\Debug\R2.exe : fatal error LNK1120: 1 個無法解析的 外部符號 程式碼(Code):(請善用置底文網頁, 記得排版) C_FILE.c: #include <stdio.h> #include "CUDA_cowork.h" int cuda_sample(); void main() {  printf("Cowork is correct. \n");         cuda_sample();           }                                ======================================================= CUDA_cowork.h: int cuda_sample(); 補充說明(Supplement): 搜尋error LNK2019:無法解析的外部符號,有提到更改Linker->Subsystem, 我試過改成Console 和 Windows都無法解決問題。 而且同樣的程式碼若將 .c 改成 .cpp 就能順利編譯並執行, 為何無法在 .c 檔案下執行,這點我很困惑!! 麻煩大家了,謝謝!!                                 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.187.150

05/07 01:02, , 1F
因為cu是用cpp編譯器,而這方面你必須使用extern "C"宣告
05/07 01:02, 1F

05/07 21:46, , 2F
我看了一下extern C, 是當main寫在cpp時引用寫在.c的涵式
05/07 21:46, 2F

05/07 21:46, , 3F
時要加入
05/07 21:46, 3F

05/07 21:47, , 4F
那如果把main寫在.c裡面, 由於cu是用cpp的編譯器
05/07 21:47, 4F

05/07 21:48, , 5F
不就不能使用extern C, 也不能讓CUDA和.c file 一起編譯?
05/07 21:48, 5F

05/07 23:18, , 6F
不是這個意思
05/07 23:18, 6F

05/07 23:21, , 7F
extern "C"用於c++,讓該符號/函式用c的規則出現在obj檔內
05/07 23:21, 7F
我懂了!!! 非常感謝你!!! ※ 編輯: bagaohya 來自: 140.116.187.150 (05/09 13:42)
文章代碼(AID): #1HXxwFd4 (C_and_CPP)
文章代碼(AID): #1HXxwFd4 (C_and_CPP)