2014-04-24 78 views
1

我有這個錯誤,我收到的時候我嘗試編譯:CMake的:不能導入外部庫

Linking CXX executable /home/atv/catkin_ws/devel/lib/atvAcrosser/main** 
CMakeFiles/main.dir/src/sendToCAN.cpp.o: In function `sendCAN()': 
sendToCAN.cpp:(.text+0x432): undefined reference to `sendCanMessage(CanMsg*)' 
collect2: ld returned 1 exit status 
make[2]: *** [/home/atv/catkin_ws/devel/lib/atvAcrosser/main] Error 1 
make[1]: *** [atvAcrosser/CMakeFiles/main.dir/all] Error 2 
make: *** [all] Error 2 
Invoking "make" failed 

Main調用線程sendCAN [內sendToCAN.cpp]誰在外部庫libARV6005.a調用函數sendCanMessage

這是CMakeList.txt

include_directories(include ${catkin_INCLUDE_DIRS}) 
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/atvAcrosser/lib/) 
add_executable(main src/main.cpp src/setupPacketProtocol.cpp src/sendToCAN.cpp) 
target_link_libraries(main ${catkin_LIBRARIES} ARV6005) 

這是樹:

src 
    ├── atvAcrosser 
    │   ├── CMakeLists.txt 
    │   ├── include 
    │   │   └── atvAcrosser 
    │   │    ├── ARV6005Lib.h 
    │   │    ├── localPlannerCommunication.h 
    │   │    ├── receiveFromCAN.h 
    │   │    └── setupPacketProtocol.h 
    │   ├── lib 
    │   │   └── libARV6005.a 
    │   ├── package.xml 
    │   ├── src 
    │   │   ├── localPlannerCommunication.cpp 
    │   │   ├── main.cpp 
    │   │   ├── sendToCAN.cpp 
    │   │   ├── setupPacketProtocol.cpp 

sendToCAN.h

// included dependencies 
#include "ARV6005Lib.h" 
//================================= 
// function 
void sendCAN(); 

sendToCAN.cpp

#include "../include/atvAcrosser/sendToCAN.h" 
... 
void sendCAN() 
{ 
struct CanMsg msg; 
struct CanMsg msg; 
int i, result; 
memset((void *)&msg, 0, sizeof msg); 
msg.id = 0x33; 
msg.id_type = STD_ID; 
msg.length = 8; 
for(i=0; i<8; i++) { 
msg.data[i] = i; 
} 
result = sendCanMessage(&msg); 
} 

但這個Makefile的教程例如,它的工作原理

all: main.c 
    gcc -w -o TestUtility -D_REENTRANT -I../Library -pthread ./main.c ./libARV6005.a 
+0

儘量讓你的問題更容易理解。如果要理解你很難回答。 –

回答

0
  1. 檢查sendCanMessage(CanMsg*)ARV6005Lib.h
  2. 檢查宣稱libARV6005.a包含符號sendCanMessage
  3. 如果符號sendCanMessagelibARV6005.a,發現它在哪裏,並將它的可執行文件鏈接到它。
  4. 運行make VERBOSE=1,提取鏈接命令,並張貼在這裏
  5. 在你CMakeLists.txt你缺少_REENTRANTpthread。使用target_compile_definitions和target_link_libraries來添加它。
0

ARV6005Lib.a是用C編寫的。 sendToCAN.cpp是用Cpp編寫的。 所以足以 編輯,其中包括ARV6005Lib.h文件:

extern "C" { 
#include "../include/atvAcrosser/ARV6005Lib.h" 
}