Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux static library generated Guide     - Shell script to delete empty folders recursively (Linux)

- Use Makeself Create installation file (Linux)

- The formatted Linux hard drive and mount (Linux)

- CentOS 6.x Basic System Optimization after installation (Linux)

- Beautiful start Ubuntu installation tool Duck Launcher 0.64.5 (Linux)

- Why everybody ought to know LLVM (Linux)

- Spark parquet merge metadata problem (Server)

- How to use Xmanager Remote Desktop and VNC Log (Linux)

- MariaDB 10 Multi-source replication (Database)

- Oracle Migration partition table (Database)

- Ubuntu 14.04 / 13.10 users how to install Ubuntu Touch core applications (Linux)

- Linux common network tools: ping host sweep (Linux)

- Calculate CPU utilization (Linux)

- Installing Linux and Windows 10 dual system (Linux)

- grub boot error resolution examples (Linux)

- C # assembly calls across constants, variables and functions (Programming)

- DataGuard Standby backup error RMAN-06820 ORA-17629 to solve (Database)

- Introduces Linux kernel compilation system and compiler installation (Linux)

- Installation Enpass secure password manager on Ubuntu (Linux)

- After reloading the cinder-volume OpenStack not start properly (Server)

 
         
  Linux static library generated Guide
     
  Add Date : 2017-05-17      
         
         
         
  Static library on Linux, in fact, is the target archive files.
To create a static library on Linux as follows:

Write source files, object files generated by gcc -c xxx.c.
Ar archive with a target file, generate static library.
With static libraries, write a function using a static library header files.
When using a static library that contains the corresponding header file in the source code, remember to link your own libraries when linking.
The following specific examples to explain through.

Write source file, object file is generated.

The first source file my_print.c

#include < stdio.h>

void cout (const char * message)
{
    fprintf (stdout, "% s \ n", message);
}
Source File 2: my_math.c

int add (int a, int b)
{
    return a + b;
}

int subtract (int a, int b)
{
    return a - b;
}
Using gcc, object file is generated for both the source file:

gcc -c my_print.c my_math.c
We get my_print.o and my_math.o.

Archive object file to obtain a static library.

We use ar target file archiving:

ar crv libmylib.a my_print.o my_math.o
We get libmylib.a, this is what we need static library.

The above command is ar crv command options:

c If you need to generate a new library file, not warnings
r replace the existing library files or insert a new file
v Output details
By ar t libmylib.a you can view the target file libmylib.a included.

More help can be viewed by ar --help.

Note: File names we want to generate a library must be shaped like libxxx.a, so when we link this library, you can use -lxxx.
Conversely, when we tell the compiler -lxxx, the compiler will search in the specified directory libxxx.a or libxxx.so.

Generate the corresponding header file

Libmylib.a header file defines the interface, that is, tell the user how to use libmylib.a.

Generate my_lib.h, which reads as follows:

#ifndef __MY_LIB_H__
#define __MY_LIB_H__

int add (int a, int b);
int subtract (int a, int b);

void cout (const char *);
#endif
Test our static library

In the same directory, the establishment test.c:

#include "my_lib.h"

int main (int argc, char * argv [])
{
    int c = add (15, -21);
    cout ( "I am a func from mylib ...");
    return 0;
}
This source file references libmylib.a the cout and add functions.

Compile test.c:

gcc test.c -L. -lmylib
Will generate a.out, you can run the program by ./a.out. Static library that we can work properly.

The above command -L. To tell gcc search link library that contains the current path, -lmylib tell gcc when the executable program to link libmylib.a.

By makefile Automation

The above steps are cumbersome, or write a simple makefile it reads as follows:

.PHONY: Build test

build: libmylib.a

libmylib.a: my_math.o my_print.o
ar crv $ @ my_math.o my_print.o

my_math.o: my_math.c
gcc -c my_math.c

my_print.o: my_print.c
gcc -c my_print.c

test: a.out

a.out: test.c
gcc test.c -L. -lmylib
After written makefile, will run make build libmylib.a build, run make test will generate links libmylib.a program.

If you use a method of generating mingw, on Windows and Linux static library is the same.
     
         
         
         
  More:      
 
- How to Install Xombrero 1.6.4 (minimalist Web browser) on Ubuntu and Archlinux (Linux)
- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)
- MySQL Server Time Synchronization Problem (Database)
- Oracle Linux 6.4 installed Oracle 11gR2 + RAC + ASM (Database)
- Linux Shell introduces (Linux)
- Upgrading from Fedora 20 to 21 (Linux)
- LVM management parameters commonly used commands explained in detail (Linux)
- To install and deploy Java applications under CentOS 6.5 (Linux)
- Linux Hard Disk Partition and file system management (Linux)
- Analysis of MySQL High Availability (Database)
- Ubuntu 14.04 install PostgreSQL 9.2 (Database)
- Multi-core CPU, multi-threading and parallel computation (Linux)
- Sleuth Kit: used to analyze a disk image and restore files open source forensics tools (Linux)
- MySQL Basic tutorial: About varchar (N) (Database)
- Linux how to view your JDK installation (Linux)
- System Security: Build Linux with LIDS steel castle (Linux)
- XP virtual machine under VirtualBox solve occupy 100% CPU problem (Linux)
- Easy to install CentOS 6.6 desktop environment (Linux)
- Install Firefox 32 official version of the Linux system (Linux)
- Linux file permissions and access modes (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.