我如何在 cmake 中 link botan?

How do I link botan in cmake?

我正在尝试使用 botan 用 C++ 编写程序,但我不知道如何正确使用它。以下面的代码为例:

Botan::AutoSeeded_RNG rng;
Botan::UUID uuid = Botan::UUID(rng);
std::cout << uuid.to_string() << std::endl;

如果我尝试 运行 它会抛出一个似乎是 issue with botan not being linked 的错误。但我不确定如何使用 CMake link 这个。 在 CMakeList.txt 我有

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
find_package(BOTAN REQUIRED)

based on this, and in modules, I have a file FindBOTAN.cmake.

但这会引发错误 Could NOT find Botan (missing: BOTAN_LIBRARIES BOTAN_INCLUDE_DIRS)、警告

The package name passed to `find_package_handle_standard_args` (PkgConfig)
does not match the name of the calling package (BOTAN).  This can lead to
problems in calling code that expects `find_package` result variables
(e.g., `_FOUND`) to follow a certain pattern.

找不到牡丹。

我几乎没有任何使用 CMake 的经验,因此我将它基于别人的,但现在我遇到了一个问题,我不知道出了什么问题。

那个查找模块是完全错误的。您应该将写它的人指向此答案。这里有一些更好的东西(但仍然可以改进,例如通过验证版本):

find_package(PkgConfig REQUIRED)

if (NOT TARGET Botan::Botan)
  pkg_check_modules(Botan QUIET IMPORTED_TARGET botan-2)
  if (TARGET PkgConfig::Botan)
    add_library(Botan::Botan ALIAS PkgConfig::Botan)
  endif ()
endif ()

if (NOT TARGET Botan::Botan)
  find_path(Botan_INCLUDE_DIRS NAMES botan/botan.h
            PATH_SUFFIXES botan-2
            DOC "The Botan include directory")

  find_library(Botan_LIBRARIES NAMES botan botan-2
               DOC "The Botan library")

  mark_as_advanced(Botan_INCLUDE_DIRS Botan_LIBRARIES)

  add_library(Botan::Botan IMPORTED UNKNOWN)
  set_target_properties(
    Botan::Botan
    PROPERTIES
    IMPORTED_LOCATION "${Botan_LIBRARIES}"
    INTERFACE_INCLUDE_DIRECTORIES "${Botan_INCLUDE_DIRS}"
  )
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  Botan
  REQUIRED_VARS Botan_LIBRARIES Botan_INCLUDE_DIRS
)

将此文件放在 cmake/FindBotan.cmake 中,然后您可以从 top-level CMakeLists.txt 中使用它,如下所示:

cmake_minimum_required(VERSION 3.23)
project(botan_example)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(Botan REQUIRED)

add_executable(botan_example main.cpp)
target_link_libraries(botan_example PRIVATE Botan::Botan)

一如既往,您应该努力 link 到 导入的目标 ,例如我修改后的查找模块定义的 Botan::Botan 目标。对于 main.cpp,我将您的代码包装成这样:

#include <botan/botan.h>
#include <botan/uuid.h>
#include <iostream>

int main() {
  Botan::AutoSeeded_RNG rng;
  Botan::UUID uuid = Botan::UUID(rng);
  std::cout << uuid.to_string() << std::endl;
}

而且我确认它确实构建了:

alex@alex-ubuntu:~/test$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for module 'botan-2'
--   Found botan-2, version 2.12.1
-- Found Botan: botan-2  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/build
alex@alex-ubuntu:~/test$ cmake --build build/
[ 50%] Building CXX object CMakeFiles/botan_example.dir/main.cpp.o
[100%] Linking CXX executable botan_example
[100%] Built target botan_example
alex@alex-ubuntu:~/test$ ./build/botan_example 
6ACD38C7-3B70-4B57-A0F8-DEDCDCEF4D34