使用属性表:VS2013上配置OpenCV

以前,windows下配置OpenCV一直不太方便:总是要手动添加lib,添加include,还要配置PATH使得程序运行时候能找到dll文件。

每次新建一个使用OpenCV的工程都要手动添加,很麻烦;而且,有时候需要不同版本的OpenCV测试。如果从32位模式切换到64位模式,又需要替换一次配置。。一旦有一个配错了,就总是徘徊在各种配置项的检查上。

后来,看到了freedomshe用属性表进行配置,其提供的属性表文件确实方便,但是还是需要手动配置OpenCV环境变量、追加OpenCV中的bin目录到PATH上(bin中有dll文件)。能不能不配置?(免得保不准自己又要重启机器...)

答案当然是可以!

配置OpenCV环境变量的目的在于,include路径、lib路径都使用这个前缀,能减少打字的次数;同时也是要把opencv的几个dll文件所在路径,追加到系统PATH上。

只要我们在属性表文件中自行定义OpenCV根目录,添加include路径、lib路径,添加dll目录,以后使用OpenCV的项目只需要导入这个属性表就OK了,太简单了!

当然,以前的老版本中的各种dll和lib分成好多个文件,现在的版本如果编译了opencv_contrib也会有额外的dll和lib,这些需要的时候再添加就好了。

废话不多说,这是OpenCV3.0.0的通用属性表文件opencv300.props

<?xml version="1.0" encoding="utf-8"?>
<!--
OpenCV 3.0.0 属性表文件
不用单独配置环境变量OpenCV
也不用单独在系统PATH上添加OpenCV可执行路径
只需要在 "菜单"-"视图"-"属性管理器"中添加现有属性表,添加本文件 使用前请将下面的OpenCVRoot修改为你的OpenCV根目录
--> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup> <!-- 请把OpenCVRoot属性的值替换为你的OpenCV编译输出的目标目录,它包含x64、x86这样的子目录 -->
<OpenCVRoot>F:\chris\lib\opencv-3.1.0\buildExtra\install</OpenCVRoot> <!-- 指定INCLUDE路径-->
<IncludePath>$(OpenCVRoot)\include;$(IncludePath)</IncludePath> <!-- 指定链接所需lib目录-->
<LibraryPath Condition="'$(Platform)'=='Win32'">$(OpenCVRoot)\x86\vc12\lib;$(LibraryPath)</LibraryPath>
<LibraryPath Condition="'$(Platform)'=='X64'">$(OpenCVRoot)\x64\vc12\lib;$(LibraryPath)</LibraryPath> <!-- 保证系统处于Debug模式时,可以使用相对路径读取源代码目录下的文件,比如"lena.jpg"而不必是"d:/work/test/test/lena.jpg" -->
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <!-- 让系统PATH临时包含指定的dll目录 -->
<LocalDebuggerEnvironment Condition="'$(Platform)'=='Win32'">PATH=%PATH%;$(OpenCVRoot)\x86\vc12\bin</LocalDebuggerEnvironment>
<LocalDebuggerEnvironment Condition="'$(Platform)'=='X64'">PATH=%PATH%;$(OpenCVRoot)\x64\vc12\bin</LocalDebuggerEnvironment>
</PropertyGroup> <!-- 以下是程序需要加载的lib文件。-->
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_ts300d.lib;opencv_world310d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_ts300.lib;opencv_world310.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

如果是opencv3.1那么lib只需要配置opencv_world310,对应的属性表文件为:

<?xml version="1.0" encoding="utf-8"?>
<!--
OpenCV 3.1.0属性表文件
不用单独配置环境变量OpenCV
也不用单独在系统PATH上添加OpenCV可执行路径
只需要在VisualStudio-"菜单"-"视图"-"属性管理器"中添加现有属性表,添加本文件 使用前请将下面的OpenCVRoot修改为你的OpenCV编译输出目录
--> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup> <!-- 请把OpenCVRoot属性的值替换为你的OpenCV编译输出的目标目录,它包含x64、x86这样的子目录 -->
<OpenCVRoot>D:\lib\opencv310\build</OpenCVRoot> <!-- 指定INCLUDE路径-->
<IncludePath>$(OpenCVRoot)\include;$(IncludePath)</IncludePath> <!-- 指定链接所需lib目录-->
<LibraryPath Condition="'$(Platform)'=='Win32'">$(OpenCVRoot)\x86\vc12\lib;$(LibraryPath)</LibraryPath>
<LibraryPath Condition="'$(Platform)'=='X64'">$(OpenCVRoot)\x64\vc12\lib;$(LibraryPath)</LibraryPath> <!-- 保证系统处于Debug模式时,可以使用相对路径读取源代码目录下的文件,比如"lena.jpg"而不必是"d:/work/test/test/lena.jpg" -->
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <!-- 让系统PATH临时包含指定的dll目录 -->
<LocalDebuggerEnvironment Condition="'$(Platform)'=='Win32'">PATH=%PATH%;$(OpenCVRoot)\x86\vc12\bin</LocalDebuggerEnvironment>
<LocalDebuggerEnvironment Condition="'$(Platform)'=='X64'">PATH=%PATH%;$(OpenCVRoot)\x64\vc12\bin</LocalDebuggerEnvironment>
</PropertyGroup> <!-- 以下是程序需要加载的lib文件。-->
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_world310d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_world310.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

当然有时候需要使用OpenCV_contrib,那么编译好contrib后就增加了很多dll和lib,对应的属性表文件是这样的(opencv3.1版):

