作者owem0410 (閒學生)
看板C_and_CPP
標題[問題] cmake有library undefined reference
時間Mon Jul 29 02:35:46 2019
開發平台(Platform): (Ex: Win10, Linux, ...)
Docker(linux ubuntu 16.04)
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
Redis
問題(Question)D:
在一個openvino的smaple中
想要
#include <hiredis/hiredis.h>
但是卻跑出
main.cpp:(.text+0x691): undefined reference to `redisCommand'
main.cpp:(.text+0x699): undefined reference to `freeReplyObject'
上網查後應該是庫沒有export
所以在環境變數中
export LD_LIBRARY_PATH=/usr/local/lib/libhiredis:$LD_LIBRARY_PATH
也在export中確定有此資料夾
但狀況卻一樣
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
main.cpp:(.text+0x691): undefined reference to `redisCommand'
main.cpp:(.text+0x699): undefined reference to `freeReplyObject'
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
最上層的 CMakeLists
https://hackmd.io/@YlZanT8HShi_e7mTutiZ0g/r1bDHJ2fB
專案 cmakelist
https://hackmd.io/@YlZanT8HShi_e7mTutiZ0g/SJybuPozB
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.77.33.178 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1564338949.A.C80.html
推 LPH66: LD_LIBRARY_PATH 是執行期的東西, 你這是連結期錯誤 07/29 10:58
好的我用這個關鍵字查一下。
→ LPH66: 你可能要找你的編譯器/連結器裡尋找函式庫路徑的選項 07/29 10:58
→ LPH66: (印象中是 -L (大L) 之類的?) 07/29 10:59
推 LPH66: 喔, 是 cmake...有看到 link_directories(${LIB_FOLDER}) 07/29 11:05
→ LPH66: 但看起來好像沒有設定 LIB_FOLDER 變數的指令? 07/29 11:06
→ LPH66: 加個 set(LIB_FOLDER <連結函式庫所在目錄>) 試試 07/29 11:07
感謝回覆剛剛發現我貼錯cmakelist,所以修正抱歉。
※ 編輯: owem0410 (111.71.39.10 臺灣), 07/29/2019 11:21:59
※ 編輯: owem0410 (111.71.39.10 臺灣), 07/29/2019 11:42:54
推 LPH66: 最上層還是沒看到 LIB_FOLDER 07/29 12:51
→ Lipraxde: cmake 通常要用 target_link_libraries 去添加要連結的 07/29 22:43
→ Lipraxde: 庫,你不是找不到 library 是根本沒去 link 它吧 07/29 22:43
感謝兩位大大的回答
在專案的cmakelist中加入下面
我一開始用發現是找的到的所以就卡住了
find_library(hiredis /usr/local/lib/ )
後來加入target_link_libraries(${TARGET_NAME} hiredis)
就可以成功build
※ 編輯: owem0410 (203.77.33.178 臺灣), 07/29/2019 22:55:25