blob: feba2021798801cccd0b314e26fa7e68fee9d1e8 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Creating a Simple Bundle / Apache Celix</title>
<link rel="icon" href="/assets/img/favicon.ico">
<link href="/assets/css/bootstrap.min.css" rel="stylesheet">
<link href="/assets/css/style.css" rel="stylesheet">
<style>
.card-body img {
max-width: 100%;
width: 100%;
height: auto;
}
.card-body img + em {
text-decoration: underline;
}
</style>
<script>
var _paq = window._paq = window._paq || [];
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '9']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
</head>
<body class="light-grey">
<a href="https://github.com/apache/celix" class="github-ribbon">
<img src="/assets/img/forkme_right_red_aa0000.png" alt="Fork me on GitHub">
</a>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
<div class="container">
<a class="navbar-brand" href="/">
<img src="/assets/img/celix-white.svg" height="40" class="d-inline-block align-top" alt="Celix Logo">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/download.cgi">Download</a>
</li>
<li class="nav-item dropdown active">
<a class="nav-link dropdown-toggle" href="#" id="ddDocs" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Docs
</a>
<div class="dropdown-menu" aria-labelledby="ddDocs">
<a class="dropdown-item" href="/docs/2.4.0/docs.html">2.4.0 (latest)</a>
<a class="dropdown-item" href="/docs/2.3.0/docs.html">2.3.0</a>
<a class="dropdown-item" href="/docs/2.2.1/docs.html">2.2.1</a>
<a class="dropdown-item" href="/docs/2.1.0/docs.html">2.1.0</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="ddContributing" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Contributing
</a>
<div class="dropdown-menu" aria-labelledby="ddContributing">
<a class="dropdown-item" href="/contributing/youatcelix.html">You at Celix</a>
<a class="dropdown-item" href="/contributing/submitting-patches.html">Submitting patches</a>
<a class="dropdown-item" href="/contributing/source-and-builds.html">Source code and builds</a>
<hr>
<a class="dropdown-item" href="/contributing/releasing.html">Releasing</a>
<a class="dropdown-item" href="/contributing/volunteers.html">Volunteers</a>
<a class="dropdown-item" href="https://whimsy.apache.org/board/minutes/Celix.html">Board Reports</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="ddSupport" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Support
</a>
<div class="dropdown-menu" aria-labelledby="ddSupport">
<a class="dropdown-item" href="/support/mailing-list.html">Mailing Lists</a>
<a class="dropdown-item" href="/support/issue-tracking.html">Issue Tracking</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="ddFoundation" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
ASF
</a>
<div class="dropdown-menu" aria-labelledby="ddFoundation">
<a class="dropdown-item" href="https://www.apache.org/">ASF Home</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/how-it-works.html">How it works</a>
<a class="dropdown-item" href="https://www.apache.org/licenses/">License</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a>
<a class="dropdown-item" href="https://www.apache.org/security/">Security</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/policies/conduct">Code of Conduct</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
<div class="section">
<div class="container">
<div class="row py-4">
<div class="col-sm-12 card">
<div class="card-body pt-5">
<a href="/docs/2.2.1/docs.html" title="back to documentation">&lt;&lt; back to documentation</a>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<h1 id="apache-celix---getting-started-guide-creating-a-simple-bundle">Apache Celix - Getting Started Guide: Creating a Simple Bundle</h1>
<h2 id="intro">Intro</h2>
<p>This page is intended for first time users of Apache Celix. It should guide you through building &amp; installing Apache Celix, setting up a new project, creating your first bundle, setting up the project for use with Eclipse project and finally running and debugging your bundle directly from eclipse workspace.</p>
<p>If there are any uncertainties or question, don&rsquo;t hesitate to ask your questions on the <a href="https://celix.apache.org/support/mailing-list.html">Apache Celix mailing list</a>.</p>
<h2 id="prerequisite">Prerequisite</h2>
<p>Some experience with a command line interface (xterm) is expected to be able to follow this guide.</p>
<h2 id="building-and-installing">Building and Installing</h2>
<p>For Apache Celix see <a href="../building/README.html">Building And Installing</a></p>
<h2 id="installing-eclipse-cdt">Installing Eclipse CDT</h2>
<p>Download the latest eclipse CDT at <a href="http://www.eclipse.org">http://www.eclipse.org</a> and install it on your system. For more information on how the install eclipse on your system consult the eclipse documentation. For this getting started guide the luna version of eclipse was used (<a href="http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/R/eclipse-cpp-luna-R-linux-gtk-x86_64.tar.gz">linux</a> <a href="http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/R/eclipse-cpp-luna-R-macosx-cocoa-x86_64.tar.gz">mac</a>).</p>
<h2 id="apache-celix-bundle-project">Apache Celix Bundle project</h2>
<p>Now that Apache Celix and Eclipse is installed, we are ready to create a new Apache Celix Bundle project.
CMake is used as build tool for Apache Celix projects.</p>
<p>To setup of the project, first create a new project dir to work in:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#080;font-style:italic">#Create a new workspace to work in, e.g:</span>
</span></span><span style="display:flex;"><span>mkdir <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">HOME</span><span style="color:#b68;font-weight:bold">}</span>/workspace
</span></span><span style="display:flex;"><span><span style="color:#a2f">export</span> <span style="color:#b8860b">WS</span><span style="color:#666">=</span><span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">HOME</span><span style="color:#b68;font-weight:bold">}</span>/workspace
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mkdir <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">WS</span><span style="color:#b68;font-weight:bold">}</span>/myproject
</span></span><span style="display:flex;"><span><span style="color:#a2f">cd</span> <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">WS</span><span style="color:#b68;font-weight:bold">}</span>/myproject
</span></span></code></pre></div><p>Then create a CMakeLists.txt file - the makefile variant of CMake -in project root directory:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cmake" data-lang="cmake"><span style="display:flex;"><span><span style="color:#080;font-style:italic">#${WS}/myproject/CMakeLists.txt
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> <span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#080;font-style:italic">#Part 1. setup project
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f">cmake_minimum_required</span>(<span style="color:#b44">VERSION</span> <span style="color:#b44">3.4</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">project</span>(<span style="color:#b44">myproject</span> <span style="color:#b44">C</span> <span style="color:#b44">CXX</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#080;font-style:italic">#Part 2. setup compilers
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f">SET</span>(<span style="color:#b44">CMAKE_C_FLAGS</span> <span style="color:#b44">&#34;-D_GNU_SOURCE -std=gnu99 -Wall -Werror -fPIC ${CMAKE_C_FLAGS}&#34;</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">SET</span>(<span style="color:#b44">CMAKE_C_FLAGS_DEBUG</span> <span style="color:#b44">&#34;-g -DDEBUG&#34;</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">SET</span>(<span style="color:#b44">CMAKE_CXX_FLAGS</span> <span style="color:#b44">&#34;-std=c++11 ${CMAKE_CXX_FLAGS}&#34;</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">SET</span>(<span style="color:#b44">CMAKE_CXX_FLAGS_DEBUG</span> <span style="color:#b44">&#34;-g -DDEBUG&#34;</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#080;font-style:italic">#Part 3. Setup Celix cmake files, include paths, libraries and library paths
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic">#Note. If celix is not installed in /usr/local dir, change the location accordingly.
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f">set</span>(<span style="color:#b44">CMAKE_MODULE_PATH</span> <span style="color:#666">${</span><span style="color:#b8860b">CMAKE_MODULE_PATH</span><span style="color:#666">}</span> <span style="color:#b44">&#34;/usr/local/share/celix/cmake/modules&#34;</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">find_package</span>(<span style="color:#b44">CELIX</span> <span style="color:#b44">REQUIRED</span>)<span style="">
</span></span></span><span style="display:flex;"><span><span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#080;font-style:italic">#Part 4. Choose C, C++ or both
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f">add_subdirectory</span>(<span style="color:#b44">bundles/HelloWorld_c</span>) <span style="color:#080;font-style:italic">#C
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f">add_subdirectory</span>(<span style="color:#b44">bundles/HelloWorld_cxx</span>) <span style="color:#080;font-style:italic">#C++
</span></span></span></code></pre></div><p>This CMakeLists.txt file, sets up the following:</p>
<ul>
<li>Part 1
<ul>
<li>The minimum cmake version required.</li>
<li>The project name</li>
<li>The type of source files to expect, in this case C and C++.</li>
</ul>
</li>
<li>Part 2
<ul>
<li>Setup the compilers. c99 for C and C++11 for C++</li>
</ul>
</li>
<li>Part 3
<ul>
<li>The Celix package should be searched, configured and that the Celix package is required.</li>
<li>For all build targets in this CMakeLists.txt file or any sub directory CMakeLists.txt files the Apache Celix headers directory should be included.</li>
</ul>
</li>
<li>Part 4
<ul>
<li>The CMakelists.txt file in the subdirectory bundles/export_import and/or bundles/HelloWorld should also be processed.</li>
</ul>
</li>
</ul>
<p>It is a good practice to create a separate CMakeLists.txt file for every bundle you want to build. For the export_import bundle a CMakeLists.txt file should be created in the bundles/export_import sub directory.</p>
<p>Create the sub directory:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#080;font-style:italic"># Create directory structure for the hello_world bundles</span>
</span></span><span style="display:flex;"><span><span style="color:#a2f">cd</span> <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">WS</span><span style="color:#b68;font-weight:bold">}</span>/myproject
</span></span><span style="display:flex;"><span>mkdir -p bundles/HelloWorld_c/src
</span></span><span style="display:flex;"><span>mkdir -p bundles/HelloWorld_cxx/src
</span></span></code></pre></div><p>And add the following CMakeLists.txt file for the C Bundle:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-CMake" data-lang="CMake"><span style="display:flex;"><span><span style="color:#080;font-style:italic">#${WS}/myproject/bundles/HelloWorld_c/CMakeLists.txt
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">add_celix_bundle</span>(<span style="color:#b44">HelloWorld_c</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">VERSION</span> <span style="color:#b44">1.0.0</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">SOURCES</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">src/HelloWorld_activator.c</span>
</span></span><span style="display:flex;"><span>) <span style="">
</span></span></span></code></pre></div><p>And/or the following CMakeLists.txt for the C++ bundle:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-CMake" data-lang="CMake"><span style="display:flex;"><span><span style="color:#080;font-style:italic">#${WS}/myproject/bundles/HelloWorld_cxx/CMakeLists.txt
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="">
</span></span></span><span style="display:flex;"><span><span style=""></span><span style="color:#a2f">add_celix_bundle</span>(<span style="color:#b44">HelloWorld_cxx</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">VERSION</span> <span style="color:#b44">1.0.0</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">SOURCES</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">src/HelloWorldActivator.cc</span>
</span></span><span style="display:flex;"><span>)<span style="">
</span></span></span></code></pre></div><p>These CMakeLists.txt files declare that the bundles should be build based on the build result (shared library) of the declared sources (in this case the <code>src/hello_world_activator.c</code> or <code>src/HelloWorldActivator.cc</code> source).
The <code>add_celix_bundle</code> CMake function is an Apache Celix specific CMake extension.</p>
<p>The Celix framework will install the bundle, load the bundle shared library and call the bundle activator entry symbols. These entries need to be programmed by the user.
Note that in these examples we use the CELIX_GEN_BUNDLE_ACTIVATOR and CELIX_GEN_CXX_BUNDLE_ACTIVATOR
to generate the bundle activator functions (and as result the symbols needed for the Celix framework); although not necessary, this prevents the need for writing some boiler plating code.</p>
<p>The C Bundle Activator:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-C" data-lang="C"><span style="display:flex;"><span><span style="color:#080;font-style:italic">//${WS}/myproject/bundles/hello_world/src/HelloWorld_activator.c
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#080">#include</span> <span style="color:#080">&lt;stdio.h&gt;</span><span style="color:#080">
</span></span></span><span style="display:flex;"><span><span style="color:#080">#include</span> <span style="color:#080">&lt;celix_api.h&gt;</span><span style="color:#080">
</span></span></span><span style="display:flex;"><span><span style="color:#080"></span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">typedef</span> <span style="color:#a2f;font-weight:bold">struct</span> activator_data {
</span></span><span style="display:flex;"><span> <span style="color:#080;font-style:italic">/*intentional empty*/</span>
</span></span><span style="display:flex;"><span>} <span style="color:#0b0;font-weight:bold">activator_data_t</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">static</span> <span style="color:#0b0;font-weight:bold">celix_status_t</span> <span style="color:#00a000">activator_start</span>(<span style="color:#0b0;font-weight:bold">activator_data_t</span> <span style="color:#666">*</span>data, <span style="color:#0b0;font-weight:bold">celix_bundle_context_t</span> <span style="color:#666">*</span>ctx) {
</span></span><span style="display:flex;"><span> <span style="color:#00a000">printf</span>(<span style="color:#b44">&#34;Hello world from C bundle with id %li</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#b44">&#34;</span>, <span style="color:#00a000">celix_bundle_getId</span>(<span style="color:#00a000">celix_bundleContext_getBundle</span>(ctx)));
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">return</span> CELIX_SUCCESS;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">static</span> <span style="color:#0b0;font-weight:bold">celix_status_t</span> <span style="color:#00a000">activator_stop</span>(<span style="color:#0b0;font-weight:bold">activator_data_t</span> <span style="color:#666">*</span>data, <span style="color:#0b0;font-weight:bold">celix_bundle_context_t</span> <span style="color:#666">*</span>ctx) {
</span></span><span style="display:flex;"><span> <span style="color:#00a000">printf</span>(<span style="color:#b44">&#34;Goodbye world from C bundle with id %li</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#b44">&#34;</span>, <span style="color:#00a000">celix_bundle_getId</span>(<span style="color:#00a000">celix_bundleContext_getBundle</span>(ctx)));
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">return</span> CELIX_SUCCESS;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a000">CELIX_GEN_BUNDLE_ACTIVATOR</span>(<span style="color:#0b0;font-weight:bold">activator_data_t</span>, activator_start, activator_stop)
</span></span></code></pre></div><p>The C++ Bundle Activator:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-C++" data-lang="C++"><span style="display:flex;"><span><span style="color:#080;font-style:italic">//${WS}/myproject/bundles/HelloWorld/private/src/HelloWorldActivator.cc
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#080">#include</span> <span style="color:#080">&lt;memory&gt;</span><span style="color:#080">
</span></span></span><span style="display:flex;"><span><span style="color:#080">#include</span> <span style="color:#080">&lt;iostream&gt;</span><span style="color:#080">
</span></span></span><span style="display:flex;"><span><span style="color:#080"></span>
</span></span><span style="display:flex;"><span><span style="color:#080">#include</span> <span style="color:#080">&lt;celix_api.h&gt;</span><span style="color:#080">
</span></span></span><span style="display:flex;"><span><span style="color:#080"></span>
</span></span><span style="display:flex;"><span><span style="color:#a2f;font-weight:bold">namespace</span> <span style="color:#080;font-style:italic">/*anon*/</span> {
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">BundleActivator</span> {
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">public</span><span style="color:#666">:</span>
</span></span><span style="display:flex;"><span> BundleActivator(std<span style="color:#666">::</span>shared_ptr<span style="color:#666">&lt;</span>celix<span style="color:#666">::</span>dm<span style="color:#666">::</span>DependencyManager<span style="color:#666">&gt;</span> _mng) <span style="color:#666">:</span> mng{_mng} {
</span></span><span style="display:flex;"><span> std<span style="color:#666">::</span>cout <span style="color:#666">&lt;&lt;</span> <span style="color:#b44">&#34;Hello world from C++ bundle with id &#34;</span> <span style="color:#666">&lt;&lt;</span> bndId() <span style="color:#666">&lt;&lt;</span> std<span style="color:#666">::</span>endl;
</span></span><span style="display:flex;"><span> }
</span></span><span style="display:flex;"><span> <span style="color:#666">~</span>BundleActivator() {
</span></span><span style="display:flex;"><span> std<span style="color:#666">::</span>cout <span style="color:#666">&lt;&lt;</span> <span style="color:#b44">&#34;Goodbye world from C++ bundle with id &#34;</span> <span style="color:#666">&lt;&lt;</span> bndId() <span style="color:#666">&lt;&lt;</span> std<span style="color:#666">::</span>endl;
</span></span><span style="display:flex;"><span> }
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">private</span><span style="color:#666">:</span>
</span></span><span style="display:flex;"><span> <span style="color:#0b0;font-weight:bold">long</span> bndId() <span style="color:#a2f;font-weight:bold">const</span> {
</span></span><span style="display:flex;"><span> <span style="color:#a2f;font-weight:bold">return</span> <span style="color:#00a000">celix_bundle_getId</span>(celix_bundleContext_getBundle(mng<span style="color:#666">-&gt;</span>bundleContext()));
</span></span><span style="display:flex;"><span> }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> std<span style="color:#666">::</span>shared_ptr<span style="color:#666">&lt;</span>celix<span style="color:#666">::</span>dm<span style="color:#666">::</span>DependencyManager<span style="color:#666">&gt;</span> mng;
</span></span><span style="display:flex;"><span> };
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>CELIX_GEN_CXX_BUNDLE_ACTIVATOR(BundleActivator)
</span></span></code></pre></div><h3 id="building">Building</h3>
<p>One of the highly recommended features of CMake is the ability to do out of source builds, the benefit is that all of the build results will go in a separate directory without cluttering the (source) project.
CMake also needs to able to find the cmake files Celix provides. This can be achieved by providing a CMAKE_MODULE_PATH variable (or setting the CMAKE_MODULE_PATH in the top level CMakeLists.txt).
For this example it is assumed that Celix in installed in <code>/usr/local</code>.
To create the build directory and build the project execute the following commands:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#a2f">cd</span> <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">WS</span><span style="color:#b68;font-weight:bold">}</span>
</span></span><span style="display:flex;"><span>mkdir myproject-build
</span></span><span style="display:flex;"><span><span style="color:#a2f">cd</span> myproject-build
</span></span><span style="display:flex;"><span>cmake ../myproject
</span></span><span style="display:flex;"><span>make all
</span></span></code></pre></div><p>Hopefully you will some some build results scrolling over the screen and actual build results in the build directory.
There should be a HelloWorld_c.zip in the bundles/HelloWorld_c directory and a HelloWorld_cxx.zip in the bundles/HelloWorld_cxx directory,
these are the actual bundles.
A bundle on its own has no real value, so lets setup a Celix container and run Celix with these bundles.</p>
<h3 id="running">Running</h3>
<p>To create a deployment for the hello world bundles two things are needed:</p>
<ol>
<li>Add a <code>add_celix_container</code> statement in the (top level) <code>CMakeLists.txt</code> file declaring what to deploy and under which name.</li>
</ol>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-CMake" data-lang="CMake"><span style="display:flex;"><span><span style="color:#080;font-style:italic">#${WS}/myproject/CMakeLists.txt
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span><span style="color:#a2f">add_celix_container</span>(<span style="color:#b44">myproject</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">CXX</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">BUNDLES</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">Celix::shell</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">Celix::shell_tui</span>
</span></span><span style="display:flex;"><span> <span style="color:#b44">HelloWorld_c</span> <span style="color:#080;font-style:italic">#C bundle
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span> <span style="color:#b44">HelloWorld_cxx</span> <span style="color:#080;font-style:italic">#C++ bundle
</span></span></span><span style="display:flex;"><span><span style="color:#080;font-style:italic"></span>) <span style="">
</span></span></span></code></pre></div><p>With the <code>add_celix_container</code> CMake function a Celix container will be configured, which bundles to use can be specified with absolute paths to
bundle files (e.g. the shell.zip bundle) or Celix bundle CMake target (e.g. the HelloWorld_c bundle).</p>
<p>Rerun make again form the build project. the make files generated by CMake will ensure cmake is run it again to update the actual make files.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#a2f">cd</span> <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">WS</span><span style="color:#b68;font-weight:bold">}</span>/myproject-build
</span></span><span style="display:flex;"><span>make -j
</span></span></code></pre></div><p>Now a deploy directory myproject should be available in the deploy directory. This directory contains - among other files - a release.sh script. This can be used to setup the required environment variables (like LD_LIBRARY_PATH).</p>
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#a2f">cd</span> <span style="color:#b68;font-weight:bold">${</span><span style="color:#b8860b">WS</span><span style="color:#b68;font-weight:bold">}</span>/myproject-build/deploy/myproject
</span></span><span style="display:flex;"><span>./myproject
</span></span></code></pre></div><p>The HelloWorld_c and HelloWorld_cxx bundles should be started with their own famous &ldquo;Hello World&rdquo; text variant printed. One for the C and one for the C++ bundle.
The shell and shell_tui bundle are also deployed and these can be used to query and control the running framework. Below some commands are shown for querying the installed bundles, listing all known shell command, showing the help of a specific command and stopping a specific bundle (note that bundle 0 is the framework &ldquo;bundle&rdquo;):</p>
<pre tabindex="0"><code>lb
help
stop 0
</code></pre><h2 id="apache-celix-projects-in-eclipse">Apache Celix Projects in Eclipse</h2>
<p>A nice feature of CMake is the ability to generate Eclipse project files, with this feature bundles can also be developed with use of Eclipse. This should help speed up the development process.
To get started change directory to the build directory and generate a eclipse project file.</p>
<pre><code>cd ${WS}/myproject-build
cmake -G &quot;Eclipse CDT4 - Unix Makefiles&quot; .
</code></pre>
<p>Startup the Eclipse EDI and a chose the <code>${WS}</code></p>
<p><img src="getting_started_img1.png" alt="select workspace"></p>
<p>Import the project with existing project.</p>
<p><img src="getting_started_img2.png" alt="import project"></p>
<p>To build the project, use Project-&gt;Build All.
To run or debug from Eclipse navigate to the myproject deploy directory and right click on
the &lsquo;myproject&rsquo; executable and as &ldquo;Local C/C++ Application&rdquo;</p>
<h2 id="apache-celix-projects-in-clion">Apache Celix Projects in CLion</h2>
<p>Using Apache Celix projects in CLion quite easy.
Just use <code>File -&gt; Open ...</code> and select a Apache Celix project.
Because CLion is a IDE for CMake projects and Apache Celix projects are CMake projects this works out of the box.</p>
<p>To run a Celix container just select the target from CLion and press Run.</p>
<h2 id="next">Next</h2>
<p>The get a complete overview of the available Celix CMake commands see:</p>
<ul>
<li><a href="../cmake_commands/README.html">Apache Celix - Celix CMake Commands</a></li>
</ul>
<p>The idea behind service oriented programming is that functionality is provided and used by abstract service, which hide implementation details.
For a guide how to provide and use services see</p>
<ul>
<li><a href="using_services_with_c.html">Apache Celix - Getting Started Guide: Using Services with C</a></li>
<li><a href="using_services_with_cxx.html">Apache Celix - Getting Started Guide: Using Services with C++</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<footer class="py-3 bg-secondary">
<div class="container">
<div class="row">
<div class="col-md-8 text-center">
<p class="m-0 text-white">
Copyright &copy; 2023 The Apache Software Foundation, Licensed under
the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.
<br>
Apache Celix, Celix, Apache, the Apache feather logo and the Apache Celix logo are trademarks of The Apache Software Foundation.
</p>
</div>
<div class="col-md-4 text-center">
<a href="https://www.apache.org/events/current-event.html" target="_blank">
<img src="https://www.apache.org/events/current-event-234x60.png" title="Apache Event" width="234" height="60" border="0">
</a>
</div>
</div>
</div>
</footer>
<script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/bootstrap.bundle.min.js"></script>
</body>
</html>