StaticLibraries: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
(Imported from Redmine)
 
(Fix formatting - Includes, quotes)
 
Line 14: Line 14:
Create a simple application to use the static libraries like this that we will call '''test.cc''':
Create a simple application to use the static libraries like this that we will call '''test.cc''':


<pre>#include  
<pre>
#include  
#include <gnuradio/top_block.h>
#include  
#include <gnuradio/blocks/vector_source_f.h>
#include <gnuradio/blocks/file_sink.h>


int main(int agrc, char **argv)
int main(int agrc, char **argv)
Line 24: Line 25:
     data[i] = i;
     data[i] = i;
   }
   }
   gr::top_block_sptr tb = gr::make_top_block(&quot;test&quot;);
   gr::top_block_sptr tb = gr::make_top_block("test");
   gr::blocks::vector_source_f::sptr src = gr::blocks::vector_source_f::make(data, false);
   gr::blocks::vector_source_f::sptr src = gr::blocks::vector_source_f::make(data, false);
   gr::blocks::file_sink::sptr snk = gr::blocks::file_sink::make(sizeof(float),
   gr::blocks::file_sink::sptr snk = gr::blocks::file_sink::make(sizeof(float),
                                                                 &quot;/tmp/static.out&quot;);
                                                                 "/tmp/static.out");
   tb-&gt;connect(src, 0, snk, 0);
   tb-&gt;connect(src, 0, snk, 0);
   tb-&gt;run();
   tb-&gt;run();
Line 41: Line 42:
-llog4cpp -lboost_system -lboost_filesystem -lboost_thread -lpthread
-llog4cpp -lboost_system -lboost_filesystem -lboost_thread -lpthread
-lrt -o test</pre>
-lrt -o test</pre>
This assumes that the program's name is '''test.cc''' and that GNU Radio was installed into ''/opt/gr''. We would obviously want to use cmake or a better build tool to manage our actual projects in the end. Furthermore, we have to link against both Orc and Log4Cpp here. This is because we compiled VOLK with ORC support and we build the logging tools using Log4Cpp into GNU Radio. If we build without either of these libraries, we would not require them at the linking stage.
This assumes that the program's name is <code>test.cc</code> and that GNU Radio was installed into <code>/opt/gr</code>. We would obviously want to use cmake or a better build tool to manage our actual projects in the end. Furthermore, we have to link against both Orc and Log4Cpp here. This is because we compiled VOLK with ORC support and we build the logging tools using Log4Cpp into GNU Radio. If we build without either of these libraries, we would not require them at the linking stage.


Because our program used blocks from the gr-blocks component, we are required to link against '''libgnuradio-blocks.a''' along with '''libgnuradio-runtime.a''', the standard GNU Radio runtime library. The other libraries for PMTs, VOLK, and the Boost and system libraries are all direct dependencies of the runtime library, and so we must link against them, too. This, of course, means that we have static library versions of each of these libraries. In Ubuntu, these are usually installed with the ``-dev'' package in apt-get.
Because our program used blocks from the gr-blocks component, we are required to link against <code>libgnuradio-blocks.a</code> along with <code>libgnuradio-runtime.a</code>, the standard GNU Radio runtime library. The other libraries for PMTs, VOLK, and the Boost and system libraries are all direct dependencies of the runtime library, and so we must link against them, too. This, of course, means that we have static library versions of each of these libraries. In Ubuntu, these are usually installed with the <code>-dev</code> package in <code>apt-get</code>.


<pre>$ ls -l test
<pre>$ ls -l test

Latest revision as of 00:01, 21 March 2017

GNU Radio as Static Libraries

Building GNU Radio's Static Libs

In version 3.7.4, we have introduced the ability to build static GNU Radio libraries alongside the shared object libraries. There is no option to only build static libraries, only add them as a secondary output.

cmake -DENABLE_STATIC_LIBS=True  

This will build libgnuradio-XXX.a and install them next to libgnuradio-XXX.so.

Due to limitations with ZeroC's ICE, we cannot build both the static libraries and ControlPort. So if static builds are enabled, ControlPort is automatically disabled. With some proper tweaking, we can probably remove ControlPort only from the static linking stage while keeping it in the shared libraries, should the need arise.

Using the Static Libs

Create a simple application to use the static libraries like this that we will call test.cc:

#include <gnuradio/top_block.h>
#include <gnuradio/blocks/vector_source_f.h>
#include <gnuradio/blocks/file_sink.h> 

int main(int agrc, char **argv)
{
  std::vector data(10000);
  for(size_t i = 0; i < data.size(); i++) {
    data[i] = i;
  }
  gr::top_block_sptr tb = gr::make_top_block("test");
  gr::blocks::vector_source_f::sptr src = gr::blocks::vector_source_f::make(data, false);
  gr::blocks::file_sink::sptr snk = gr::blocks::file_sink::make(sizeof(float),
                                                                "/tmp/static.out");
  tb->connect(src, 0, snk, 0);
  tb->run();

  return 0;
}

This application simply creates ten thousand values in a vector source and writes them to the file /tmp/static.out. We must then
compile this and link in all of the necessary static libraries we use:

g++ test.cc -I/opt/gr/include -fpic -static \
-L/opt/gr/lib -L/usr/lib/x86_64-linux-gnu \
-lgnuradio-blocks -lgnuradio-runtime -lgnuradio-pmt -lvolk -lorc-0.4 \
-llog4cpp -lboost_system -lboost_filesystem -lboost_thread -lpthread
-lrt -o test

This assumes that the program's name is test.cc and that GNU Radio was installed into /opt/gr. We would obviously want to use cmake or a better build tool to manage our actual projects in the end. Furthermore, we have to link against both Orc and Log4Cpp here. This is because we compiled VOLK with ORC support and we build the logging tools using Log4Cpp into GNU Radio. If we build without either of these libraries, we would not require them at the linking stage.

Because our program used blocks from the gr-blocks component, we are required to link against libgnuradio-blocks.a along with libgnuradio-runtime.a, the standard GNU Radio runtime library. The other libraries for PMTs, VOLK, and the Boost and system libraries are all direct dependencies of the runtime library, and so we must link against them, too. This, of course, means that we have static library versions of each of these libraries. In Ubuntu, these are usually installed with the -dev package in apt-get.

$ ls -l test
-rwxrwxr-x 1 ubuntu ubuntu 24748765 Jul  7 14:35 test

$ file test
test: ELF 64-bit LSB  executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=a35e61043101342f9e8fd0d97c846ce8ec81c0c8, not stripped