簡體   English   中英

如何使用自制軟件、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命令。

要鏈接現有庫,您可以調用target_link_libraries

例子:

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

最好找到一個簡單的 CMake 設置並嘗試將其用作模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM