Linux Makefile的一些用法

一、介绍
本文用实例介绍Makefile的一些使用规则和用法

二、一些用法

1. 宏定义使用

A:宏定义
-D DEBUG
-D DEBUG=[value]

CFLAGS=

hello world
1 + 9 = 10

CFLAGS=-D DEBUG

hello world
1 + 9 = 10
Debug is defined
Debug is defined as true


CFLAGS=-D DEBUG=1
hello world
1 + 9 = 10
Debug is defined
Debug is defined as true


CFLAGS=-D DEBUG=0
hello world
1 + 9 = 10
Debug is defined

2. ifeq的使用

 ifeq ($(TARGET_ARCH), arm)
        LOCAL_SRC_FILES := ...
    else ifeq ($(TARGET_ARCH), x86)
        LOCAL_SRC_FILES := ...
    else ifeq ($(TARGET_ARCH), mips)
        LOCAL_SRC_FILES := ...
    else 
        LOCAL_SRC_FILES := ...
    endif
    

E:链接参数

-L.:指定库搜索路径
-l :指定链接库名称
-I:指定头文件搜索路径

F:编译优先级

三、 编译:

通过make命令进行编译

编译前目录:
.
.
|-- Makefile
|-- lib
|   |-- include
|   |   `-- math_method.h
|   `-- src
|       `-- math_method.c
`-- main.c


编译后目录:
.
|-- Makefile
|-- a.out
|-- lib
|   |-- include
|   |   `-- math_method.h
|   `-- src
|       `-- math_method.c
|-- libmath_method.a
|-- libmath_method.so
|-- main.c
`-- math_method.o

四、源码:

LIB_A_SUPPORT=y
LIB_SO_SUPPORT=y

CC=gcc

ifeq ($(LIB_A_SUPPORT), y)
LIBRARY=-L. -Wl,-dn -lmath_method -Wl,-dy -lgcc_s
endif


ifeq ($(LIB_SO_SUPPORT), y)
LIBRARY=-L. -Wl,-dy -lmath_method -Wl,-dy -lgcc_s
endif

INCLUDE=-I./lib/include

CFLAGS=

TARGET=a.out

ifeq ($(LIB_SO_SUPPORT), y)

DEPEND_TARGET += libmath_method.so

endif

ifeq ($(LIB_A_SUPPORT), y)

DEPEND_TARGET += libmath_method.a

endif

LIB_SRCS=$(wildcard ./lib/src/*.c)
LIB_NDIR_SRCS=$(notdir $(LIB_SRCS))
LIB_OBJ=$(patsubst %.c, %.o, $(LIB_NDIR_SRCS))

$(TARGET): $(DEPEND_TARGET)
        gcc -o $@ main.c $(LIBRARY) $(INCLUDE) $(CFLAGS)

$(LIB_OBJ): $(LIB_SRCS)
        echo $(LIB_SRCS)  "f"$(LIB_OBJ)
        $(CC) -fPIC -c $(LIB_SRCS)

ifeq ($(LIB_A_SUPPORT), y)
libmath_method.a: $(LIB_OBJ)
        ar cr $@ $^
        ranlib $@

endif

ifeq ($(LIB_SO_SUPPORT), y)
libmath_method.so: $(LIB_OBJ)
        $(CC) -shared -o $@ $^

endif

.PHONY:
clean:
        -rm -rf *.o *.a *.so
        -rm $(TARGET)


main.c:
#include <stdio.h>

#include "math_method.h"

int main(int arc, char *argv[])
{
    printf("hello world\n");

    printf("%d + %d = %d\n", 1, 9, math_add(1, 9));

#ifdef DEBUG

    printf("Debug is defined\n");

#endif

#if DEBUG

    printf("Debug is defined as true\n");

#endif

    return 0;
}

math_method.c:
#include <stdio.h>


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

int math_sub(int a, int b)
{
    return a - b;
}

math_method.h:
#ifndef __MATH_METHOD_H__
#define __MATH_METHOD_H__


int math_add(int a, int b);

int math_sub(int a, int b);

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪游东戴河

你就是这个世界的唯一

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值