现在的位置: 首页 > 综合 > 正文

Linux下面C语言多文件编译

2013年10月25日 ⁄ 综合 ⁄ 共 801字 ⁄ 字号 评论关闭
刚开始学习Linux下面的C语言开发,以前只是在Windows下面写过简单C语言的代码,所以对Linux下面的C语言编写不慎了解,这几天看了看相关文章,总算有个一知半解了。

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

main.c
#include<stdio.h>
#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<stdio.h>
#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文件中写明源文件的路径即可...

抱歉!评论已关闭.