C语言项目编译

Posted Mars.wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言项目编译相关的知识,希望对你有一定的参考价值。

在《linux-Unix系统编程》一书中,发现作者的源码编译写的非常高明。虽然尚不能完全理解,顺手牵过来分享给大家。

一、项目结构

首先,项目的目录结构如下,lib是共享的源文件,fileio是一个模块,fileio的源文件会依赖到lib中的文件,这也是一般项目的结构

.
├── fileio
│   ├── main
│   ├── main2
│   ├── main2.c
│   ├── main.c
│   └── makefile
├── lib
│   ├── Build_ename.sh
│   ├── function.c
│   ├── function.h
│   └── Makefile
├── Makefile
└── Makefile.inc

二、一级目录的Makefile

一级目录的Makefile是make命令的配置文件,大概是按顺序将所有二级目录进行构建,这里的lib一定要在fileio之前

DIRS = lib fileio

all:
        for i in $(DIRS); do \\
                (cd $$i && echo "making $$i" && $(MAKE) ) || exit 1; \\
        done
allgen:
        for dir in ${DIRS}; do (cd $${dir}; ${MAKE} allgen) ; done

clean:
        for i in $(DIRS); do \\
                (cd $$i && echo "cleaning $$i" && $(MAKE) clean) || exit 1; \\
        done

一级目录的Makefile.inc是二级目录的Makefile都要引用到的

TLPI_DIR = ..
TLPI_LIB = ${TLPI_DIR}/libtlpi.a
TLPI_INCL_DIR = ${TLPI_DIR}/lib

LINUX_LIBRT = -lrt
LINUX_LIBDL = -ldl
LINUX_LIBACL = -lacl
LINUX_LIBCRYPT = -lcrypt
LINUX_LIBCAP = -lcap

IMPL_CFLAGS = -std=c99 -D_XOPEN_SOURCE=600 \\
                -D_DEFAULT_SOURCE \\
                -g -I${TLPI_INCL_DIR} \\
                -pedantic \\
                -Wall \\
                -W \\
                -Wmissing-prototypes \\
                -Wno-sign-compare \\
                -Wno-unused-parameter

CFLAGS = ${IMPL_CFLAGS}

IMPL_THREAD_FLAGS = -pthread

IMPL_LDLIBS = ${TLPI_LIB} -lm

LDLIBS = ${IMPL_LDLIBS}

RM = rm -f

三、lib下的Makefile

里面引用了当前目录的一个shell脚本Build_ename.sh,我贴在后面

include ../Makefile.inc
allgen : ${TLPI_LIB}

${TLPI_LIB} : *.c ename.c.inc
        ${CC} -c -g ${CFLAGS} *.c
        ${RM} ${TLPI_LIB}
        ${AR} rs ${TLPI_LIB} *.o

ename.c.inc :
        sh Build_ename.sh > ename.c.inc
        echo 1>&2 "ename.c.inc built"

clean :
        ${RM} *.o ename.c.inc ${TLPI_LIB}

Build_ename.sh

#!/bin/sh
echo \'#include <errno.h>\' | cpp -dM |
sed -n -e \'/#define  *E/s/#define  *//p\' |sort -k2n |
awk \'
BEGIN {
        entries_per_line = 4
        line_len = 68;
        last = 0;
        varname ="    enames";
        print "static char *ename[] = {";
        line =  "    /*   0 */ \\"\\"";
}

{
    if ($2 ~ /^E[A-Z0-9]*$/) {      # These entries are sorted at top
        synonym[$1] = $2;
    } else {
        while (last + 1 < $2) {
            last++;
            line = line ", ";
            if (length(line ename) > line_len || last == 1) {
                print line;
                line = "    /* " last " */ ";
                line = sprintf("    /* %3d */ ", last);
            }
            line = line "\\"" "\\"" ;
        }
        last = $2;
        ename = $1;
        for (k in synonym)
            if (synonym[k] == $1) ename = ename "/" k;

            line = line ", ";
            if (length(line ename) > line_len || last == 1) {
                print line;
                line = "    /* " last " */ ";
                line = sprintf("    /* %3d */ ", last);;
            }
            line = line "\\"" ename "\\"" ;
    }
}
END {
    print  line;
    print "};"
    print "";
    print "#define MAX_ENAME " last;
}
\'

四、fileio中的makefile

include ../Makefile.inc

GEN_EXE = main main2

LINUX_EXE =

EXE = ${GEN_EXE} ${LINUX_EXE}

all : ${EXE}

allgen : ${GEN_EXE}


clean :
        ${RM} ${EXE} *.o

showall :
        @ echo ${EXE}

${EXE} : ${TLPI_LIB}

其中fileio/main.c,lib/function.h lib/function.c代码都在上一篇文章中,这里就不贴了 

以上是关于C语言项目编译的主要内容,如果未能解决你的问题,请参考以下文章

编译原理 实验一 java语言实现对C语言词法分析

C语言代码片段

用VS2008如何编译.c,C语言文件?

c语言程序解答(在线等)?

vs2017编译器编写c语言代码函数安全问题

C语言项目贪吃蛇游戏(下)