CMake中find_package功能示範

來源:互聯網
上載者:User
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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.