热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何编写动态库的makefile

我有一个.CPP和.h的文件,现在想把其编译成动态库的形式,各位大侠知道应该如何编写这个makefile文件,我是在嵌入式下开发。盼回复
我有一个.CPP和.h的文件,现在想把其编译成动态库的形式,各位大侠知道应该如何编写这个makefile文件,我是在嵌入式下开发。盼回复

7 个解决方案

#1


顶下

#2


CC      =       gcc
CXX     =       gcc
CFLAGS  =
CXXFLAGS=       
LINK    =       gcc     
LFLAGS  =       -shared 
LIBS    =       
####### Files
HEADERS =       s1.h    
SOURCES =       s1.c    
OBJECTS =       s1.o    
TARGET  =       s1.so   
####### Implicit rules

.SUFFIXES: .cpp .cxx .cc .C .c
.cpp.o:
        $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.cxx.o:
        $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.cc.o:
        $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.C.o:
        $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
.c.o:
        $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
####### Build rules
all:$(TARGET)
$(TARGET): $(OBJECTS)
        $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS)$(LIBS)
clean:
        -rm -rf $(OBJECTS) $(TARGET)



注意 -shared 参数就行了。 

#3


再顶

#4


include make.rules



LIBNAME:=libWiFi.so

LIBVERSION:=0.19

HDRNAME:=wi_ui_api.h

TARGETNAME:=wi_ui_api

CNAME:=$(patsubst %,%.c,$(TARGETNAME))

ONAME:=$(patsubst %,%.o,$(TARGETNAME)) 



all: libWiFi



libWiFi:



# Create shared libray's object file, JL00PublicLib.o.

$(GPP) -fPIC -O3 -g -c -Wall $(CNAME)



# Create shared library

# Use -lc to link it against C library, since JL00PublicLib depends on the C library.

$(GPP) -O3 -g -shared -Wl,-soname,$(LIBNAME) \

-o $(LIBNAME).$(LIBVERSION) $(ONAME) -lc



cp -f $(LIBNAME).$(LIBVERSION) $(JL00_LIB_PATH)/lib

ln -fs $(LIBNAME).$(LIBVERSION) $(JL00_LIB_PATH)/lib/$(LIBNAME)

cp -f $(HDRNAME) $(JL00_LIB_PATH)/include

$(RM) *.o

.PHONY : clean

clean:

$(RM) *.o *.so.*



make.rule 如下

GCCBINDIR=/opt/freescale/usr/local/gcc-4.1.1-glibc-2.4-nptl-6/arm-none-linux-gnueabi/bin
EXE_PREFIX=arm-none-linux-gnueabi-

ROOTFS_PREFIX=/opt/gsl/epop/ltib/rootfs
INCLUDEDIR=$(ROOTFS_PREFIX)/usr/include
LIBDIR=$(ROOTFS_PREFIX)/usr/lib
JL00_LIB_PATH=/opt/gsl/epop/ltib/rootfs/usr

GCC=$(GCCBINDIR)/$(EXE_PREFIX)gcc
GPP=$(GCCBINDIR)/$(EXE_PREFIX)g++


DIRECTFB_LIBS=-L$(LIBDIR) -lfusion -ldirect -ldirectfb -lpthread -ldl -lz
DIRECTFB_CFLAGS=-D_REENTRANT -I$(INCLUDEDIR)/directfb

#5


用autotools和libtool吧!

$ lf -R
Makefile.am     configure.ac    libmym/

./libmym:
Makefile.am     mym.cpp         mym.h
$ cat Makefile.am
AUTOMAKE_OPTIONS = foreign
SUBDIRS = libmym
$ cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(libmym, 1.0, mymtom@mymtom.cn)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([libmym/mym.cpp])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CXX
AC_PROG_LIBTOOL

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile
                 libmym/Makefile])
AC_OUTPUT
$ cat libmym/Makefile.am
AUTOMAKE_OPTIONS = foreign
lib_LTLIBRARIES = libmym.la
libmym_la_SOURCES = mym.cpp
$ cat libmym/mym.cpp
#include 
using namespace std;

#include "mym.h"

