我在尝试编译时收到此错误:
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
答案 0 :(得分:0)
sendCanMessage(CanMsg*)
ARV6005Lib.h
libARV6005.a
是否包含符号sendCanMessage
sendCanMessage
不在libARV6005.a
中,请找到它的位置并将可执行文件与其链接。make VERBOSE=1
,解压缩链接命令并在此处发布CMakeLists.txt
中,您遗失了_REENTRANT
和pthread
。使用target_compile_definitions和target_link_libraries添加它。答案 1 :(得分:0)
ARV6005Lib.a是用C语言编写的。 sendToCAN.cpp是用Cpp编写的。 所以就足够了 编辑包含ARV6005Lib.h的文件:
extern "C" {
#include "../include/atvAcrosser/ARV6005Lib.h"
}