Makefile Static Library Dynamic Library

Source: Internet
Author: User
Dynamic Library calls another static library:
cc=$ (cross_compile) gcc
ld=$ (cross_compile) LD

cflags:=-fpic
ldflags:=-shared-fpic
( Wildcard *.cpp)
objs:=$ (Patsubst%.cpp,%.o,$ (SOURCE))
target_lib:=liblive.so
INC =-I.. /librtmp


all:$ (OBJS)
	echo $ (OBJS)
	$ (LD) $ (ldflags)-O $ (target_lib) $ (OBJS). /librtmp/librtmp.a

%.o:%.cpp
	@echo compiling $< ...
	$ (CC)-C $ (cflags) $ (INC) $<-o $*.o

. Phony:clean clean

:
	rm *.so *.O-RF

Static library calls another static library:Compiling LIBLIVE.A,LIBLIVE.A relies on librtmp.a.
CC = gcc
cflags  : =-wall-o3-std=c99-fpic

Makefilepath = $ (Shell pwd)
# rtmp header file and Lib path 
Rtmp_inc_roo T = $ (Makefilepath)/.. /librtmp
rtmp_lib_root = $ (Makefilepath)/.. /librtmp

# @OBJS = GENDLL.O #.o file has the same name as the. cpp file
source:=$ (wildcard *.cpp)
objs:=$ (Patsubst%.cpp,%.o,$ (SOURCE )
$ (Warning $ (OBJS))
LIB = Liblive.a

opencv_inc=-i$ (rtmp_inc_root)

include_path = $ (opencv_inc)
$ (Warning $ (include_path))

Lib_path =-l$ (rtmp_lib_root)

# dependent LIB name
Rtmp_lib = $ (rtmp_lib_root)/librtmp.a all

: $ (LIB)

# 2. Generate. o File 
%.o:%.cpp
	$ (CC) $ (cflags)-C $<-o $@ $ (include_path) $ (lib_path) $ (rtmp_lib) 

# 3. Generate a static library file 
  
   $ (LIB): $ (OBJS)
	rm-f $@
	ar cr $@ $ (OBJS) $ (rtmp_lib) #这里虽然把他打包进去了, but I'm going to call this liblive.a later with a compile. So. Found inside the LIBRTMP library inside the function is undefined, to solve this problem, need to be in. So inside the librtmp.a included, but this is only the next worst.
	rm-f $ (OBJS)

Tags:
	ctags-r *

# 4. Delete Intermediate process generated files clean 
:
	rm-f $ (OBJS) $ (TARGET) $ (LIB)

  


compile a librts.so to invoke Liblive.aAccording to the second step of compiling liblive.a can see the AR command is packaged LIBRTMP.A, above also said, in fact, did not work, so the above AR packing time to remove the LIBRTMP.A is the same, and then compile the time to take the LIBRTMP.A, In this case, the hierarchy does not come out, which heroes know can leave a message under the guidance.
cc=$ (cross_compile) gcc
ld=$ (cross_compile) LD

makefilepath = $ (shell pwd)
$ (Warning $ (makefilepath))

Cflags:=-fpic
Ldflags:=-shared-fpic
source:=$ (wildcard *.c)
objs:=$ (Patsubst%.c,%.o,$ (SOURCE))
target_lib:= librts.so
inc:=-i$ (Makefilepath)/.. /thirdlibs/include-i$ (Makefilepath)/.. /thirdpart/live/liveplay

libdir:=$ (Makefilepath)/.. /thirdlibs/lib
livedir:=$ (Makefilepath)/.. /thirdpart/live
libs:= $ (libdir)/libstrophe.so $ (livedir)/liveplay/liblive.a $ (livedir)/LIBRTMP/LIBRTMP.A-LM -lpthread 

all:$ (OBJS)
	echo $ (OBJS)
	$ (LD) $ (ldflags)-O $ (target_lib) $ (OBJS) $ (LIBS)

%.o:%.c
	@echo compiling $<
	... $ (CC)-C $ (cflags) $ (INC) $<-o $*.o

. Phony:clean clean

:
	rm *.so *.O-RF
Finally, you can write an executable program to call,-wl,-rpath= this is to specify a run-time search path, the role of the same as environment variables
CC=GCC
cflags=-wall-std=c99

target=main
srcs = main.c,

INC =-i./thirdlibs/include  -i./ Thirdpart/live/liveplay
ldflags + +-l./thirdlibs/lib/  -l./src-wl,-rpath=./thirdlibs/lib-wl,-rpath=./src
dlibs =-lrts-lstrophe
OBJS = $ (SRCS:.C=.O)

cflags + = $ (INC) #header files

$ (TARGET): $ (OBJS)
	$ (CC)-g-o $@ $ ($^) $ (ldflags) C12/>clean:
	RM-RF $ (TARGET) $ (OBJS)

%.o:%.c
	$ (CC) $ (cflags)-g-o $@-C $<


some things about configure. compiling LibexpatCflags= "-fpic" This is very important, you do not add this compiled dynamic library no problem, but the static library will be problematic, for example, if you write a so to call this. A, you will be prompted to relocation r_x86_64_32 against '. Rodata ' can is used when making a is shared object; This error, so be sure to remember to add.
Cflags= "-fpic"./configure--prefix=/home/vmuser/ljt/rts_linux/thirdlibs
OpenSSL compilation/config Shared-fpic--prefix=/home/vmuser/ljt/rts_linux/thirdlibs If it is arm, remember to add no-asm, see a blog in detail. Here also remember to add-fpic.
Libstrophe Compilation1. The dependent library is the static library should be so written,. A's all write absolute path, and then Libs is a dependent system library, This compiles the LIBSTROPHE.A directly to include the LIBSSL.A and so on. A library, when other programs call the LIBSTROPHE.A does not need to include LIBSSL.A, this is more advanced than the above liblive.a, how to do I still do not understand. Expat_cflags=-i/home/vmuser/ljt/rts_linux/thirdlibs/include expat_libs= '/home/vmuser/ljt/rts_linux/thirdlibs/ Lib/libexpat.a ' Openssl_cflags=-i/home/vmuser/ljt/rts_linux/thirdlibs/include openssl_LIBS= '/home/vmuser/ljt/rts _linux/thirdlibs/lib/libssl.a/home/vmuser/ljt/rts_linux/thirdlibs/lib/libcrypto.a ' LIBS=-ldl CFLAGS= "-fPIC"./ Configure--prefix=/home/vmuser/ljt/rts_linux/thirdlibs/
2. If the dependent library is the dynamic library should be so written, libs options can not, very strange bar, hehe. Openssl_cflags=-i/home/vmuser/ljt/rts_linux/thirdlibs/include openssl_libs= '-l/home/vmuser/ljt/rts_linux/ Thirdlibs/lib-lssl-lcrypto ' Expat_cflags=-i/home/vmuser/ljt/rts_linux/thirdlibs/include expat_LIBS= '-L/home/ Vmuser/ljt/rts_linux/thirdlibs/lib-lexpat './configure--prefix=/home/vmuser/ljt/rts_linux/thirdlibs/


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.