<?xml version="1.0" encoding="utf-8"?>
<!--
OpenCV 3.1.0 带contrib的属性表文件
不用单独配置环境变量OpenCV
也不用单独在系统PATH上添加OpenCV可执行路径
只需要在 "菜单"-"视图"-"属性管理器"中添加现有属性表,添加本文件 使用前请将下面的OpenCVRoot修改为你的OpenCV根目录
--> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup> <!-- 请把OpenCVRoot属性的值替换为你的OpenCV编译输出的目标目录,它包含x64、x86这样的子目录 -->
<OpenCVRoot>F:\chris\lib\opencv-3.1.0\buildExtra\install</OpenCVRoot> <!-- 指定INCLUDE路径-->
<IncludePath>$(OpenCVRoot)\include;$(IncludePath)</IncludePath> <!-- 指定链接所需lib目录-->
<LibraryPath Condition="'$(Platform)'=='Win32'">$(OpenCVRoot)\x86\vc12\lib;$(LibraryPath)</LibraryPath>
<LibraryPath Condition="'$(Platform)'=='X64'">$(OpenCVRoot)\x64\vc12\lib;$(LibraryPath)</LibraryPath> <!-- 保证系统处于Debug模式时,可以使用相对路径读取源代码目录下的文件,比如"lena.jpg"而不必是"d:/work/test/test/lena.jpg" -->
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <!-- 让系统PATH临时包含指定的dll目录 -->
<LocalDebuggerEnvironment Condition="'$(Platform)'=='Win32'">PATH=%PATH%;$(OpenCVRoot)\x86\vc12\bin</LocalDebuggerEnvironment>
<LocalDebuggerEnvironment Condition="'$(Platform)'=='X64'">PATH=%PATH%;$(OpenCVRoot)\x64\vc12\bin</LocalDebuggerEnvironment>
</PropertyGroup> <!-- 以下是程序需要加载的lib文件。-->
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_aruco310d.lib;opencv_bgsegm310d.lib;opencv_bioinspired310d.lib;opencv_calib3d310d.lib;opencv_ccalib310d.lib;opencv_core310d.lib;opencv_cudaarithm310d.lib;opencv_cudabgsegm310d.lib;opencv_cudacodec310d.lib;opencv_cudastereo310d.lib;opencv_cudawarping310d.lib;opencv_cudev310d.lib;opencv_datasets310d.lib;opencv_dnn310d.lib;opencv_dpm310d.lib;opencv_face310d.lib;opencv_features2d310d.lib;opencv_flann310d.lib;opencv_fuzzy310d.lib;opencv_highgui310d.lib;opencv_imgcodecs310d.lib;opencv_imgproc310d.lib;opencv_line_descriptor310d.lib;opencv_ml310d.lib;opencv_objdetect310d.lib;opencv_optflow310d.lib;opencv_plot310d.lib;opencv_reg310d.lib;opencv_rgbd310d.lib;opencv_saliency310d.lib;opencv_shape310d.lib;opencv_stereo310d.lib;opencv_structured_light310d.lib;opencv_surface_matching310d.lib;opencv_text310d.lib;opencv_tracking310d.lib;opencv_ts310d.lib;opencv_video310d.lib;opencv_videoio310d.lib;opencv_world310d.lib;opencv_ximgproc310d.lib;opencv_xobjdetect310d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_aruco310.lib;opencv_bgsegm310.lib;opencv_bioinspired310.lib;opencv_calib3d310.lib;opencv_ccalib310.lib;opencv_core310.lib;opencv_cudaarithm310.lib;opencv_cudabgsegm310.lib;opencv_cudacodec310.lib;opencv_cudastereo310.lib;opencv_cudawarping310.lib;opencv_cudev310.lib;opencv_datasets310.lib;opencv_dnn310.lib;opencv_dpm310.lib;opencv_face310.lib;opencv_features2d310.lib;opencv_flann310.lib;opencv_fuzzy310.lib;opencv_highgui310.lib;opencv_imgcodecs310.lib;opencv_imgproc310.lib;opencv_line_descriptor310.lib;opencv_ml310.lib;opencv_objdetect310.lib;opencv_optflow310.lib;opencv_plot310.lib;opencv_reg310.lib;opencv_rgbd310.lib;opencv_saliency310.lib;opencv_shape310.lib;opencv_stereo310.lib;opencv_structured_light310.lib;opencv_surface_matching310.lib;opencv_text310.lib;opencv_tracking310.lib;opencv_ts310.lib;opencv_video310.lib;opencv_videoio310.lib;opencv_world310.lib;opencv_ximgproc310.lib;opencv_xobjdetect310.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

===========

P.S. 有人可能会问,上面的属性表XML文件里面的各属性是如何确定的?属性值容易设定,属性名确实不是一眼看出来的。

可以通过新建一个工程,在工程的属性中手动设定VC++、C/C++、链接器等条目中的项,进行设定后vs2013会写入到<your_project_name>/<your_project_name>.vcxproj.user这个文件中。手动在GUI界面中设定了需要的属性后,查看.vcxproj.user文件,把需要的属性剪切到属性表文件中,以后同类型的工程就可以仅仅使用此属性表文件进行配置了。比如dll的路径,通过当前工程的属性->调试->工作目录进行设定:

使用属性表:VS2013上配置OpenCV

上一篇:Communication System(dp)


下一篇:EF 外键不显示、如何让外键显示!增、删、改 操作时,外键不显示,只显示导航属性!