Demo of find_package in cmake

Source: Internet
Author: User
Demo of find_package in cmake

The find_package can be used to automatically find the library required to configure the build project in the system. This command is particularly useful in Linux and UNIX systems. Most of the module files provided by cmake are supported by find_package for various common open-source libraries. There are many supported libraries.

The following example uses the cmake build file in the Hello world example of boost python to show how to use find_package to find the boost library and Python library. This example uses the latest cmake version 2.6.


File: 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_00000.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 );
}


File: cmakelists.txt

Cmake_minimum_required (version 2.6 fatal_error)

Project (Hello)

Set (boost_use_static_libs on)
Set (boost_use_multithread on)

# Search for the python bind library in the boost Library
# Required indicates that, if not found, cmake will stop processing and report an error.
Find_package (boost 1.35 required
Components Python)

# After finding boost, the variable boost_include_dirs will include the search path for the header file of the specified boost library.
# The boost_library_dirs variable contains the path of the directory where the. A or. So file of the specified boost library is located.
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}
)


Save the preceding two files to the same directory. When you use the cmake-GUI tool to generate a local build file, after configure is completed, open the show advanced entries option and you will see the existence and value of variables such as boost_python_library_debug, runtime, python_debug_libraries.

Because GCC does not support the Lib link command written in the code file, the automatic connection of the boost library is invalid in GCC. During the construction, the programmer needs to specify the library to be linked according to the naming rules of the boost library file. This is very troublesome. After using find_package of cmake, these troubles are ignored, and they are not as error-prone as manually specified, improving productivity.

The libhello. So file should be generated after the build is successful. Run ln libhello. So hello_ext.so-s to create a symbolic link.
Enter the python interpreter in the terminal.
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 supports many databases, as shown below. For more information, see the cmake documentation.
# 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.