建立一个工程,工程包含三个文件:main.cpp, func.h, func.cpp.
main.cpp:
//main.cpp
#include <iostream>
#include <cstdio> // atoi()
#include "func.h" // ADD()
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
if(argc != 3){
std::cout << "Usage: " << argv[0]
<< " op1 op2" << std::endl;
return 1;
}
int a, b;
a = atoi(argv[1]);
b = atoi(argv[2]);
std::cout << ADD(a, b) << std::endl;
return 0;
}
func.h:
//func.h
#ifndef FUNC_H
#define FUNC_H
int ADD(int a, int b);
#endif
//实验证明:预编译操作符后面跟FUNC_H或func.h都是可以编译通过的
/*
#ifndef func.h
#define func.h
int ADD(int a, int b);
#endif
*/
func.cpp:
//func.cpp
#include "func.h"
int ADD(int a, int b)
{
return a + b;
}
Dev Cpp自动生成的Makefile文件内容为:
# Project: Project001
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = main.o func.o
LINKOBJ = main.o func.o
LIBS = -L"C:/Program Files/Dev-Cpp/MinGW64/lib32" -L"C:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc -m32
INCS = -I"C:/Program Files/Dev-Cpp/MinGW64/include" -I"C:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"C:/Program Files/Dev-Cpp/MinGW64/include" -I"C:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN = Project001.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11
CFLAGS = $(INCS) -m32 -std=c++11
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
main.o: main.cpp
$(CPP) -c main.cpp -o main.o $(CXXFLAGS)
func.o: func.cpp
$(CPP) -c func.cpp -o func.o $(CXXFLAGS)
编译运行时需要在“参数”窗口输入命令行参数。