linux 动态库

1.文件结构

hello (用于生产动态库的文件)

     + hello.c hello.h Makefile

lib(用于存放编译好的动态库)

test.c test Makefile

2.相关代码

hello.h

#ifndef _HELLO_H_
#define _HELLO_H_

#define debug_c 1

#ifdef debug_c

      #define MSGT "msg is good \n"
#else
        #define MSGT "msg is bye \n"
#endif

void debug(void);

#endif

hello.c

#include <stdio.h>
#include "hello.h"
void debug(void)
{
   printf(MSGT);
}


Makefile

###############################################################
#
# Purpose: Makefile for hello
# Author.: Angel
# Version: 1.0
# License: GPL
#
###############################################################
CC = gcc
OTHER_HEADERS =./hello.h 
CFLAGS += -O2 -Wall -shared -fPIC
LFLAGS += -ldl
filename = hello.c
all: libhello.so
clean:
rm -f *.a *.o core *~ *.so *.lo
libhello.so: $(OTHER_HEADERS) $(filename)
$(CC) $(CFLAGS) -o $@ $(filename)

test.c

#include "hello.h"
int main(void)
{
    debug();  
}

Makefile

################################################################

# Purpose: Makefile for test
# Author.: angel
# Version: 1.0
# License: GPL
#
###############################################################
CC = gcc
OTHER_HEADERS = 
CFLAGS += -O2 -Wall -l hello -L ./lib -I ./hello
LFLAGS += -ldl

all:libhello.so test
libhello.so:
    make -C ./hello/ all
    cp ./hello/libhello.so ./lib
test:$(OTHER_HEADERS) test.c
    $(CC) $(CFLAGS) -o $@ test.c
    export LD_LIBRARY_PATH=$(shell pwd)/lib
clean:
    make -C ./hello $@
    rm -f *.a *.o core *~ *.so *.lo test
    rm -f ./lib/*



转载于:https://my.oschina.net/u/1390984/blog/175669

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值