阐述Linux Makefile文件概念

系统 Linux
Linux系统越来越受到电脑用户的欢迎,于是很多人开始学习Linux时,学习Linux,你可能会遇到Linux makefile问题,这里将介绍Linux makefile问题的解决方法,在这里拿出来和大家分享一下。

Linux系统越来越受到电脑用户的欢迎,于是很多人开始学习Linux时,学习Linux,你可能会遇到Linux makefile问题,这里将介绍Linux makefile问题的解决方法,在这里拿出来和大家分享一下。

将各个模块的关系写进makefile,并且写明了编译命令,这样,当有模块的源代码进行修改后,就可以通过使用make命令运行makefile文件就可以进行涉及模块修改的所有模块的重新编译,其他模块就不用管了。

makefile文件的写法:

目标, 组件
规则

例如 有下面5个文件:

/* main.c */
#include "mytool1.h"
#include "mytool2.h"
int main(int argc,char **argv)
{
mytool1_print("hello");
mytool2_print("hello");
}
/* mytool1.h */
#ifndef _MYTOOL_1_H
#define _MYTOOL_1_H
void mytool1_print(char *print_str);
#endif
/* mytool1.c */
#include "mytool1.h"
void mytool1_print(char *print_str)
{
printf("This is mytool1 print %s\n",print_str);
}
/* mytool2.h */
#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H
void mytool2_print(char *print_str);
#endif
/* mytool2.c */
#include "mytool2.h"
void mytool2_print(char *print_str)
{
printf("This is mytool2 print %s\n",print_str);
}

可以这样进行编译以便运行main这个可执行文件

gcc -c main.c (生成main.o)
gcc -c mytool1.c (生成mytool1.0)
gcc -c mytool2.c (生成mytool2.0)
gcc -o main main.o mytool1.o mytool2.o (生成main)

也可以这样写makefile文件

main main.o mytool.o mytool2.o
gcc -0 $@ $^
main.0 main.c mytool1.h mytool2.h
gcc -c $<
mytool1.0 mytool1.c mytool1.h
gcc -c $<(或者是mytool.c)
mytool2.0 mytool2.c mytool2.h
gcc -c $<(或者是mytool2.c)

通过make命令可以运行该文件,也就是进行编译了。

Linux上有很多库,c语言编写的各种库的总称为libc,glibc为libc的一个子集,由gnu提供,内核提供的系统函数和系统调用是不包括在libc中。

Linux系统默认会安装glibc

glibc中

常用库gcc会自动去查找,不予理会。

在/lib, /usr/lib, /usr/local/lib 在这三个路径下面有一些标准库,只需-l+库名 可以不必要指定路径。其他库必须在用gcc时用-L+具体的路径。通过本文你就能全面了解Linux makefile。

【编辑推荐】

  1. 掌握Linux系统性能指标 学好Linux
  2. 详细介绍Linux grep指令
  3. 教会你Linux获取文件大小方法
  4. 操作笔记:Linux查看系统时间
  5. 详解Linux系统修改环境变量PATH路径的方法
责任编辑:小霞 来源: 网易博客
相关推荐

2010-06-29 15:58:26

Linux SNMP协

2009-12-14 13:14:57

2009-12-22 15:12:33

Linux扩展文件系统

2009-10-26 11:34:42

linux makef

2010-09-17 09:34:00

SIP路由机制

2009-12-16 15:59:13

Linux设备文件管理

2009-12-09 14:15:39

2010-03-17 17:54:51

Python语法

2009-12-24 10:12:02

Linux查看文件编码

2010-03-10 14:48:24

2009-12-02 14:09:52

2009-11-30 10:54:59

solaris命令

2009-12-01 09:51:50

备份Linux内核

2009-10-26 13:45:39

linux Makef

2009-12-21 14:33:11

2009-12-10 10:25:12

Linux触摸屏驱动

2009-12-01 18:41:08

SUSE Linux

2009-12-23 10:50:57

Linux chmod

2009-12-25 17:15:03

Linux内存

2009-12-22 10:58:50

ADO.NET类
点赞
收藏

51CTO技术栈公众号