如何将此共享库添加到我的CMakeLists.txt?

时间:2012-10-15 21:48:27

标签: cmake ubuntu-12.04 ros

我几乎阅读了每个链接教程的库,但没有一个能解决我的具体问题。

我有一个名为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);


}  

那么如何添加预编译库?

1 个答案:

答案 0 :(得分:1)

我根本不熟悉rosbuild,但以下几点可能有所帮助:

  • 将构建的exes和libs的输出路径更改为源树的一部分是不常见的。您可能想要删除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)