开罗库和Cmake

3

我刚接触C++和CMake。我通过端口安装了cairo库,方法在这里here。现在我想将cairo包含到我的项目中。我按照这里here所示的CMakeLists.txt命令编写了代码。

cmake_minimum_required(VERSION 3.6)
project(HelloOpenGL)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(HelloOpenGL ${SOURCE_FILES})

#find_package(ImageMagick COMPONENTS Magick++)
#include_directories(${ImageMagick_INCLUDE_DIRS})
#target_link_libraries(HelloOpenGL ${ImageMagick_LIBRARIES})

find_package(Cairo)
include_directories(${Cairo_INCLUDE_DIRS})
target_link_libraries(HelloOpenGL ${Cairo_LIBRARIES})

if(CAIRO_FOUND)
    message("Cairo found")
    else()
    message("Cairo not found")
    endif()

但它不起作用,我得到了这个输出 -

CMake Warning at CMakeLists.txt:16 (find_package):
  By not providing "FindCairo.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Cairo", but
  CMake did not find one.

  Could not find a package configuration file provided by "Cairo" with any of
  the following names:

    CairoConfig.cmake
    cairo-config.cmake

  Add the installation prefix of "Cairo" to CMAKE_PREFIX_PATH or set
  "Cairo_DIR" to a directory containing one of the above files.  If "Cairo"
  provides a separate development package or SDK, be sure it has been
  installed.

请帮我正确地包含cairo


你使用的是哪个版本的CMake? - fedepad
1个回答

4

问题在于您的CMake版本没有(顺便说一下,即使是最新的CMake开发版本也没有... https://gitlab.kitware.com/cmake/cmake/tree/master/Modules)您需要运行命令find_package(Cairo)FindCairo.cmake文件,并且您没有将此文件包含在您的软件包中。
解决方法是从Web上获取一个FindCairo.cmake文件,在项目根目录下创建一个cmake目录,并在CMakeLists.txt中添加额外的行。

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

所以你从 CMakeLists.txt 中提取的代码片段应该如下所示:
cmake_minimum_required(VERSION 3.6)
project(HelloOpenGL)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(HelloOpenGL ${SOURCE_FILES})

#find_package(ImageMagick COMPONENTS Magick++)
#include_directories(${ImageMagick_INCLUDE_DIRS})
#target_link_libraries(HelloOpenGL ${ImageMagick_LIBRARIES})

find_package(Cairo)
include_directories(${Cairo_INCLUDE_DIRS})
target_link_libraries(HelloOpenGL ${Cairo_LIBRARIES})

如果您不使用已经存在的FindCairo.cmake(例如您安装的Cairo可能包含这样一个文件),则必须编写一个或找到另一种替代方法来包含该软件包。

我尝试了这个方法,但问题只有在我通过brew install cairo重新安装cairo后才得以解决。现在又出现了一个新问题。IDE无法找到include<cairo.h>。但是include<cairo/cairo.h>可以正常工作。 - Mansur Nashaev
然而,在编译时我收到了以下错误信息:Undefined symbols for architecture x86_64: "_cairo_arc", referenced from: ... ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: *** [HelloOpenGL] Error 1 make[2]: *** [CMakeFiles/HelloOpenGL.dir/all] Error 2 make[1]: *** [CMakeFiles/HelloOpenGL.dir/rule] Error 2 make: *** [HelloOpenGL] Error 2 - Mansur Nashaev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接