我几乎阅读了每个链接教程的库,但没有一个能解决我的具体问题。
我有一个名为VSCOM_API_TEST的文件夹,其中包含以下文件和文件夹:
VSCOM_API_TEST/CMakeLists.txt
VSCOM_API_TEST/include/vs_can_api.h
VSCOM_API_TEST/include/startup.h
VSCOM_API_TEST/lib/libvs_can_api.so
VSCOM_API_TEST/lib/libvs_can_api.a
VSCOM_API_TEST/src/startup.cpp
startup是我的可执行文件,我想要包含库。
我遇到的问题是我的程序无法找到库函数:
CMakeFiles/startup.dir/src/startup.o: In function main:
/home/max/fuerte_workspace/sandbox/VSCOM_API_TEST/src/startup.cpp:6: undefined reference to VSCAN_Open
CMakeLists.txt
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
#common commands for building c++ executables and libraries
rosbuild_add_library(${PROJECT_NAME} lib/libvs_can_api.a)
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C)
rosbuild_add_executable(startup src/startup.cpp)
#target_link_libraries(startup ${PROJECT_NAME})
startup.cpp
// ROS includes
#include <ros/ros.h>
#include <vs_can_api.h>
int main(int argc, char** argv)
{
VSCAN_HANDLE handle = VSCAN_Open("192.168.5.10:23", VSCAN_MODE_NORMAL);
VSCAN_API_VERSION *version;
VSCAN_STATUS status;
VSCAN_HWPARAM *hwParams;
// API Version
status = VSCAN_Ioctl(handle, VSCAN_IOCTL_GET_API_VERSION, &version);
//HW Version
status = VSCAN_Ioctl(handle, VSCAN_IOCTL_GET_HWPARAM, &hwParams);
}
那么如何添加预编译库?
答案 0 :(得分:1)
我根本不熟悉rosbuild,但以下几点可能有所帮助:
set(EXECUTABLE/LIBRARY_ ...)
行。rosbuild_add_library
是CMake的add_library
命令的包装器,不打算与编译库一起使用。相反,它将用于定义创建库所需的源。此行也应删除。LINKER_LANGUAGE
设置为C
,无论如何都应该CXX
。target_link_libraries
是您要用于链接到已编译库的命令。但是,您需要提供库的完整路径作为第二个参数。所以(未经测试)我建议您尝试以下CMakelists.txt
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()
rosbuild_add_executable(startup src/startup.cpp)
target_link_libraries(startup ${CMAKE_CURRENT_LIST_DIR}/lib/libvs_can_api.a)