用premake编译跨平台opencv程序

premake与cmake都是非常不错的跨平台编译系统,主要针对C++。记得老东家当年针对WIn+MacOS,使用的就是cmake (一开始维护两套编译脚本,非常痛苦),cmake不错,但使用了一套自定义的格式,对于程序员来讲,可能更喜欢使用lua作为meta data的premake,熟悉,灵活且可扩展。

今天用opencv试了下编译opencv的example,感觉的确非常方便 - 你不用特别去了解VS或者gmake的格式/语法,就能通过一些简单的premake配置,产生project文件或者makefile,继而编译出你要的binary。

premake的文档挺不错的,基本上,你可以:

  1. 首先,使用这个script编译你的第一个程序:http://industriousone.com/sample-script
  2. 然后,可以通读一遍:http://industriousone.com/scripting-premake, 了解configuration,platform, includedirs, libdirs, linking等重要概念
  3. 其他的,当然可以参考premake的reference:http://industriousone.com/scripting-reference

然后,你就上手了:)

关于在Win和Linux下配置OpenCV开发环境,这里有两篇可以参考:

这边我用premake编译了opencv tutorials中的第一个display_image这个例子,下面是lua script:

 1 -- A solution contains projects, and defines the available configurations
 2 solution "display_image"
 3    configurations { "Debug", "Release" }
 4    platforms { "x32", "x64" }
 5  
 6    -- A project defines one build target
 7    project "display_image"
 8       kind "ConsoleApp"
 9       language "C++"
10       files { "**.h", "**.cpp" }
11       
12       
13      
14       configuration "x64"
15           if os.is("windows") then
16             includedirs { "$(OPENCV_SDK_DIR)\\build\\include" }
17             libdirs { "$(OPENCV_SDK_DIR)\\build\\x64\\vc9\\lib" }
18             links { "opencv_core231", "opencv_highgui231", "opencv_imgproc231", "opencv_objdetect231" }
19           elseif os.is("linux") then
20             includedirs { "/usr/local/include" }
21             libdirs { "/usr/local/lib" }
22             links { "opencv_core", "opencv_highgui", "opencv_imgproc", "opencv_objdetect" }
23           end
24         
25        configuration "x32"
26           if os.is("windows") then
27             includedirs { "$(OPENCV_SDK_DIR)\\build\\include" }
28             libdirs { "$(OPENCV_SDK_DIR)\\build\\x86\\vc9\\lib" }
29             links { "opencv_core231", "opencv_highgui231", "opencv_imgproc231", "opencv_objdetect231" }
30           elseif os.is("linux") then
31             includedirs { "/usr/local/include" }
32             libdirs { "/usr/local/lib" }
33             links { "opencv_core", "opencv_highgui", "opencv_imgproc", "opencv_objdetect" }
34           end
35     
36       
37       configuration "Debug"
38          defines { "DEBUG" }
39          flags { "Symbols" }
40  
41       configuration "Release"
42          defines { "NDEBUG" }
43          flags { "Optimize" } 
44          
45     

 例子的地址:https://github.com/lzprgmr/examples/blob/master/premake/display_image/premake4.lua

一些解释:

  • 声明了两个configuration(debug/release)和platform(x32/x64),这样最后就能产生2*2=4个配置,这其实VC++中概念,但在linux下也同样适用
  • 针对不同的configuration或者platform进行设置,你可以通过在相应的configuration后进行 (是的,对platform也是如此)
  • 对于C++,最重要的配置自然是:includedirs, libdirs和links
  • 由于在windows或者linux下,其includedirs与libdirs肯定是不同的,我们可以通过os.is("windows")这个函数来区别

好,下面来编译:

Windows

$ premake4 vs2008
$ vcbuild display_image.vcproj "Debug|x64"

命令行编译vcproj,其实还可以使用devenv和msbuild

Linux

$ premake4 gmake
$ make help
$ make config=release32 display_image

 

如此,一个跨平台编译环境就搭好了,可以开始搞我的cvlab了。

posted @ 2012-08-12 21:52  lzprgmr  阅读(2180)  评论(0编辑  收藏  举报

黄将军