void
mym_hello(void)
{
        cout << "mym_hello(): Hello, World!" << endl;
}

$ cat libmym/mym.h
#ifndef MYM_H
#define MYM_H

#ifdef __cplusplus
extern "C" {
#endif

void     mym_hello(void);

#ifdef __cplusplus
}
#endif

#endif
$ aclocal
$ libtoolize -c -f
$ autoheader
$ autoconf
$ automake --add-missing
configure.ac:5: installing `./missing'
configure.ac:5: installing `./install-sh'
libmym/Makefile.am: installing `./depcomp'
$ ./configure --prefix=/usr/home/mymtom/local
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
......
......
checking if g++ PIC flag -fPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking whether the g++ linker (/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... freebsd8.0 ld.so
checking how to hardcode library paths into programs... immediate
appending configuration tag "F77" to libtool
configure: creating ./config.status
config.status: creating Makefile
config.status: creating libmym/Makefile
config.status: creating config.h
config.status: executing depfiles commands
$ gmake
gmake  all-recursive
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
Making all in libmym
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
/usr/local/bin/bash ../libtool --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT mym.lo -MD -MP -MF .deps/mym.Tpo -c -o mym.lo mym.cpp
mkdir .libs
 g++ -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT mym.lo -MD -MP -MF .deps/mym.Tpo -c mym.cpp  -fPIC -DPIC -o .libs/mym.o
 g++ -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT mym.lo -MD -MP -MF .deps/mym.Tpo -c mym.cpp -o mym.o >/dev/null 2>&1
mv -f .deps/mym.Tpo .deps/mym.Plo
/usr/local/bin/bash ../libtool --tag=CXX   --mode=link g++  -g -O2   -o libmym.la -rpath /usr/home/mymtom/local/lib mym.lo  
g++ -shared -nostdlib /usr/lib/crti.o /usr/lib/crtbeginS.o  .libs/mym.o  -L/usr/lib -lstdc++ -lm -lc -lgcc_s /usr/lib/crtendS.o /usr/lib/crtn.o  -Wl,-soname -Wl,libmym.so.0 -o .libs/libmym.so.0
(cd .libs && rm -f libmym.so && ln -s libmym.so.0 libmym.so)
(cd .libs && rm -f libmym.so && ln -s libmym.so.0 libmym.so)
ar cru .libs/libmym.a  mym.o
ranlib .libs/libmym.a
creating libmym.la
(cd .libs && rm -f libmym.la && ln -s ../libmym.la libmym.la)
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Nothing to be done for `all-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
$ gmake install
Making install in libmym
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
test -z "/usr/home/mymtom/local/lib" || .././install-sh -c -d "/usr/home/mymtom/local/lib"
 /usr/local/bin/bash ../libtool --mode=install /usr/bin/install -c  'libmym.la' '/usr/home/mymtom/local/lib/libmym.la'
