在 CMakeList.txt 文件中包含 OpenSSL

2021-12-26 00:00:00 macos openssl cmake c++ podofo

我有一个问题要问在 C++ 中使用 CMakeList.txt 的人.我想使用 Podofo 项目(一个解析和创建 pdf 的项目).

I have a question for people who work with CMakeList.txt in C++. I want to use Podofo project (a project to parse & create pdf).

所以我的主要功能很简单:

So my main function is simple as:

#include <iostream>
#include <podofo/podofo.h>

int main() {
  PoDoFo::PdfMemDocument pdf;
  pdf.Load("/Users/user/path/to.pdf");

  int nbOfPage = pdf.GetPageCount();

  std::cout << "Our pdf have " << nbOfPage << " pages." << std::endl;
  return 0;
}

我的 CMakeList.txt 是:

My CMakeList.txt is:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)

add_executable(untitled ${SOURCE_FILES})

但我被这个错误困住了:

But I am stuck with this error:

/usr/local/include/podofo/base/PdfEncrypt.h:44:10: fatal error: 'openssl/opensslconf.h' file not found
#include <openssl/opensslconf.h

我尝试使用 find_packagefind_library 来包含 .. 设置了一些变量,但我没有找到方法.

I tried to include with find_package, find_library .. setting some variables but I do not find the way.

我的环境是:

  • macOS
  • 克里昂
  • Podofo 通过 home-brew 安装在 /usr/local/podofo
  • OpenSSL 通过 home-brew 安装在 /usr/local/opt/openssl

感谢提前社区!!

推荐答案

find_package 是正确的做法;您可以在此处找到有关它的详细信息.

find_package is the correct approach; you find details about it here.

在您的情况下,您应该添加以下几行:

In your case, you should add these lines:

find_package(OpenSSL REQUIRED)
target_link_libraries(untitled OpenSSL::SSL)

如果CMake没有直接找到OpenSSL,你应该设置CMake变量OPENSSL_ROOT_DIR.

If CMake doesn't find OpenSSL directly, you should set the CMake variable OPENSSL_ROOT_DIR.

相关文章