http://blog.csdn.net/chenyusiyuan/article/details/4623675
20091027-Update:
參考於仕琪老師《VC 2008 Express下安裝OpenCV2.0》作適當修改。
OpenCV2.0.0今天發布了,我馬上趕去下載嘗鮮,不過安裝後才發現,這個版本的 bin 和 lib 都是未經編譯、還不能直接使用的。通過 OpenCV 中文論壇得知,要使用 Cmake 軟件對 OpenCV 源代碼重新編譯,才能得到最終的 dll 和 lib 文件。編譯方法如下:
1. 假設OpenCV2.0安裝於以下目錄: C:/opencv2.0。
2. 使用CMake建立VC工程文件
2.1 運行Cmake gui
Where is the source code: C:/opencv2.0
Where to build the binaries: C:/opencv2.0/vs2008build(自己新建此目錄)
2.2 點擊configure按鈕,選擇相應的編譯器為visual studio 9 2008,點擊finish,出現紅色標記的參數。
2.3 再點configur按鈕,參數消除了紅色。
2.4 點擊generator按鈕,生成工程文件。
3. 用vc2008進行編譯
完成上一步驟後,將在C:/opencv2.0/vs2008build目錄下生成OpenCV.sln的VC Solution File,請用VS2008打開OpenCV.sln,然後執行如下操作:
- 在Debug下,選擇Solution Explorer裡的 Solution OpenCV,點右鍵,運行"Rebuild Solution";如編譯無錯誤,再選擇INSTALL項目,運行"Build"。
- 在Release下,選擇Solution Explorer裡的 Solution OpenCV,點右鍵,運行"Rebuild Solution";如編譯無錯誤,再選擇INSTALL項目,運行"Build"。
此時,OpenCV的*d.dll文件(for debug)和*.dll文件(for release)將出現在C:/opencv2.0/vs2008build/bin目錄中;OpenCV的*d.lib文件(for debug)和*.lib文件(for release)將出現在C:/opencv2.0/vs2008build/lib目錄;頭文件*.h出現在C:/opencv2.0/vs2008build/include/opencv中。
可以被VS2008調用的OpenCV動態庫生成完畢。
下面是原來的編譯方法,不大合適,會產生編譯錯誤。
3.1 用vc2008打開產生的工程文件OpenCV.sln,打開batch build菜單,全選中,然後Build (這個過程大概要6、7分鐘)
3.2 出現以下錯誤:
------ Build started: Project: RUN_TESTS, Configuration: Release Win32 ------
Performing Post-Build Event...
Start processing tests
Test project D:/OpenCV2.0/build
1/ 3 Testing cvtest Could not find executable D:/OpenCV2.0/build/bin/$(OutDir)/cvtest.exe
Looked in the following places:
D:/OpenCV2.0/build/bin/$(OutDir)/cvtest.exe
D:/OpenCV2.0/build/bin/$(OutDir)/cvtest.exe.exe
D:/OpenCV2.0/build/bin/$(OutDir)/Release/cvtest.exe
D:/OpenCV2.0/build/bin/$(OutDir)/Release/cvtest.exe.exe
Release/D:/OpenCV2.0/build/bin/$(OutDir)/cvtest.exe
Release/D:/OpenCV2.0/build/bin/$(OutDir)/cvtest.exe.exe
Unable to find executable: D:/OpenCV2.0/build/bin/$(OutDir)/cvtest.exe
2/ 3 Testing cxcoretest Could not find executable D:/OpenCV2.0/build/bin/$(OutDir)/cxcoretest.exe
Looked in the following places:
D:/OpenCV2.0/build/bin/$(OutDir)/cxcoretest.exe
D:/OpenCV2.0/build/bin/$(OutDir)/cxcoretest.exe.exe
D:/OpenCV2.0/build/bin/$(OutDir)/Release/cxcoretest.exe
D:/OpenCV2.0/build/bin/$(OutDir)/Release/cxcoretest.exe.exe
Release/D:/OpenCV2.0/build/bin/$(OutDir)/cxcoretest.exe
Release/D:/OpenCV2.0/build/bin/$(OutDir)/cxcoretest.exe.exe
Unable to find executable: D:/OpenCV2.0/build/bin/$(OutDir)/cxcoretest.exe
3/ 3 Testing mltest Could not find executable D:/OpenCV2.0/build/bin/$(OutDir)/mltest.exe
Looked in the following places:
D:/OpenCV2.0/build/bin/$(OutDir)/mltest.exe
D:/OpenCV2.0/build/bin/$(OutDir)/mltest.exe.exe
D:/OpenCV2.0/build/bin/$(OutDir)/Release/mltest.exe
D:/OpenCV2.0/build/bin/$(OutDir)/Release/mltest.exe.exe
Release/D:/OpenCV2.0/build/bin/$(OutDir)/mltest.exe
Release/D:/OpenCV2.0/build/bin/$(OutDir)/mltest.exe.exe
Unable to find executable: D:/OpenCV2.0/build/bin/$(OutDir)/mltest.exe
0% tests passed, 3 tests failed out of 3
The following tests FAILED:
1 - cvtest (Not Run)
2 - cxcoretest (Not Run)
3 - mltest (Not Run)
Errors while running CTest
Project : error PRJ0019: A tool returned an error code from "Performing Post-Build Event..."
解決方法之一是:將RUN_TESTS項目從solution中刪除,不過似乎這種方式不大好。在 OpenCV 中文論壇發起提問,得到回復如下:
「按以下方式編譯可以解決:
運行Cmake gui,設定生成的目標路徑為opencv安裝路徑。」