|
|
一个简单的Demo演示源程序- [root@root Make]# ls
- include main.cpp Makefile README src
- [root@root Make]# less include/hello.h
- #ifndef _HELLO_H
- #define _HELLO_H
- void Display(char *);
- #endif
- [root@root Make]# less src/hello.cpp
- #include <stdio.h>
- #include <stdlib.h>
- #include <include/hello.h>
- void Display(char *pChar)
- {
- fprintf(stdout, "%s\n", pChar);
-
- return;
- }
- [root@root Make]# less main.cpp
- #include <stdio.h>
- #include <stdlib.h>
- #include <include/hello.h>
- int main(int argc, char *argv[])
- {
- Display(argv[1]);
- return 1;
- }
复制代码 Makefile的结构- [root@root Make]# less Makefile
- # Compiler.
- CC = gcc
- CCPLUS = g++
- BASEDIR= ./
- SRCS =$(BASEDIR)main.cpp \
- $(BASEDIR)/src/hello.cpp
- OBJS =$(BASEDIR)main.o \
- $(BASEDIR)/src/hello.o
-
- # Compilers options for tool.
- CFLAGS = -Wall -g
- INCS = -I.
- 第一种写法:编译失败,为什么?
- $(OBJS) : $(SRCS)
- $(CCPLUS) $(CFLAGS) $(INCS) -c $^ -o $@
- 第二种写法:只能编译前一个main.cpp, src/hello.cpp不能编译,为什么?
- $(BASEDIR)main.o : $(BASEDIR)main.cpp
- $(CCPLUS) $(CFLAGS) $(INCS) -c $< -o $@
- $(BASEDIR)/src/hello.o : $(BASEDIR)/src/hello.cpp
- $(CCPLUS) $(CFLAGS) $(INCS) -c $< -o $@
- demo: $(OBJS)
- $(CCPLUS) -o $@ $^
- all: demo
- .PHONY: clean
- clean:
- rm $(OBJS)
- rm demo
复制代码 目的:是想通过Makefile,在当前目录下生成main.o,在src/下生成hello.o, 然后又在当前目录下生成ELF的demo。如果用make的implicit rules时,改一下他们的头文件路径就可以编译通过。用自己的rules,始终没过。 |
|