使用cmake进行编译,使用macport安装依赖项

时间:2012-10-17 16:32:42

标签: opencv compilation cmake

我正在尝试构建依赖于OpenCV的项目。我使用macports安装了Opencv,当我尝试构建项目时,cmake给出了以下输出:

CMake Error at CMakeLists.txt:47 (FIND_PACKAGE):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

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

    OpenCVConfig.cmake
    opencv-config.cmake

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

我搜索了一下这个问题,并添加了以下环境。变量到我的$HOME/.profile文件

export DYLD_LIBRARY_PATH=/opt/local/lib:$DYLD_LIBRARY_PATH
export CMAKE_PREFIX_PATH=/opt/local

没有成功。我检查了所有opencv文件都在/ opt / local / lib和/ opt / local / include / opencv目录中。以下路径中还有OpenCVConfig.cmake

/opt/local/lib/cmake/OpenCVConfig.cmake

如何让cmake了解opencv的安装路径?以前我使用cmake自己构建OpenCV并安装到/ usr / local然后它没有任何其他修复工作正常。但是我在使用ffmpeg时遇到了一些问题,现在我转而使用macports。

7 个答案:

答案 0 :(得分:8)

另一个适用于我的选项是在cmake opencv目录中设置env值OpenCV_DIR:

export OpenCV_DIR=/opt/local/lib/cmake/

答案 1 :(得分:1)

macports 传统上将OpenCV安装到/opt/local/而不是标准/usr/local/

您的问题的解决方案如下:

  

将“OpenCV”的安装前缀添加到CMAKE_PREFIX_PATH或设置     “OpenCV_DIR”到包含上述文件之一的目录。

在命令行中构建项目时,请确保执行:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/local/lib

然后为cmake设置标志CMAKE_PREFIX_PATH

cmake -D CMAKE_PREFIX_PATH=/opt/local ../

答案 2 :(得分:1)

我终于阅读了OpenCVConfig.cmake文件的标题。 它指示包含这些行以从外部项目中使用:

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MY_TARGET_NAME ${OpenCV_LIBS})

(将include_directories添加到CMakeLists.txt为我修复了它)

答案 3 :(得分:0)

不是MACPORT问题,但有人可能会觉得这很有帮助。跟随@ hugh-pearse和@ leszek-hanusz在this question的回答,稍微调整一下。我从ubuntu 12.10存储库(libopencv - )* 安装了 opencv,并遇到了同样的问题。无法用export OpenCV_DIR=/usr/share/OpenCV/来解决它(因为我的OpenCVConfig.cmake已经存在)。当我在OpenCVConfig.cmake文件上更改了一些行时,它就解决了:

# ======================================================
# Include directories to add to the user project:
# ======================================================

# Provide the include directories to the caller

#SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include")

SET(OpenCV_INCLUDE_DIRS "/usr/include/opencv;/usr/include/opencv2")
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})

# ======================================================
# Link directories to add to the user project:
# ======================================================

# Provide the libs directory anyway, it may be needed in some cases.

#SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib")

SET(OpenCV_LIB_DIR "/usr/lib")

LINK_DIRECTORIES(${OpenCV_LIB_DIR})

这对我的Ubuntu 12.10有用。请务必在CMakeLists.txt中添加target_link_libraries(yourprojectname ${OpenCV_LIBS})

答案 4 :(得分:0)

我徒劳地尝试了上述所有想法。 我最终找到了一种编译工作的方法:除了使用@kengregson步骤之外,我只需重命名文件夹/ usr / local / include / opencv2,以便在编译cpp文件时不会选择它。

答案 5 :(得分:0)

由于我在多用户服务器中编译自己的OpenCV,所以我不能直接在/ usr / local中安装OpenCV库,但在我的主文件夹中,那么我就会遇到完全相同的问题。

以下是我修复它的方法:

  1. 完成OpenCV official doc: Using OpenCV with gcc and CMake

  2. 中的步骤
  3. 添加displayImage测试程序,并使用此CMake文件

  4. cmake_minimum_required(VERSION 2.8) project( DisplayImage ) find_package( OpenCV) add_executable( DisplayImage DisplayImage.cpp ) target_link_libraries( DisplayImage ${OpenCV_LIBS} )

    1. 发生了同样的错误,因此在我的.tcshrc中添加一个环境变量
    2. setenv OpenCV_DIR "${folder where contains OpenCVConfig.cmake }" \# mine is: setenv OpenCV_DIR "~/local/OpenCV2.4.13/share/OpenCV"

答案 6 :(得分:0)

确保已编译OpenCV,一旦编译,OpenCVConfig.cmake将在构建目录中生成。

按照these的步骤进行编译

然后,export OpenCV_DIR=<path to build directory with OpenCVConfig.cmake>

现在应该可以工作!