设为首页 加入收藏

TOP

Linux下面C语言多文件编译
2014-11-24 02:29:32 来源: 作者: 【 】 浏览:1
Tags:Linux 下面 语言 文件 编译

刚开始学习Linux下面的C语言开发,以前只是在Windows下面写过简单C语言的代码,所以对Linux下面的C语言编写不慎了解,这几天看了看相关文章,总算有个一知半解了。


首先打开命终端(Alt+Ctrl+T),用Vim编写三个文件main.c, stack.c, stack.h


main.c


#include
#include"stack.h"


void main()
{
int elem;
Stack stack;
push(&stack, 1);
pop(&stack, &elem);
}


stack.h


typedef struct Stack {
int *base;
int top;
}Stack;


extern void push(Stack *stack, int elem);
extern void pop(Stack *stack, int *elem);


stack.c


#include
#include"stack.h"
void push(Stack *stack, int elem)
{
printf("this is push function\n");
}


void pop(Stack *stack, int *elem)
{
printf("this is pop function\n");
}


这三个文件都在同一文件夹下面,这时可以使用命令:gcc main.c stack.c stack.h -o mian直接在命令行中编译,但是为了学习使用Makefile 就写了一个文件用于编译


Makefile文件:


main: main.o stack.o
gcc main.o stack.o -o main


main.o: main.c stack.h
gcc -c main.c


stack.o: stack.c stack.h
gcc -c stack.c


这样直接在终端中运行make就可以进行编译了...
如果编译的文件不在同一文件夹下面,则在Makefile文件中写明源文件的路径即可...


推荐阅读


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux编程练习(二)—— Linux下.. 下一篇Java 初始化及类的加载

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: