编译一个最简单的C程序

编译一个最简单的C程序

创建一个目录cstudy, 创建文件 hello.c:

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

int main ()
{
    printf("Hello C\n");
    return 0;  
}

$ cd cstudy



1 二阶段编译:

1) 源文件(hello.c)编译(-c)成目标文件(hello.o)

$ gcc -c hello.c


2)目标文件(hello.o)链接输出(-o)成可执行文件(hello)

$ gcc -o hello hello.o


2 使用make构建

上述过程用Makefile来完成:

# Makefile for hello.c
# 2012-07-20
#   [email protected]
#
###########################################################
# change version[] in hello.c by below version:
PREFIX = .

all: hello

hello.o: hello.c
	cc -c $(PREFIX)/hello.c -o $@

hello: hello.o
	cc -o $@ \
	$(PREFIX)/hello.o

clean:
	-rm -f $(PREFIX)/hello.o
	-rm -f $(PREFIX)/hello.exe

check: all
	@echo "**** ALL TESTS PASSED ****"

.PHONY: all clean

3 执行

$ chmod a+x hello

$ ./hello





猜你喜欢

转载自blog.csdn.net/cheungmine/article/details/59078806