CMake:导入外部库失败

时间:2014-04-24 12:36:46

标签: reference cmake undefined .a

我在尝试编译时收到此错误:

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}内部调用外部库sendCanMessage中的函数libARV6005.a。{/ p>

这是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

2 个答案:

答案 0 :(得分:0)

  1. 检查sendCanMessage(CanMsg*)
  2. 中是否已声明ARV6005Lib.h
  3. 检查libARV6005.a是否包含符号sendCanMessage
  4. 如果符号sendCanMessage不在libARV6005.a中,请找到它的位置并将可执行文件与其链接。
  5. 运行make VERBOSE=1,解压缩链接命令并在此处发布
  6. 在您的CMakeLists.txt中,您遗失了_REENTRANTpthread。使用target_compile_definitions和target_link_libraries添加它。

答案 1 :(得分:0)

ARV6005Lib.a是用C语言编写的。 sendToCAN.cpp是用Cpp编写的。 所以就足够了 编辑包含ARV6005Lib.h的文件:

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