c++ - Cmake,在/usr/local/include 中找到头文件,在/usr/local/lib 中找到库

标签 c++ cmake

我希望 CMake 找到 add_executable 的 header 并找到 target_link_libraries 的 .so 文件。

我要找的头文件是lcm-cpp.hpp(在ubunthu上)

ls /usr/local/include/lcm/
eventlog.h  lcm_coretypes.h  lcm-cpp.hpp  lcm-cpp-impl.hpp  lcm.h

我项目根目录下的CMakeLists.txt文件

 cmake_minimum_required (VERSION 2.6)
 project (libFoo)
 include_directories(include /usr/local/include/lcm/)

 set(PROJECT_SRC
     src/Foo.cpp )

 set(PROJECT_H
     include/Foo.hpp )

 find_library(LCM_LIBRARY
     NAMES liblcm.so
     PATHS
     /usr/local/lib/
 )

add_library(liblcm STATIC IMPORTED)

add_library(foo_lib ${PROJECT_SRC} ${PROJECT_H})

add_executable(foo_lcm src/lcm_foo.cpp ${PROJECT_H} lcm-cpp.hpp)

我得到的错误:

  Cannot find source file:

  lcm-cpp.hpp

 Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
 .hxx .in .txx

最佳答案

CMake 命令 include_directories() 用于指定附加目录,编译器 应在其中搜索 #included 文件。完全不影响CMake对源文件的搜索。

如果文件 /usr/local/include/lcm/lcm-cpp.hpp 确实是您的可执行文件的一部分(例如,您希望它列在 Visual Studio 的项目中),您必须使用完整路径指定它:

add_executable(foo_lcm src/lcm_foo.cpp ${PROJECT_H} /usr/local/include/lcm/lcm-cpp.hpp)

但是,根据其位置,它看起来更像是可执行文件外部的库。如果是这种情况,它根本不应该列在 add_executable() 中。

关于c++ - Cmake,在/usr/local/include 中找到头文件,在/usr/local/lib 中找到库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053629/

相关文章:

c++ - #define 字符串转换为 LPCSTR 或 std::string

c++ - 在 Qt 中使用 SOIL 库时出错

c++ - QTcpSocket 为什么接收到错误的数据?

c++ - CMake 无法确定目标 : azurestorage error 的链接器语言

linux - github 上使用 cmake 共享库版本控制

c++ - 验证 STACK_OF(X509) 中的证书链

c++ - 混淆右值和左值

c++ - 如何指定编译器和编译不同的主要函数

directory - 如何使用 cmake "install directory"指令保留文件权限?

c++ - 如何使 CMake 输出到 'bin' 目录?