main.cpp
#include <stdio.h>
//#include <iostream>
#include "show.h"
int main()
{
Show a;
a.Print();
return 0;
}
show.h
#ifndef TEST_MAKE_SHOW_H_
#define TEST_MAKE_SHOW_H_
class Show
{
private:
int a;
public:
Show();
~Show();
int Print();
};
#endif //TEST_MAKE_SHOW_H_
show.cpp
#include <stdio.h>
#include <stdlib.h>
#include "show.h"
Show::Show()
{}
Show::~Show()
{}
int Show::Print()
{
printf("hello world\n");
return 0;
}
makefile
#
edit:main.o show.o
g++ -o edit main.o show.o
show.o: show.cpp
g++ -c show.cpp
main.o: main.cpp
g++ -c main.cpp
clean:
rm edit main.o Show.o
#
后记
第一次些C++类的makefile,遇到很多挫折,关键还是对gcc/g++的命令不熟悉
顺便也将错误记下 :
1、将头文件stdlib.h、stdio.h、show.h添加进去(错误:make: *** 没有规则可以创建“main.o”需要的目标“shwo.h”。 停止。),殊不知C++跟C不一样,
在C++中源文件*.cpp>*.h
2、g++ -o edit main.o show.o中省略了-o后面的target,这是对g++规则命令不熟引起
本文详细介绍了如何使用C++编写类,并结合Makefile进行编译管理,通过实例展示了从头文件到源文件的完整过程,以及在编译过程中遇到的问题及解决方法。
8818

被折叠的 条评论
为什么被折叠?



