用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
Bowater的公告
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-4448577-1"); pageTracker._initData(); pageTracker._trackPageview(); </script>
文章分类
    存档

    原创  CMake中find_package功能演示 收藏

    CMake中find_package功能演示

    find_package可以被用来在系统中自动查找配置构建工程所需的程序库。在linux和unix类系统下这个命令尤其有用。CMake自带的模块文件里有大半是对各种常见开源库的find_package支持,支持库的种类非常多。

    以下使用boost python的一个hello world例子的cmake构建文件为例,展示了如何使用find_package使用查找boost库和python库。这个例子需要使用最新的cmake版本2.6。


    文件:boost_1_35_0/libs/python/example/tutorial/hello.cpp

    // Copyright Joel de Guzman 2002-2004. Distributed under the Boost
    // Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
    // or copy at http://www.boost.org/LICENSE_1_0.txt)
    // Hello World Example from the tutorial
    // [Joel de Guzman 10/9/2002]

    #include
    #include

    char const* greet()
    {
    return "hello, world";
    }

    BOOST_PYTHON_MODULE(hello_ext)
    {
    using namespace boost::python;
    def("greet", greet);
    }


    文件:CMakeLists.txt

    cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

    project(hello)

    set(Boost_USE_STATIC_LIBS ON)
    set(Boost_USE_MULTITHREAD ON)

    #查找boost库中python的bind库
    #REQUIRED表示如果没有找到,cmake会停止处理,并报告一个错误.
    find_package( Boost 1.35 REQUIRED
    COMPONENTS python)

    #找到Boost后,变量Boost_INCLUDE_DIRS中将包括指定boost库头文件的查找路径.
    #变量Boost_LIBRARY_DIRS中将包含指定boost库的.a或.so文件的所在目录的路径.
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})

    find_package(PythonLibs 2.5 REQUIRED)

    add_library(hello SHARED
    hello.cpp)

    include_directories(${PYTHON_INCLUDE_PATH})

    target_link_libraries(hello
    debug ${Boost_PYTHON_LIBRARY_DEBUG}
    optimized ${Boost_PYTHON_LIBRARY_RELEASE}
    )

    target_link_libraries(hello
    debug ${PYTHON_DEBUG_LIBRARIES}
    optimized ${PYTHON_LIBRARIES}
    )


    以上2个文件保存到同一目录即可。使用cmake-gui工具生成本地构建文件时,当完成configure后,打开Show Advanced Entries选项,即可看见Boost_PYTHON_LIBRARY_DEBUG、Boost_PYTHON_LIBRARY_RELEASE、PYTHON_DEBUG_LIBRARIES、PYTHON_LIBRARIES等变量的存在及其值。

    因为gcc不支持写在代码文件中lib链接指令,boost库的自动连接在gcc中无效。构建时需要程序员自己去按照boost库文件的命名规则去指定要链接的库,这个很麻烦。在使用cmake的find_package后,这些麻烦的事都被省略掉了,而且不像人工指定那么容易出错,提高了生产力。

    构建成功后应该生成libhello.so文件。使用命令ln libhello.so hello_ext.so -s 做个符号链接。
    在终端输入命令python 进入python解释器。
    Python 2.5.2 (r252:60911, May 7 2008, 15:19:09)
    [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import hello_ext
    >>> print hello_ext.greet()
    hello, world
    >>>



    cmake支持查找的库很多,见下面。详细使用方式请查cmake文档。
    # FindASPELL
    # FindAVIFile
    # FindBLAS
    # FindBZip2
    # FindBoost
    # FindCABLE
    # FindCURL
    # FindCVS
    # FindCups
    # FindCurses
    # FindCygwin
    # FindDCMTK
    # FindDart
    # FindDoxygen
    # FindEXPAT
    # FindFLTK
    # FindFLTK2
    # FindFreetype
    # FindGCCXML
    # FindGDAL
    # FindGIF
    # FindGLUT
    # FindGTK
    # FindGettext
    # FindGnuplot
    # FindHSPELL
    # FindHTMLHelp
    # FindITK
    # FindImageMagick
    # FindJNI
    # FindJPEG
    # FindJasper
    # FindJava
    # FindKDE3
    # FindKDE4
    # FindLAPACK
    # FindLATEX
    # FindLibXml2
    # FindLibXslt
    # FindLua50
    # FindLua51
    # FindMFC
    # FindMPEG
    # FindMPEG2
    # FindMPI
    # FindMatlab
    # FindMotif
    # FindOpenAL
    # FindOpenGL
    # FindOpenSSL
    # FindOpenThreads
    # FindPHP4
    # FindPNG
    # FindPackageHandleStandardArgs
    # FindPackageMessage
    # FindPerl
    # FindPerlLibs
    # FindPhysFS
    # FindPike
    # FindPkgConfig
    # FindProducer
    # FindPythonInterp
    # FindPythonLibs
    # FindQt
    # FindQt3
    # FindQt4
    # FindQuickTime
    # FindRuby
    # FindSDL
    # FindSDL_image
    # FindSDL_mixer
    # FindSDL_net
    # FindSDL_sound
    # FindSDL_ttf
    # FindSWIG
    # FindSelfPackers
    # FindSubversion
    # FindTCL
    # FindTIFF
    # FindTclStub
    # FindTclsh
    # FindThreads
    # FindUnixCommands
    # FindVTK
    # FindWget
    # FindWish
    # FindX11
    # FindXMLRPC
    # FindZLIB
    # Findosg
    # FindosgDB
    # FindosgFX
    # FindosgGA
    # FindosgIntrospection
    # FindosgManipulator
    # FindosgParticle
    # FindosgProducer
    # FindosgShadow
    # FindosgSim
    # FindosgTerrain
    # FindosgText
    # FindosgUtil
    # FindosgViewer
    # FindwxWidgets
    # FindwxWindows


    发表于 @ 2008年05月24日 06:13:00 | 评论( loading... ) | 编辑| 举报| 收藏

    旧一篇:为 CMake 添加 MSVC 预编译头文件支持

    • 发表评论
    • 评论内容:
    •  
    Copyright © Bowater
    Powered by CSDN Blog