1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Creating your own projects using CMake</title>
<link href="/site.css" rel="stylesheet" type="text/css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="search/search.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!--#include file="header.html" -->
<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.shtml"><span>Main Page</span></a></li>
<li class="current"><a href="pages.shtml"><span>Related Pages</span></a></li>
<li><a href="modules.shtml"><span>Modules</span></a></li>
<li><a href="namespaces.shtml"><span>Namespaces</span></a></li>
<li><a href="annotated.shtml"><span>Classes</span></a></li>
<li><a href="files.shtml"><span>Files</span></a></li>
<li><a href="examples.shtml"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Creating your own projects using CMake </div> </div>
</div>
<div class="contents">
<div class="textblock"><p>CMake provides modules to find a large number of modules to find common dependencies. A module to find openbabel is included in the release or can be copied below. The filename of modules to find packages must start with Find. For example, the default filename for the openbabel module is FindOpenBabel2.cmake. This file is usually placed in the cmake/modules directory of your project. This path must be specified by setting the CMAKE_MODULE_PATH variable. Next, calling find_package will execute the module to find openbabel and set 3 variables.</p>
<ul>
<li>OPENBABEL2_FOUND </li>
<li>OPENBABEL2_INCLUDE_DIR </li>
<li>OPENBABEL2_LIBRARIES</li>
</ul>
<p>The find_package command allows you to specify the package is required and cmake will handle this further. If openbabel is optional, the first variable can be used in your cmake logic to optionally build the additional code. Since find_package only sets variables, you still need to call include_directories with OPENBABEL2_INCLUDE_DIR in the argument list. The OPENBABEL2_LIBRARIES variable can be used directly in your target_link_libraries command.</p>
<p>Below is a minimal but working example of a project. For simplicity, only one CMakeLists.txt file and one source file is used. This can be used as a template to get started. The <a href="http://www.cmake.org/cmake/help/documentation.html">cmake documentation</a> can be consultated as your project becomes more complex.</p>
<p><b>CMakeLists.txt</b> </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"># this line is required for cmake backwards compatibility</span>
<span class="preprocessor"></span>cmake_minimum_required(VERSION 2.6)
<span class="preprocessor"># name of your project</span>
<span class="preprocessor"></span>project(myproject)
<span class="preprocessor"># set the module path</span>
<span class="preprocessor"></span><span class="keyword">set</span>(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
# find and setup openbabel
find_package(OpenBabel2 REQUIRED)
include_directories(${OPENBABEL2_INCLUDE_DIR})
# create a list of source files (easier to maintain)
<span class="keyword">set</span>(sources main.cpp)
# the executable
add_executable(myexe ${sources})
target_link_libraries(myexe ${OPENBABEL2_LIBRARIES})
install(TARGETS myexe DESTINATION bin)
</pre></div><p> <b>main.cpp</b> </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include <<a class="code" href="mol_8h.shtml" title="Handle molecules. Declarations of OBMol, OBAtom, OBBond, OBResidue. (the main header for Open Babel)...">openbabel/mol.h</a>></span>
<span class="keyword">using namespace </span>OpenBabel;
<span class="keywordtype">int</span> main()
{
<a class="code" href="classOpenBabel_1_1OBMol.shtml" title="Molecule Class.">OBMol</a> mol;
<span class="comment">// ... see examples for code ...</span>
<span class="keywordflow">return</span> 0;
}
</pre></div><p><b>cmake/modules/FindOpenBabel2.cmake</b> </p>
<div class="fragment"><pre class="fragment"><span class="preprocessor"># - Try to find OpenBabel2</span>
<span class="preprocessor"></span><span class="preprocessor"># Once done this will define</span>
<span class="preprocessor"></span><span class="preprocessor">#</span>
<span class="preprocessor"></span><span class="preprocessor"># OPENBABEL2_FOUND - system has OpenBabel2</span>
<span class="preprocessor"></span><span class="preprocessor"># OPENBABEL2_INCLUDE_DIR - the OpenBabel2 include directory</span>
<span class="preprocessor"></span><span class="preprocessor"># OPENBABEL2_LIBRARIES - Link these to use OpenBabel2</span>
<span class="preprocessor"></span><span class="preprocessor">#</span>
<span class="preprocessor"></span><span class="preprocessor"># Copyright (c) 2006, 2007 Carsten Niehaus, <cniehaus@gmx.de></span>
<span class="preprocessor"></span><span class="preprocessor"># Copyright (C) 2008 Marcus D. Hanwell <marcus@cryos.org></span>
<span class="preprocessor"></span><span class="preprocessor"># Redistribution and use is allowed according to the terms of the BSD license.</span>
<span class="preprocessor"></span><span class="preprocessor"># For details see the accompanying COPYING-CMAKE-SCRIPTS file.</span>
<span class="preprocessor"></span>
<span class="keywordflow">if</span> (OPENBABEL2_INCLUDE_DIR AND OPENBABEL2_LIBRARIES AND OPENBABEL2_VERSION_MET)
<span class="preprocessor"> # in cache already</span>
<span class="preprocessor"></span> <span class="keyword">set</span>(OPENBABEL2_FOUND TRUE)
<span class="keywordflow">else</span> (OPENBABEL2_INCLUDE_DIR AND OPENBABEL2_LIBRARIES AND OPENBABEL2_VERSION_MET)
<span class="keywordflow">if</span>(NOT WIN32)
<span class="preprocessor"> # Use the newer PkgConfig stuff</span>
<span class="preprocessor"></span> find_package(PkgConfig REQUIRED)
pkg_check_modules(OPENBABEL2 openbabel-2.0>=2.2.0)
<span class="preprocessor"># Maintain backwards compatibility with previous version of module</span>
<span class="preprocessor"></span> <span class="keywordflow">if</span>(OPENBABEL2_FOUND STREQUAL <span class="stringliteral">"1"</span>)
<span class="keyword">set</span>(OPENBABEL2_VERSION_MET TRUE)
<span class="keyword">set</span>(OPENBABEL2_INCLUDE_DIR ${OPENBABEL2_INCLUDE_DIRS})
endif(OPENBABEL2_FOUND STREQUAL <span class="stringliteral">"1"</span>)
<span class="keywordflow">else</span>(NOT WIN32)
<span class="keyword">set</span>(OPENBABEL2_VERSION_MET TRUE)
endif(NOT WIN32)
<span class="keywordflow">if</span>(OPENBABEL2_VERSION_MET)
<span class="keywordflow">if</span>(WIN32)
<span class="keywordflow">if</span>(NOT OPENBABEL2_INCLUDE_DIR)
find_path(OPENBABEL2_INCLUDE_DIR openbabel-2.0/openbabel/obconversion.h
PATHS
${_obIncDir}
${GNUWIN32_DIR}/include
$ENV{OPENBABEL2_INCLUDE_DIR}
)
<span class="keywordflow">if</span>(OPENBABEL2_INCLUDE_DIR)
set(OPENBABEL2_INCLUDE_DIR ${OPENBABEL2_INCLUDE_DIR}/openbabel-2.0)
endif(OPENBABEL2_INCLUDE_DIR)
endif(NOT OPENBABEL2_INCLUDE_DIR)
endif(WIN32)
find_library(OPENBABEL2_LIBRARIES NAMES openbabel openbabel-2
PATHS
${_obLinkDir}
${GNUWIN32_DIR}/lib
$ENV{OPENBABEL2_LIBRARIES}
)
endif(OPENBABEL2_VERSION_MET)
if(OPENBABEL2_INCLUDE_DIR AND OPENBABEL2_LIBRARIES AND OPENBABEL2_VERSION_MET)
set(OPENBABEL2_FOUND TRUE)
endif(OPENBABEL2_INCLUDE_DIR AND OPENBABEL2_LIBRARIES AND OPENBABEL2_VERSION_MET)
if (OPENBABEL2_FOUND)
if (NOT OpenBabel2_FIND_QUIETLY)
message(STATUS "Found OpenBabel 2.2 or later: ${OPENBABEL2_LIBRARIES}<span class="stringliteral">")</span>
<span class="stringliteral"> endif (NOT OpenBabel2_FIND_QUIETLY)</span>
<span class="stringliteral"> else (OPENBABEL2_FOUND)</span>
<span class="stringliteral"> if (OpenBabel2_FIND_REQUIRED)</span>
<span class="stringliteral"> message(FATAL_ERROR "</span>Could NOT find OpenBabel 2.2 or later <span class="stringliteral">")</span>
<span class="stringliteral"> endif (OpenBabel2_FIND_REQUIRED)</span>
<span class="stringliteral"> endif (OPENBABEL2_FOUND)</span>
<span class="stringliteral"></span>
<span class="stringliteral"> mark_as_advanced(OPENBABEL2_INCLUDE_DIR OPENBABEL2_LIBRARIES)</span>
<span class="stringliteral"></span>
<span class="stringliteral">endif (OPENBABEL2_INCLUDE_DIR AND OPENBABEL2_LIBRARIES AND OPENBABEL2_VERSION_MET)</span>
<span class="stringliteral"></span>
<span class="stringliteral"># Search for Open Babel2 executable</span>
<span class="stringliteral">if(OPENBABEL2_EXECUTABLE)</span>
<span class="stringliteral"></span>
<span class="stringliteral"> # in cache already</span>
<span class="stringliteral"> set(OPENBABEL2_EXECUTABLE_FOUND TRUE)</span>
<span class="stringliteral"></span>
<span class="stringliteral">else(OPENBABEL2_EXECUTABLE)</span>
<span class="stringliteral"> find_program(OPENBABEL2_EXECUTABLE NAMES babel</span>
<span class="stringliteral"> PATHS</span>
<span class="stringliteral"> [HKEY_CURRENT_USER\\SOFTWARE\\OpenBabel\ 2.2.0]</span>
<span class="stringliteral"> $ENV{OPENBABEL2_EXECUTABLE}</span>
<span class="stringliteral"> )</span>
<span class="stringliteral"></span>
<span class="stringliteral"> if(OPENBABEL2_EXECUTABLE)</span>
<span class="stringliteral"> set(OPENBABEL2_EXECUTABLE_FOUND TRUE)</span>
<span class="stringliteral"> endif(OPENBABEL2_EXECUTABLE)</span>
<span class="stringliteral"></span>
<span class="stringliteral"> if(OPENBABEL2_EXECUTABLE_FOUND)</span>
<span class="stringliteral"> message(STATUS "</span>Found OpenBabel2 executable: ${OPENBABEL2_EXECUTABLE}<span class="stringliteral">")</span>
<span class="stringliteral"> endif(OPENBABEL2_EXECUTABLE_FOUND)</span>
<span class="stringliteral"></span>
<span class="stringliteral">endif(OPENBABEL2_EXECUTABLE)</span>
</pre></div> </div></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- end content -->
<!--#include file="footer.html" -->
<div id="footer">
<hr size="1">
<img src="http://openbabel.org/babel256.png" width="136" height="127" alt="" style="float: left;" />
<p>This file is part of the documentation for <a href="http://openbabel.org/wiki/">Open Babel</a>, version 2.3.</p>
<div class="bottom">
Documentation copyright © 1998-2007, the <a href="http://openbabel.org/wiki/THANKS">Open Babel Developers</a>.<br>
Open Babel is hosted by: <a href="http://sourceforge.net">
<img src="http://sourceforge.net/sflogo.php?group_id=40728"
width="88" height="31" border="0" alt="SourceForge Logo"></a><br>
Generated on Thu Oct 13 2011 16:08:08 by <a href="http://www.doxygen.org/"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.7.5.1.
</div>
</body>
</html>
|