Dynamic library / static library and Makefile

1. static library: the program needs to run the library, the program compile time compiled together into, so when the program is running, you do not have a specific environment of the library to run properly, but relatively speaking, Since adding the library file into the compiled together, so the memory is generated executable file is relatively large.

  Disadvantages: 1 large footprint, resulting in a waste of space.

     2. If the program needs to modify a part, it is necessary to re-compile the entire file, links.

   Advantages: easy to transplant procedures, there is no need to consider whether the environment under the appropriate library file.

Math tools such as design modules 
mathmodel.h, mathmodel.c 
main.c uses mathematical functions to run 
gcc   - O mathmodel.c main main.c 
gcc   -o -c mathmodel.c mathmodel.o - the first step 
gcc   -o main main.c mathmodel.o - storage means may be in the mathmodel.o (static library, a dynamic library)

Static library production steps:

The mathmodel.c making static libraries
 1 . The source file compiled .o files (binaries) 
gcc -o mathmodel.o - c mathmodel.c 
 2 . Ar archive with archiving tool as a library file (lib library name .a) 
CRV libmathmodel.a mathmodel.o Ar 
3 . use a static library compiler 
gcc -o main main.c -L -. lmathmodel
  -L -. Add the path (path automatically searches the library) library is located behind 
  -lmathmodel --- specify which link library (- L library name) 
if it is below the current path, it is (L.)

 

2. Dynamic library: compile time does not need to be translated into the required library together, but when the program is running, the current environment is required to run these libraries, compiled executable file will not be so great.

  Advantages: 1 program easy to upgrade.

     2. The code and data sharing.

     3. The portion of the module can be loaded selectively.

     4. save space and resources

 DLL production:

1 . Preparation of the source file 
mathmodel.c 
2 to compile the source file into a binary file mathmodel.o. 
GCC -fPIC -o mathmodel.o - C mathmodel.c 
 . 3 . The packaged library .o file into 
GCC -fPIC -shared - O libmathmodel mathmodel.o .so
 4 . dynamic libraries compiler 
gcc -o main main.c -L -. lmathmodel
 5 to run the program. 
GEC @ Ubuntu: / mnt / hgfs / 05 / code / Shared $ ./main

Problems may occur:

./main: error while loading shared libraries: libmathmodel.so: cannot open shared object file: No such file or directory

Problem Solving - add the library environment variables (to tell the system which directory to find the library)

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD

 

3.makefile file under linux programming environment is simply an artifact that enables you to compile files every time no longer need to knock those huge long compile command, change a few lines after recompiling, knock again, again after the command, or keep pressing the "down" button to find commands, makefile file he can help as long as you enter it "make" command, it will automatically compile the command knock the giant long no longer needed. Different different compiler command corresponding makefile content.

 
 

TARGET=main
SRCS=$(wildcard *.c ./*/*.c)
OBJS=$(patsubst %.c,%.o,$(SRCS) )

 
 

CC=gcc

 
 

$(TARGET):$(OBJS)
$(CC) -o $@ $^

%.o:%.c
$(CC) -c $< -o $@ £»
.PHONY:clean echo

 
 

echo:
@echo "source files: $(SRCS)"
@echo "object files: $(OBJS)"

 
 

clean:
-rm $(OBJS) $(TARGET)

 

Common makefile command:

Compile: make

Delete compiled files: make clean

 

PS: Where if there is wrong, please correct me, we learn about each other.

Guess you like

Origin www.cnblogs.com/smallqizhang/p/12441164.html