Linux Learning notes-how to use shared libraries when cross-compiling

Source: Internet
Author: User
Tags sha1

0. PrefaceIn more complex projects, a cross-compiled shared library (*.so file) is used.    In this case, the following question arises, such as whether the shared library at the "1" cross-compile time needs to be placed in the target board, if it needs to be placed in which directory.    Whether the shared library at the "2" cross-compile time needs to be placed in the host, if it needs to be placed in which directory.        "3" cross-compile how to specify how the shared library "4" program runs to find shared libraries and so on. This article summarizes the general approach to using shared libraries and illustrates the problem with an example.    If you already have a cross-compiled shared library, you can start with "2", step "1" just to illustrate the problem and fabricate a simple shared library to try to illustrate the problem. "1" cross-compilation Get the link library "2" cross-compile source file and join the link library "3" Mobile dynamic Link Library "4" execution
"Necessary Instructions" "Host" Ubuntu 14.04 AMD64 "target Board" Raspberry Pi "related blog post" "Example of Makefile Index blog" "Raspberry Pi Study notes-cross-compilation tool chain"
The Code warehouse--makefile-example code warehouse is located in BitBucket, where you can clone code with TORTOISEHG (GUI tools) or download a zip package directly from a Web page.
1. Cross-compile to get the dynamic link library
This example first makes a very simple shared library with two Api--add and sub in a shared library. "Libtest.h" Specifies the interface, giving the corresponding declaration #ifndef __libtest_h#define __libtest_hint Sub (int a, int b); int add (int a, int b); #endif "tes    T-ADD.C "int Add (int a, int b) {return a+b;}    "TEST-SUB.C" int sub (int a, int b) {return a-A;} "Makefile" contains the makefile file in the same directory, replace it with [tab] and take the makefile file in the code warehouse as the main. After the compilation is complete, move the libtest.so to the parent Lib directory. Note that the cross tool chain at this point is ARM-LINUX-GNUEABIHF-GCC, and Target B
# directive compiler and Option CC = arm-linux-gnueabihf-gcccflags =-wall-std=gnu99# destination file target = libtest.so# c file SRCs = TEST-ADD.C test-sub.c# Target file Objs = $ (SRCS:.C=.O) # link as executable $ (target): $ (OBJS) [tab]$ (CC)-shared-o [email protected] $^[TAB]MV $ (target). /LIBCLEAN:[TAB]RM-RF $ (target) $ (OBJS) # compilation rule [email protected] on behalf of the destination file $< represents the first dependent file%.o:%.c[tab]$ (CC) $ (CFLAGS)-O [Emai    L protected]-fpic-c $< "Necessary Authentication" use the file directive to view libtest.so information. File Libtest.solibtest.so:ELF 32-bit LSBshared Object, ARM, EABI5Version 1 (SYSV), dynamically linked, buildid[sha1]=e22558b8cf089b92e5534b636c6d501f1cc54581, not stripped
From the console output information can be seen, libtest.so run on the arm platform, not the host's AMD64 platform.

2. Cross-compiling the source file and joining the dynamic-link library    Source file #include <stdio.h> #include <libtest.h>int main (void) {    int a = 3;    int B = 2;           printf ("a=%d\n", a);    printf ("b=%d\n", b);  &NBSP;&NB Sp   printf ("a+b=%d\n", add (A, b));    printf ("a-b=%d\n", Sub (A, b));    return 0;}     "Makefile file"    # specify compiler and option # Specify the Raspberry Pi cross compiler cc = arm-linux-gnueabihf-gcccflags =-wall-std=gnu99# destination file t arget = test# c File SRCs = test.c# header File Lookup Path inc =-i.# library file and library find path dlibs =-ltestldflags =-l./lib# destination File Objs = $ (SRCS:.C=.O) # link is available Execution file $ (target): $ (OBJS) [tab]$ (CC)-O [email protected] $^ $ (ldflags) $ (dlibs) CLEAN:[TAB]RM-RF $ (target) $ (OBJS) # The compilation rule [email protected] represents the target file $< represents the first dependent file%.o:%.c[tab]$ (CC) $ (CFLAGS) $ (INC)-O [email protected]-C $<& nbsp   Description     "1" Cross toolchain for arm-linux-gnueabihf-gcc    "2" specifies the shared library and shared library paths after cross-compilation, and the linked shared library uses-ltest. The shared library is located in the Lib directory. Please note that the-ltest corresponds to libtest.so.     "3" makAfter e, you can get the executable file and view the information through file test. Test:elf 32-bit lsb  executable, ARM, EABI5Version 1 (SYSV), dynamically linked (uses shared libs), for Gnu/linux 2.6.26, buildid[sha1]=88e4142dceabd295369657b29757 141f98a03753, not stripped
As you can see from the console output, the executable runs on the platform arm, not AMD64.
3. Mobile Dynamic Link library"Move shared library to target board/usr/lib directory" through the FTP tool to transfer the shared library to the Raspberry Pi, and then through the CP instruction copied to/usr/lib in the sudo CP libtest.so/usr/lib Linux system default search Multipath is/lib and/    Usr/lib,libtest.so can be copied to any directory.    Modify the execution permissions for the libtest.so. sudo chmod 775 libtest.so
4. ImplementationFTP upload copies the executable test to the Raspberry Pi via the FTP tool, and then views the shared library link status through the LDD directive. "Inspection" LDD test/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6f7f000)
libtest.so =/usr/lib/libtest.so (0xb6f6b000)
libc.so.6 =/lib/arm-linux-gnueabihf/libc.so.6 (0xb6e3b000)
/lib/ld-linux-armhf.so.3 (0xb6f8d000)
As you can see from the console output, test successfully linked the libtest.so in/usr/lib
"Execute"./test
A=3
b=2
A+b=5
A-b=1 from the results of the implementation, the previous efforts are correct.
5. SummaryAnswer the question in the preface.    Whether the shared library at the "1" cross-compile time needs to be placed in the target board, if it needs to be placed in which directory. The shared library after cross-compilation needs to be copied to the target board, preferably in/usr/lib or/lib, but there are other methods, which are not explained in detail here.
Whether the shared library at the "2" cross-compile time needs to be placed in the host, if it needs to be placed in which directory. When cross-compiling, it is necessary to specify the shared library problem during the link process, and specify the shared library name through-l by specifying the directory by-L.    However, it is best not to place the cross-compiled shared library in the/lib or/usr/lib of the host, to avoid confusion. Combined with "1" and "2", the libtest.so is present in both the target board and the host.
"3" Cross compile how to specify the shared library through-l Specify the directory, the shared library name by-l
The "4" program runs the most intuitive way to find the shared library, copied to the/usr/lib directory, so that the Linux system automatically find.
Finally, found that the blog wrote more of their own feeling good tired ah, I hope these summaries are useful to everyone.

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.