Chinaunix首页 | 论坛 | 博客
  • 博客访问: 52131
  • 博文数量: 17
  • 博客积分: 651
  • 博客等级: 上士
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-23 14:23
文章分类

全部博文(17)

文章存档

2011年(10)

2010年(7)

我的朋友

分类: C/C++

2011-01-04 10:00:29

假设我们分别有两个目录:include和src

其中include中存放的是头文件:hello.h

src中存放的是源文件:main.c和print_hello.c


那么有两种方法来包含头文件:

一、直接在源文件中指定头文件的路径

在main.c中:

#include "../include/hello.h"

在print_hello.c中:

#include "../include/hello.h"


那么在src目录中可以直接编译即可:

gcc -o main main.c print_hello.c

就可以生成可执行文件

或者可以写个Makefile:

objects = main.o print_hello.o

main : ${objects}
gcc -o main ${objects}

main.o : main.c ../include/hello.h
gcc -c main.c

print_hello.o : print_hello.c ../include/hello.h
gcc -c print_hello.c


clean:
@rm *.o main

.PHONY: clean


二、在源文件中不指定头文件路径,而是在编译时指定

在main.c中:

#include "hello.h"

在print_hello.c中:

#include "hello.h"

那么在src目录中需要为gcc指定头文件路径:

gcc -o main main.c print_hello.c -I../include

这样才可以生成可执行文件

或者可以写个Makefile:

objects = main.o print_hello.o

main : ${objects}
gcc -o main ${objects}

main.o : main.c ../include/hello.h
gcc -c main.c

print_hello.o : print_hello.c ../include/hello.h
gcc -c print_hello.c


clean:
@rm *.o main

.PHONY: clean

或者

Makefile也可以这么写:

objects = main.o print_hello.o

main : ${objects}
gcc -o main ${objects}

main.o : main.c
gcc -c main.c -I../include

print_hello.o : print_hello.c
gcc -c print_hello.c -I../include


clean:
@rm *.o main

.PHONY: clean

阅读(1414) | 评论(1) | 转发(0) |
0

上一篇:GCC 编译选项

下一篇:asmlinkage

给主人留下些什么吧!~~

chinaunix网友2011-01-05 11:05:39

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com