如何使用自制软件、cMake 和 cLion 将库添加到 C++ 项目

How to add library to C++ Project using homebrew, cMake and, cLion

我真的是C++新手,有点迷茫。

我正在尝试将此库添加到我的项目中 (https://github.com/mrtazz/restclient-cpp)。 我已经使用 Homebrew

安装了它
brew tap mrtazz/oss
brew install restclient-cpp

然后我尝试通过包含和链接 Homebrew 安装目录将库添加到我的 CMakeLists。

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(POS)

set(CMAKE_CXX_STANDARD 14)

include_directories(/usr/local/include)
link_directories(/usr/local/lib)

add_library(
        restclient-cpp STATIC
        connection.h
        helpers.h
        restclient.h
        version.h
)

add_executable(POS main.cpp program.cpp program.h programs/find.cpp programs/find.h tools/db.cpp tools/db.h)

target_link_libraries(POS PUBLIC restclient-cpp)

然后我得到这个错误...

CMake Error at CMakeLists.txt:16 (add_library):
  Cannot find source file:

    connection.h

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


CMake Error at CMakeLists.txt:16 (add_library):
  No SOURCES given to target: restclient-cpp

我知道目录有问题,但我就是想不通,如果能提供更多信息,我将不胜感激。我只是想找点乐子,但我不明白为什么我不能将这个简单的库添加到我的构建中。

谢谢。

您正在尝试将头文件添加到 add_library 命令。这些文件需要位于您通过 include_directory 包含的目录中。您也不应该将头文件放入 add_executable 命令中。

要 link 一个现有的库,您可以调用 target_link_libraries

示例:

include_directories(${MY_INCLUDE_DIRS})
add_executable(main source.cpp)
target_link_libraries(main extlib)

最好找到一个 simple CMake setup 并尝试将其用作模板。