Recently, when I tried to use Makefile to compile and link several .cpp files under Ubuntu, I found that irregular writing of header files and source files would cause error problems. Now I will record the definition specifications of C++/C header files and source files:
Requirements: Write main.cpp, myfunctions.h, myfunctions.cpp, and include the myfunctions.h header file in main.cpp to call the function implemented in myfunctions.cpp to print out Hello world!
myfunctions.h definition:
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
void MyFunction(void);
#endif
myfunctions.cpp definition:
#include <stdio.h>
#include "myfunctions.h"
void MyFunction(void){
printf("Hello world!\n");
return ;
}
main.cpp definition:
#include <stdio.h>
#include "myfunctions.h"
int main(){
MyFunction();
return 0;
}
It should be noted that to include header files in main.cpp, you need to use #include "myfunctions.h" but #include <myfunctions.h> cannot be used. The source file that implements the function must include the corresponding header file used.