使用 CMake 和 NMake 构建 OpenCV 3.0 时出现链接错误

Linking error when building OpenCV 3.0 with CMake and NMake

尝试使用 CMake 和 NMake 构建 OpenCV 3.0:

cmake -DCMAKE_BUILD_TYPE=release -G "NMake Makefiles" -DBUILD_SHARED_LIBS=OFF -DBUILD_opencv_java=OFF -DWITH_MSMF=OFF -DWITH_DSHOW=OFF -DWITH_VFW=OFF DWITH_OPENEXR=OFF -DWITH_CUDA=OFF ..

nmake

我明白了

...
-- Configuring done
-- Generating done
-- Build files have been written to: W:/VSONLINE/Passport-Photo/thirdparty/openc
v-3.0.0/build
nmake

Microsoft (R) Program Maintenance Utility Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Scanning dependencies of target zlib
[  0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/adler32.c.obj
adler32.c
[  0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/compress.c.obj
compress.c
[  0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/crc32.c.obj
crc32.c
[  0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/deflate.c.obj
deflate.c
[  0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzclose.c.obj
gzclose.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzlib.c.obj
gzlib.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzread.c.obj
gzread.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzwrite.c.obj
gzwrite.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/inflate.c.obj
inflate.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/infback.c.obj
infback.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/inftrees.c.obj
inftrees.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/inffast.c.obj
inffast.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/trees.c.obj
trees.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/uncompr.c.obj
uncompr.c
[  1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/zutil.c.obj
zutil.c
[  2%] Linking C static library ..\lib\zlib.lib
The system cannot find the path specified.
NMAKE : fatal error U1077: 'cd' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 12.0
\VC\BIN\nmake.exe"' : return code '0x2'

知道我做错了什么吗?我试过 Visual Studio 2010 和 2013,结果完全相同。

更新 如果我将生成器更改为 "Visual Studio 10 2010" 或 Visual Studio 的任何其他版本,然后使用 msbuild.exe OpenCV.sln 进行构建,则所有构建都会成功。这是 nmakecmake 中的问题吗?

这不是答案,只是我做过的一些调试。问题不在于链接。问题在于堆积起来的 cd 命令。我一直无法找到这些在 CMake 脚本中的位置,但在某个地方,这些出错了。如果您 运行 CMake 配置步骤为:

 -DCMAKE_VERBOSE_MAKEFILE=true

然后你会看到如下输出:

[ 2%] Linking C static library ..\lib\zlib.lib cd 3rdparty\zlib C:\Users\builder\Miniconda2\envs\_build\Library\bin\cmake.exe -P CMakeFiles\zlib.dir\cmake_clean_target.cmake cd ..\.. cd 3rdparty\zlib The system cannot find the path specified.

后面的 cd 行很可疑 - 你不能在没有连接字符的情况下将两张 cd 链接在一起。这里的 NMake 生成器出了问题 - 要么是由于 OpenCV 源代码,要么是由于 CMake。

如果你深入研究 build/3rdparty/zlib/CMakeFiles/zlib.dir/build.make 文件,你就会明白为什么会这样:所有 cd ..\..\ 行都有那个额外的斜杠,我认为在一行的末尾是行继续,而不是路径的结尾。

显然,这是一个已知错误 (issue 5496),已被标记为 wontfix。 在评论中,用户 MarianMMX 建议采用以下解决方法:

  1. Open CMakeLists.txt .
  2. Go to line 70. (Note: it's line 90 in my case, so better do a text search)
  3. Comment (or delete):

    #if(MSVC)
    # set(CMAKE_USE_RELATIVE_PATHS ON CACHE INTERNAL "" FORCE)
    #endif()
    
  4. Delete the generated files and rerun cmake.

我已经使用 nmake(VS2010 和 VS2013)成功编译了 OpenCV 3.0.0 应用上述解决方法:

> cmake -G"NMake Makefiles" <other options> <opencv src dir>
...
> nmake
...