包含3个文件夹,和一个文件Makefile
目录组织结构如下:
Makefile
inc/hello.h
main/main.c
src/hello.c
Makefile文件在外面,这样生成的.o和可执行文件都在外面,clean之后会很干净,结构清晰
文件内容如下:
Makefile(之所以用大写,因为make可以识别Makefile和makefile,用大写可以鲜明一些)::
# String declaration objects = main.o hello.o # Command app : $(objects) cc -o app $(objects) main.o : main/main.c hello.h cc -c main/main.c hello.o : src/hello.c stdio.h cc -c src/hello.c # Search paths vpath %.h /usr/include inc # Clean the intermediate files .PHONY : clean clean : rm app $(objects)
hello.h:
void hello(char name[]);
main.c:
#include <stdio.h> #include "../inc/hello.h" // The second hello.h should in "" int main() { hello("GCC"); printf("Haha Linux Ubuntu!/n"); return 0; }
其中,第二个包含文件,hello.h,必须要用"",如果用<>则gcc只会到系统目录下去搜索,不会到本当前目录下搜索
就是""在用户目录下,<>在系统目录下,这个在windows上不严格, 在linux里似乎很严格
hello.c:
#include <stdio.h> void hello(char name[]) { printf("Hello %s!/n", name); }