作者hardman1110 (笨小孩)
看板C_and_CPP
標題[問題] 用makefile編譯包含CUDA的動態連結(.so)
時間Thu Feb 1 21:46:14 2018
開發平台(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
推 Bencrie: pdda_cuda.o 也補個 -fPIC 02/01 22:14
→ hardman1110: 請問大大是要加在哪部份? 02/01 22:27
→ hardman1110: nvcc 不支援-fPIC 02/01 22:28
→ hardman1110: 如果是加在 .pdda_cuda.o前則是出現一樣錯誤 02/01 22:36
推 LPH66: 稍微咕狗了一下看到這個 02/01 23:11
→ hardman1110: L大~ 所以我得用nvcc 把.cu編成.so再用g++將其跟.cpp 02/01 23:17
→ hardman1110: 編成最後的pdda.so? 02/01 23:17
→ LPH66: 我不確定喔, 你可能要參照其他資料例如 nvcc 的說明等 02/01 23:52
→ hardman1110: 謝謝L大 在nvcc加入-Xcompiler '-fPIC' -dc即可~~~ 02/02 10:56