/usr/bin/install -c .libs/libmym.so.0 /usr/home/mymtom/local/lib/libmym.so.0
(cd /usr/home/mymtom/local/lib && { ln -s -f libmym.so.0 libmym.so || { rm -f libmym.so && ln -s libmym.so.0 libmym.so; }; })
(cd /usr/home/mymtom/local/lib && { ln -s -f libmym.so.0 libmym.so || { rm -f libmym.so && ln -s libmym.so.0 libmym.so; }; })
/usr/bin/install -c .libs/libmym.lai /usr/home/mymtom/local/lib/libmym.la
/usr/bin/install -c .libs/libmym.a /usr/home/mymtom/local/lib/libmym.a
chmod 644 /usr/home/mymtom/local/lib/libmym.a
ranlib /usr/home/mymtom/local/lib/libmym.a
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/home/mymtom/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
gmake[2]: Nothing to be done for `install-data-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Nothing to be done for `install-exec-am'.
gmake[2]: Nothing to be done for `install-data-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
$ gmake dist-bzip2
{ test ! -d libmym-1.0 || { find libmym-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr libmym-1.0; }; }
test -d libmym-1.0 || mkdir libmym-1.0
list='libmym'; for subdir in $list; do \
          if test "$subdir" = .; then :; else \
            test -d "libmym-1.0/$subdir" \
            || ./install-sh -c -d "libmym-1.0/$subdir" \
            || exit 1; \
            distdir=`CDPATH="${ZSH_VERSION+.}:" && cd libmym-1.0 && pwd`; \
            top_distdir=`CDPATH="${ZSH_VERSION+.}:" && cd libmym-1.0 && pwd`; \
            (cd $subdir && \
              gmake  \
                top_distdir="$top_distdir" \
                distdir="$distdir/$subdir" \
                am__remove_distdir=: \
                am__skip_length_check=: \
                distdir) \
              || exit 1; \
          fi; \
        done
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
find libmym-1.0 -type d ! -perm -777 -exec chmod a+rwx {} \; -o \
          ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
          ! -type d ! -perm -400 -exec chmod a+r {} \; -o \
          ! -type d ! -perm -444 -exec /usr/local/bin/bash /usr/home/mymtom/src/play/automake/hello-1.0/install-sh -c -m a+r {} {} \; \
        || chmod -R a+r libmym-1.0
tardir=libmym-1.0 && /usr/local/bin/bash /usr/home/mymtom/src/play/automake/hello-1.0/missing --run tar chof - "$tardir" | bzip2 -9 -c >libmym-1.0.tar.bz2
{ test ! -d libmym-1.0 || { find libmym-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr libmym-1.0; }; }

#6


目标指定为XXX.so
编译的时候加-shared -fpic

#7


g++ -shared -fPIC -o xxx.so xxx.cpp

推荐阅读
  • spotify engineering culture part 1
    原文,因为原视频说的太快太长,又没有字幕,于是借助youtube,把原文听&打出来了。中文版日后有时间再翻译。oneofthebigsucceessfactorshereatSpo ... [详细]
  • JS动态生成表格案例 ... [详细]
  • 本文翻译自:WhatisaMavenartifact?什么是神器?为什么Maven需要它?#1楼参考:https:sta ... [详细]
  • (一)javax.mail.Session:Session类代表JavaMail中的一次邮件会话.每个基于JavaMail的应用程序至少有一次会话,也可以产生多次会话.发送邮件之前 ... [详细]
  • Linux提权之suid篇
    Linux提权之suid篇不知攻,焉知防一个在安服路上摸索的大三生,记录平时学习笔记suid前言:1.只有可以执行的二进制程序文件才 ... [详细]
  • 是不是zlib是这些库的压缩算法的实现库,而这么多库它们只是在打包的时候使用了zlib进行压缩而已.而具体的打包格式就有ZIP,BZIP2,GZ之分?但是在我们在用gz压缩时候通常之前 ... [详细]
  • GPS 校验和 代码_Linux recovery 移除签名校验
    原创作者:王锐,多年Linux系统、龙芯平台移植与优化研发经验,LinuxContributor、Mozillian。背景某个设备配套的刷 ... [详细]
  • 标签PostgreSQL,Linux,perf,性能诊断,stap,systemtap,strace,dtrace,dwarf,profiler,perf_events,probe ... [详细]
  • github:https:github.comfroghuiyolandaIO模型和多线程模型实现多线程设计的几个考虑在我们的设计中,mainre ... [详细]
  • 一、如果使用默认的1521端口,让实例自动注册到该监听上,那么local_listener无需设置,listener.ora文件按照正常方 ... [详细]
  • Matlab中利用mex编译Opencv实现画板绘图功能
    图形绘制是标记和可视化数据的重要方法.通过在Matlab中集成画板绘图功能,可为科学计算提供便利.1设置Matlab支持Opencv编译操作系统:麒麟14.04(基于Ubu ... [详细]
  • JS加密解密
    leta=汪政..222RRRp767868^*%^*%344h哈哈;letb=udp.d(ud(a));//需要加密的内容letc=udp. ... [详细]
  • Go 中的 init 函数 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • linux进阶50——无锁CAS
    1.概念比较并交换(compareandswap,CAS),是原⼦操作的⼀种,可⽤于在多线程编程中实现不被打断的数据交换操作࿰ ... [详细]
author-avatar
叮咚叮咚_叮
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有