代码改变世界

Linux下编译程序

2012-10-29 23:38  fingertouch  阅读(235)  评论(0编辑  收藏  举报

一.编译C程序

1.编译单个C程序:

现编译如下程序(hello.c):

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
    printf("Hello Linux!");
    return 0;
} 

 使用:

#gcc -o hello hello.c

其中:-o选项是用来重命名程序名词,否则默认的程序名词为a.out

2.编译多个源文件

现同时编译两个文件(hello.h hello.c和main.c):

//hello.h
#ifndef __HELLO_H_
#define __HELLO_H_

void print(const char *word);

#endif

 

//hello.c
#include<stdio.h>
#include "hello.h"

void print(const char *word)
{
printf("%s",word);
}

 

//main.c
#include<stdio.h>
#include<stdlib.h>
#include"hello.h"
int main(int argc,char *argv[])
{
print("hello Linux\n");
return 0;
} 

 编译此函数使用:

#gcc -o hello hello.c main.c

 或者分步生成:

#gcc -c hello.c
#gcc -c main.c
#gcc -o hello hello.o main.o

其中,选项-c是只编译不链接