簡體   English   中英

無法使用 CMake 中的 git 子模塊添加外部庫

[英]Not able to add external library using git submodules in CMake

我正在嘗試使用CMake在我的項目中添加CMake jwt-cpp庫。

我的項目結構如下

build
External
---- jwt
.gitignore
.gitmodules
CMakeLists.txt
main.cpp

我使用命令git submodule add https://github.com/Thalhammer/jwt-cpp.git External/jwt將提到的 git 子模塊添加到我的項目中。 .gitmodules文件的內容是

[submodule "External/jwt"]
    path = External/jwt
    url = https://github.com/Thalhammer/jwt-cpp.git

我的 CMakeLists.txt 看起來像

cmake_minimum_required(VERSION 3.24.2)
set(This Trial)
project(${This} C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(Sources main.cpp)
add_executable(${This} ${Sources})

add_subdirectory(External/jwt)

if(NOT TARGET jwt-cpp)
  find_package(jwt-cpp CONFIG REQUIRED)
endif()

target_include_directories(${This} INTERFACE External/jwt/include)
target_link_libraries(${This} INTERFACE jwt-cpp::jwt-cpp)

我的main.cpp

#include <iostream>
#include <jwt-cpp/jwt.h>

int main ()
{
    return 0;
}

我面臨的問題是,如果不在我的main.cpp文件中包含#include <jwt-cpp/jwt.h> ,那么所有配置都會正確構建,但如果我這樣做會拋出錯誤。 錯誤如下所示:

fatal error: 'jwt-cpp/jwt.h' file not found
#include <jwt-cpp/jwt.h>
         ^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/Trial.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Trial.dir/all] Error 2
make: *** [all] Error 2

我無法弄清楚我應該更正/編輯什么。

INTERFACE屬性為依賴目標添加文件夾。 您需要使用PUBLIC屬性,以便將文件夾添加到要構建的目標中。

暫無
暫無

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

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