C++/C 头文件和源文件的定义

最近在ubuntu下尝试使用Makefile编译链接几个.cpp文件时,发现头文件和源文件编写不规范会引发报错问题,现记录一下C++/C 头文件和源文件的定义规范:

要求:编写main.cpp,myfunctions.h,myfunctions.cpp,在main.cpp中包含myfunctions.h头文件以调用myfunctions.cpp中实现的函数打印输出Hello world!

myfunctions.h定义:

#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H

void MyFunction(void);

#endif 

 myfunctions.cpp定义:

#include <stdio.h>
#include "myfunctions.h"
void MyFunction(void){
	printf("Hello world!\n");
	return ;
}

main.cpp定义: 

#include <stdio.h>
#include "myfunctions.h"

int main(){
	MyFunction();
	return 0;
}

需要注意的是,main.cpp中包含头文件需使用 #include "myfunctions.h"不能使用#include <myfunctions.h>,实现函数的源文件中需包含使用的相应头文件。

猜你喜欢

转载自blog.csdn.net/Reading8/article/details/132630694
今日推荐