[問題] 用makefile編譯包含CUDA的動態連結(.so)

看板C_and_CPP作者 (笨小孩)時間7年前 (2018/02/01 21:46), 7年前編輯推噓2(208)
留言10則, 3人參與, 7年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Unbuntu 16 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) g++ 5.4 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) CUDA: 8 openCV:3.2 問題(Question): 想將自己的函式編譯成動態連結給別人使用 在win10 已成功產生其.dll並使用 by vs2017 但在ubuntu上make時卻出現錯誤結果 此時如果在.cpp 加個main 輸出程執行檔就不會有問題 懇請各位大大指點一下小弟makefile在輸出.so時做錯了哪步 餵入的資料(Input): .cpp .cu .h檔 預期的正確結果(Expected Output): 成功產生 pdda.so 錯誤結果(Wrong Output): /usr/bin/ld: ./pdda_cuda.o: relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC ./pdda_cuda.o: error adding symbols: Bad value collect2: error: ld returned 1 exit status 程式碼(Code):(請善用置底文網頁, 記得排版) https://gist.github.com/ChiFang/70b6ee3ac2d3266373706ac16ff90345 pdda.so: pdda_cuda.o g++ -fPIC -shared -o pdda.so ./src/pdda.cpp ./pdda_cuda.o -I ./src/ -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcuda -lcudart `pkg-config --libs opencv` pdda_cuda.o: nvcc -c ./src/pdda_cuda.cu -o pdda_cuda.o clean: 補充說明(Supplement): nvcc -c ./src/pdda_cuda.cu -o pdda_cuda.o >> 可執行 .o檔也有產生 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.132.89.186 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1517492778.A.4E0.html ※ 編輯: hardman1110 (220.132.89.186), 02/01/2018 21:47:30

02/01 22:14, 7年前 , 1F
pdda_cuda.o 也補個 -fPIC
02/01 22:14, 1F

02/01 22:27, 7年前 , 2F
請問大大是要加在哪部份?
02/01 22:27, 2F

02/01 22:28, 7年前 , 3F
nvcc 不支援-fPIC
02/01 22:28, 3F

02/01 22:36, 7年前 , 4F
如果是加在 .pdda_cuda.o前則是出現一樣錯誤
02/01 22:36, 4F

02/01 23:11, 7年前 , 5F
稍微咕狗了一下看到這個
02/01 23:11, 5F

02/01 23:11, 7年前 , 6F

02/01 23:17, 7年前 , 7F
L大~ 所以我得用nvcc 把.cu編成.so再用g++將其跟.cpp
02/01 23:17, 7F

02/01 23:17, 7年前 , 8F
編成最後的pdda.so?
02/01 23:17, 8F

02/01 23:52, 7年前 , 9F
我不確定喔, 你可能要參照其他資料例如 nvcc 的說明等
02/01 23:52, 9F

02/02 10:56, 7年前 , 10F
謝謝L大 在nvcc加入-Xcompiler '-fPIC' -dc即可~~~
02/02 10:56, 10F
文章代碼(AID): #1QSnegJW (C_and_CPP)