zoukankan      html  css  js  c++  java
  • Linux Makefile Howto

    Linux Makefile Howto

    Linux Makefile Howto

    As far as I can remember, I have worked with makefile for quite a while, and I will look up for the meaning of all kinds of symbols in it, and after a thorough understanding of one makefile and the combination of a makefile I used most, I will post my own version of the makefile.

    I will borrow the example I found on the Internet.(A Simple Makefile Tutorial) and (makefile by examples)

    The following files are needed to make the compilation correct:

    hellomake.h
    extern void print_hello(void);
    
    print_fun.c
    #include <stdio.h>
    #include <stdlib.h>
    
    void print_hello(void)
    {
        printf("Hello make world!
    ");
    }
    
    main.c
    #include "hellomake.h"
    
    int main(void)
    {
        print_hello();
    }
    

    In a typical small or medium sized project, these directories are needed:

    name function
    src all the c or cpp files
    src/obj the compiler generated object files, which are normally correspondents with src files
    exe the final output of the project
    libs the external libs the project needs
    include all the .h files needed by other .c files

    The src/Makefile has the following contents:

    CC=gcc
    IDIR = ../include
    CFLAGS=-I$(IDIR)
    
    ODIR = obj
    LDIR = ../libs
    
    LIBS = -lm
    
    _DEPS = hellomake.h
    DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
    
    SOURCES = $(wildcard *.c)
    _OBJ = $(SOURCES:.c=.o)
    OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
    
    EXE = hellomake
    EXEDIR = ../exe
    
    $(ODIR)/%.o: %.c $(DEPS)
        $(CC) -c -o $@ $< $(CFLAGS)
    
    $(EXEDIR)/$(EXE): $(OBJ)
        gcc -o $@ $^ $(CFLAGS) $(LIBS)
    
    .PHONY:clean
    
    clean:
        rm -f $(ODIR)/*.o $(EXEDIR)/$(EXE)
    

    A fe-fined version of the makefile looks like:

    CC=gcc
    IDIR = ../include
    CFLAGS=-I$(IDIR)
    ODIR = obj
    LDIR = ../libs
    EXEDIR = ../exe
    LIBS = -lm
    DEPS = $(IDIR)/$(wildcard *.h)          
    SOURCES = $(wildcard *.c)
    _OBJ = $(SOURCES:.c=.o)
    OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
    
    #the name of the executable file should be changed when used in a specific project
    EXE = hellomake
    
    $(ODIR)/%.o: %.c $(DEPS)
        $(CC) -c -o $@ $< $(CFLAGS)
    
    $(EXEDIR)/$(EXE): $(OBJ)
        gcc -o $@ $^ $(CFLAGS) $(LIBS)
    
    .PHONY:clean
    
    clean:
        rm -f $(ODIR)/*.o $(EXEDIR)/$(EXE)
    
    #make project-dir-tree is used to create the necessary directory structure used in this makefile
    project-dir-tree:
        mkdir include libs src src/obj exe
        cp Makefile ./src
    

    In order to use this makefile, you should first create a project directory say, test_project, and you copy the makefile to the newly created directory, then invoke make project-dir-tree the following directories will appear, and you can organize your project clearly.

    exe include libs src

    But if you are working on a relatively small project which involves only a couple of source files, you can use this simplified version of makefile:

    CC=gcc
    CFLAGS = -I.
    DEFPS = $(wildcard *.h)
    SOURCES = $(wildcard *.c)
    OBJ = $(SOURCES:.o=.c)
    LIBS = -lm
    
    EXE = test
    
    %.o:%.c $(DEPS)
        gcc -c -o $@ $< $(CFLAGS)
    
    $(EXE):$(OBJ)
        gcc -o $@ $^ $(CFLAGS) $(LIBS)
    
    .PHONY:clean
    
    clean:
        rm -rf *.o $(EXE)
    

    With this file in hand, you can modify the desired output of the EXE to get the desired exe-file, and that is all you need do, enjoy it!

    By the way, if you don't want to compile all the source files in a directory, you can specify the sources files you want to compile by changing the SOURCES variable, like

    SOURCES = a.c b.c

    Author: wujing

    Created: 2014-08-26 二 10:08

    Emacs 24.3.1 (Org mode 8.2.6)

    Validate

  • 相关阅读:
    企业远程视频会议云服务EasyRTC-SFU版本支持 https 功能设计逻辑
    企业视频远程办公会议通话系统EasyRTC在Windows2012部署运行出现“计算机中丢失VCRUNTIME140.dll”如何解决?
    IP摄像头RTSP协议客户端EasyNVR视频平台提示加密机授权异常原因分析
    【方案搭建】多个工程如何做监控视频的集中管控?EasyNVR视频平台打造智慧工厂新方向
    IP摄像头RTSP协议客户端EasyNVR视频平台如何调用主码流和子码流?
    如何通过私有化部署自建一套基于RTSP协议的视频流媒体边缘计算系统EasyNVR视频平台来实现网络摄像头的实时直播回看?
    IP网络摄像机RTMP协议互联网直播/点播平台EasyDSS直播间录像录制机制说明
    视频上云网关/网络穿透设备EasyNTS下载表格时后端打印报错信息,但是又不影响下载的问题如何解决
    视频高速上云网关EasyNTS组网服务2.1.1新版本无法兼容低版数据库问题解析
    视频上云/网络穿透/网络映射服务EasyNTS前端组织添加页面出现Vue冲突怎么解决?
  • 原文地址:https://www.cnblogs.com/wujingcqu/p/3936598.html
Copyright © 2011-2022 走看看