CMake中find_package功能演示

CMake中find_package功能演示

标签: pythonlibraryincludecomponentsubuntugcc
  14461人阅读  评论(0)  收藏  举报
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值