📄 adding_libraries.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head> <title></title> <link rel="stylesheet" media="screen" type="text/css" href="./style.css" /> <link rel="stylesheet" media="screen" type="text/css" href="./design.css" /> <link rel="stylesheet" media="print" type="text/css" href="./print.css" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><div class="toc"><div class="tocheader toctoggle" id="toc__header">Table of Contents</div><div id="toc__inside"><ul class="toc"><li class="level1"><div class="li"><span class="li"><a href="#adding_libraries" class="toc">Adding Libraries</a></span></div><ul class="toc"><li class="level2"><div class="li"><span class="li"><a href="#statically_linked_libraries" class="toc">Statically Linked Libraries</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#to_create_a_lib_which_can_be_used_for_statically_linking" class="toc">To Create a Lib which can be used for statically linking</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#using_the_new_library" class="toc">Using the New Library</a></span></div></li></ul></li><li class="level2"><div class="li"><span class="li"><a href="#create_shared_libraries" class="toc">Create Shared Libraries</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#enable_kernel_support_for_shared_flat_format" class="toc">Enable kernel support for Shared FLAT format</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#link_your_application_to_uclibc_shared_library" class="toc">Link your application to uClibc Shared Library</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#create_your_own_shared_library" class="toc">Create your own Shared Library</a></span></div></li></ul></li><li class="level2"><div class="li"><span class="li"><a href="#for_more_information_on_shared_library_for_uclinux" class="toc">For more information on Shared Library for uClinux</a></span></div></li></ul></li></ul></div></div><h1><a name="adding_libraries" id="adding_libraries">Adding Libraries</a></h1><div class="level1"><p> The library build is already included in the default system build.</p><p>You can add your own library components to the uClibc library. You can build your library either as statically linked library or shared library. </p></div><!-- SECTION [1-248] --><h2><a name="statically_linked_libraries" id="statically_linked_libraries">Statically Linked Libraries</a></h2><div class="level2"></div><!-- SECTION [249-289] --><h3><a name="to_create_a_lib_which_can_be_used_for_statically_linking" id="to_create_a_lib_which_can_be_used_for_statically_linking">To Create a Lib which can be used for statically linking</a></h3><div class="level3"><p> Follow the steps </p><ul><li class="level1"><div class="li"> create a new subdirectory in the uClibc directory</div></li></ul><pre class="code"> cd uClinux-dist mkdir uClibc/libtest</pre><ul><li class="level1"><div class="li"> Copy the librt Makefile and update the sources</div></li></ul><pre class="code">LIBTEST=libtest.aLIBTEST_SHARED=libtest.soLIBTEST_SHARED_FULLNAME=libtest-$(MAJOR_VERSION).$(MINOR_VERSION).$(SUBLEVEL).soCSRC=test1.c test2.c</pre><ul><li class="level1"><div class="li"> add to the uClibc Makefile</div></li></ul><pre class="code">DIRS = ldso libc libcrypt libresolv libnsl libutil libm libpthread librt\ libtest</pre><ul><li class="level1"><div class="li"> modify vendors/config/bfin/config.arch to include the new library</div></li></ul><pre class="code"> LIBTEST = -ltest LDPATH = \ -L$(ROOTDIR)/lib/$(LIBCDIR)/. \ -L$(ROOTDIR)/lib/$(LIBCDIR)/lib \ -L$(ROOTDIR)/lib/$(LIBCDIR)/libm \ -L$(ROOTDIR)/lib/libnet \ -L$(ROOTDIR)/lib/libtest </pre><ul><li class="level1"><div class="li"> build the libraries</div></li></ul><pre class="code"> make lib_only</pre></div><!-- SECTION [290-1305] --><h3><a name="using_the_new_library" id="using_the_new_library">Using the New Library</a></h3><div class="level3"><p> In your project Makefile add the extra library.</p><pre class="code">EXEC = helloOBJS = hello.oall: $(EXEC)$(EXEC): $(OBJS) $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBTEST) $(LDLIBS$(LDLIBS_$@))romfs: $(ROMFSINST) /bin/$(EXEC)clean: -rm -f $(EXEC) *.elf *.gdb *.o</pre></div><!-- SECTION [1306-1625] --><h2><a name="create_shared_libraries" id="create_shared_libraries">Create Shared Libraries</a></h2><div class="level2"></div><!-- SECTION [1626-1662] --><h3><a name="enable_kernel_support_for_shared_flat_format" id="enable_kernel_support_for_shared_flat_format">Enable kernel support for Shared FLAT format</a></h3><div class="level3"><p> To have shared library support, it is needed to have CONFIG_BINFMT_SHARED_FLT enabled in kernel configuration. Then rebuild the kernel.</p><pre class="code">under Customize Kernel Settings== Select Executable File Formats == Enable shared FLAT support </pre></div><!-- SECTION [1663-1981] --><h3><a name="link_your_application_to_uclibc_shared_library" id="link_your_application_to_uclibc_shared_library">Link your application to uClibc Shared Library</a></h3><div class="level3"><p> The uClibc Shared Library are built if kernel is configured for “shared FLAT support”. Please check the “/lib” directory in target:</p><pre class="code">root:~> ls -l /lib/-rwxr--r-- 1 1000 100 202508 lib1.so-rwxr--r-- 1 1000 100 242400 lib2.sodrwxr-xr-x 3 1000 100 1024 modules</pre><ul><li class="level1"><div class="li"> Create a source file e.g. main_uclibc.c to use shared uClibc library. The source code bellow calls uClibc libary:<pre class="code c"><span class="co1">// main_uclibc.c</span><span class="co2">#include "stdlib.h"</span><span class="co2">#include "stdio.h"</span>main<span class="br0">(</span><span class="br0">)</span><span class="br0">{</span> <span class="kw4">char</span> i,j; i= <span class="st0">'*'</span>; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">"Print a char:<span class="es0">\n</span>"</span><span class="br0">)</span>; putchar<span class="br0">(</span>i<span class="br0">)</span>; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">"<span class="es0">\n</span>Testing using uClibc shared library passes!<span class="es0">\n</span>"</span><span class="br0">)</span>;<span class="br0">}</span></pre></div></li></ul><ul><li class="level1"><div class="li"> Compile the source code on host machine (<uClinux-dist> stands for the source tree of your Blackfin uClinux):<pre class="code"> adam@linux:~> bfin-uclinux-gcc -g -mid-shared-library -mshared-library-id=0 -c -o \ main.o main_uclibc.c</pre></div></li></ul><ul><li class="level1"><div class="li"> Link to shared library. For the details of the options, please refer to GCC and LD manual.<pre class="code"> adam@linux:~> bfin-uclinux-gcc -g -Wl,-elf2flt -Wl,-shared-lib-id,0 \ -mid-shared-library -o main main.o \ -Wl,-R,<uClinux-dist>/uClibc/lib/libc.gdb \ -L<uClinux-dist>/uClibc/lib/ -lc</pre></div></li><li class="level1"><div class="li"> Boot the kernel with Shared FLAT support, and run your application.</div></li></ul></div><!-- SECTION [1982-3304] --><h3><a name="create_your_own_shared_library" id="create_your_own_shared_library">Create your own Shared Library</a></h3><div class="level3"></div><h4><a name="create_library_source" id="create_library_source">Create library source:</a></h4><div class="level4"><p>my_libA.c </p><pre class="code c"><span class="co2">#include "my_lib.h"</span><span class="co2">#include <stdio.h></span> <span class="kw4">int</span> foo_A<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span><span class="br0">{</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">"foo_A called: %d<span class="es0">\n</span>"</span>, i<span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span></pre><p> my_libB.c </p><pre class="code c"><span class="co2">#include "my_lib.h"</span><span class="co2">#include <stdio.h></span> <span class="kw4">int</span> foo_B<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span><span class="br0">{</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">"foo_B called: %d<span class="es0">\n</span>"</span>, i<span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span></pre><p> my_lib.h </p><pre class="code c"><span class="co2">#ifndef __MY_LIB_H</span><span class="co2">#define __MY_LIB_H</span><span class="kw4">int</span> foo_A<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span>;<span class="kw4">int</span> foo_B<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span>;<span class="co2">#endif</span></pre></div><h4><a name="build_the_shared_library" id="build_the_shared_library">Build the shared library</a></h4><div class="level4"><ul><li class="level1"><div class="li"> Compile the source and create object file: <pre class="code">adam@linux:~> bfin-uclinux-gcc -O3 -mid-shared-library -mshared-library-id=3 -c -o my_libA.o my_libA.cadam@linux:~> bfin-uclinux-gcc -O3 -mid-shared-library -mshared-library-id=3 -c -o my_libB.o my_libB.cadam@linux:~> bfin-uclinux-ar -r libmylib.a my_libA.o my_libB.o</pre></div></li></ul><ul><li class="level1"><div class="li"> Create the shared library: libmylib and libmylib.gdb are generated. We assign Library ID “3” to the shared library “libmylib” <pre class="code">adam@linux:~> bfin-uclinux-gcc -o libmylib -O3 \ -mid-shared-library -mshared-library-id=3 \ -Wl,-elf2flt -nodefaultlibs -Wl,-shared-lib-id,3 \ /home/adam/workspace/kernel/2005R4/uClinux-dist/uClibc/lib/main.o \ -Wl,--whole-archive,libmylib.a,--no-whole-archive \ -Wl,-R,/home/adam/workspace/kernel/2005R4/uClinux-dist/lib/uClibc/libc.gdb -lc </pre></div></li></ul><ul><li class="level1"><div class="li"> Localize some special symbols in libmylib.gdb, so they are invisible externally<pre class="code">adam@linux:~> bfin-uclinux-objcopy -L _GLOBAL_OFFSET_TABLE_ -L main \ -L __main -L _start -L _main -L ___main -L __start -L __uClibc_main \ -L __uClibc_start_main -L lib_main -L ___uClibc_main -L ___uClibc_start_main \ -L _lib_main -L _exit_dummy_ref -L __do_global_dtors -L __do_global_ctors \ -L __CTOR_LIST__ -L __DTOR_LIST__ -L __exit_dummy_ref -L ___do_global_dtors \ -L ___do_global_ctors -L ___CTOR_LIST__ -L ___DTOR_LIST__ \ -L _current_shared_library_a5_offset_ -L _current_shared_library_p5_offset_ libmylib.gdb</pre></div></li></ul></div><h4><a name="download_the_shared_libary_to_target" id="download_the_shared_libary_to_target">Download the shared libary to target</a></h4><div class="level4"><p> Now the shared library is ready to use. You can load the library to your target. For example, your libary is downloaded to target as “/usr/mylib/libmylib”:</p><pre class="code">root:~> cp /usr/mylib/libmylib /lib/lib3.so </pre><p> You can also make a symbol link instead of rename:</p><pre class="code">root:~> ln -s /usr/mylib/libmylib /lib/lib3.so</pre><p> ‘’ ‘’“” </p></div><h4><a name="link_your_application_with_the_shared_library" id="link_your_application_with_the_shared_library">Link your application with the Shared Library</a></h4><div class="level4"><ul><li class="level1"><div class="li"> Create application using the library funtions: my_test.c<pre class="code c"><span class="co2">#include <stdio.h></span><span class="co2">#include "my_lib.h"</span> <span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span><span class="br0">{</span> foo_A<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span>; foo_B<span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span>; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">"my_test finished<span class="es0">\n</span>"</span><span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span></pre></div></li><li class="level1"><div class="li"> Link with shared libary: libmylib<pre class="code">adam@linux:~> bfin-uclinux-gcc my_test.c -o my_test -O3 -Wl,-elf2flt -mid-shared-library -mshared-library-id=0 \-Wl,-shared-lib-id,0 -Wl,-R/home/adam/workspace/kernel/2005R4/uClinux-dist/uClibc/lib/libc.gdb \-L/home/adam/workspace/kernel/2005R4/uClinux-dist/uClibc/lib/ -lc -Wl,-Rlibmylib.gdb </pre></div></li><li class="level1"><div class="li"> Download the application to target and run. It will call the funtions in shared library:<pre class="code">root:~> ./my_testfoo_A called: 1foo_B called: 2my_test finished</pre></div></li></ul></div><!-- SECTION [3305-6758] --><h2><a name="for_more_information_on_shared_library_for_uclinux" id="for_more_information_on_shared_library_for_uclinux">For more information on Shared Library for uClinux</a></h2><div class="level2"><ul><li class="level1"><div class="li"> <a href="http://www.ucdot.org/article.pl?sid=03/11/25/1126257&mode=thread" class="urlextern" title="http://www.ucdot.org/article.pl?sid=03/11/25/1126257&mode=thread" rel="nofollow">Shared libraries under uClinux mini-HOWTO</a> </div></li></ul></div><!-- SECTION [6759-] --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -