zoukankan      html  css  js  c++  java
  • Linux学习笔记——例说makefile 头文件查找路径

    0.前言
        从学习C语言開始就慢慢開始接触makefile,查阅了非常多的makefile的资料但总感觉没有真正掌握makefile,假设自己动手写一个makefile总认为非常吃力。所以特意借助博客总结makefile的相关知识,通过样例说明makefile的详细使用方法。
        例说makefile分为下面几个部分,很多其它内容请參考【例说makefile索引博文
        1.仅仅有单个C文件   
        2.含有多个C文件    
        3.须要包含头文件路径
        4.添加宏定义
        5.添加系统共享库
        6.添加自己定义共享库
        7.一个实际的样例

        【代码仓库】——makefile-example
        代码仓库位于bitbucket,可借助TortoiseHg(GUI工具)克隆代码或者在网页中直接下载zip包。

    1.三个C文件和三个头文件
        此处的样例略微复杂些但更接近实际情况。
        文件结果例如以下:根目录中包括test.c makefileh和目录test-add和目录test-sub。
    test.c makefile
        【test-add】test-add.c test-add.h
        【test-sub】test-sub.c test-sub.h

        【test.c】
    #include <stdio.h>
    #include <test-add.h>
    #include <test-sub.h>
    int main(void)
    {
        int a = 3;
        int b = 2;
           
        printf("a=%d
    ", a);
        printf("b=%d
    ", b);
     
        printf("a+b=%d
    ", add(a,b));
        printf("a-b=%d
    ", sub(a,b));
        return 0;
    }
        【test-add.c】
    #include <test-add.h>
    int add(int a, int b)
    {
        return a+b;
    }
        【test-add.h】
    #ifndef __TEST_ADD
    int add(int a, int b);
    #endif
        【test-sub.c】
    #include "test-sub.h"
    int sub(int a, int b)
    {
        return a-b;
    }
        【test-sub.h】
    #ifndef __TEST_SUB
    int sub(int a, int b);
    #endif

    2.复习gcc指令
        gcc指令可通过-I前缀指定头文件路径,特别说明./代表当前路径,../代表上一级文件夹。

    3.编写makefile
        请替换当中的[tab],并以代码仓库中的makefile文件为主。
    # 指令编译器和选项
    CC=gcc
    CFLAGS=-Wall -std=gnu99
    
    # 目标文件
    TARGET=test
    SRCS = test.c 
      ./test-add/test-add.c 
      ./test-sub/test-sub.c
    
    INC = -I./test-add -I./test-sub
    
    OBJS = $(SRCS:.c=.o)
    
    $(TARGET):$(OBJS)
    #	@echo TARGET:$@
    #	@echo OBJECTS:$^
    [tab]$(CC) -o $@ $^
    
    clean:
    [tab]rm -rf $(TARGET) $(OBJS)
    
    %.o:%.c
    [tab]$(CC) $(CFLAGS) $(INC) -o $@ -c $<
        【详细说明】
        【1】相比于单个文件和多个文件的makefile,通过变量INC制定了头文件路径。头文件路径之间通过空格隔开。
        【2】编译规则%.o:%.c中增加了头文件參数$(CC) $(CFLAGS) $(INC) -o $@ -c $<,那么在编译的过程中便会出现
        gcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test.o -c test.c。和单个文件和多个文件的makefile相比添加了头文件路径參数。
        【3】SRCS变量中,文件较多时可通过“”符号续行。

        【编译】
        make clean && make
        【控制台输出】
    rm -rf test test.o ./test-add/test-add.o ./test-sub/test-sub.o
    gcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test.o -c test.c
    gcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test-add/test-add.o -c test-add/test-add.c
    gcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test-sub/test-sub.o -c test-sub/test-sub.c
    gcc -o test test.o test-add/test-add.o test-sub/test-sub.o
        从控制台的输出能够看出,通过make clean清除上一次的可运行文件和目标文件,然后依次编译各个C文件,在编译的过程中制定了头文件路径,最后把3个目标文件链接为终于可运行文件。

  • 相关阅读:
    sh执行脚本报错Syntax error: "(" unexpected
    Ubuntu源码编译安装PostgreSQL
    Linux解决Graphical installers are not supported by the VM
    pytest功能入门
    vscode配置远程开发环境失败_workbench.downloadResource
    花生壳新版 内网穿透教程
    同局域网内进行本机调试
    Windows10激活
    win server 2012安装Vmware tools
    windows实现超级隐藏用户
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3983150.html
Copyright © 2011-2022 走看看