//TestClass.h
#include <iostream>
class TestClass{
public:
TestClass();
~TestClass();
void Print();
};
//TestClass.cpp
#include "TestClass.h"
TestClass::TestClass()
{
}
TestClass::~TestClass()
{
}
void TestClass::Print()
{
std::cout<<"TestClass::Print()"<<std::endl;
}
//TestClass2.h
#include <iostream>
class TestClass2
{
public:
TestClass2();
~TestClass2();
void Print();
};
//TestClass2.cpp
#include "TestClass2.h"
TestClass2::TestClass2()
{
}
TestClass2::~TestClass2()
{
}
void TestClass2::Print()
{
std::cout<<"TestClass2::Print()"<<std::endl;
}
//main.cpp
#include <iostream>
#include "TestClass.h"
#include "TestClass2.h"
int main()
{
TestClass test;
test.Print();
TestClass2 test2;
test2.Print();
std::cout<<"This is my first C++ programming."<<std::endl;
}
编译运行:
[b][color=black]
kevin@kevin-desktop:~/ctest$ g++ -c TestClass.cpp -o TestClass.o
kevin@kevin-desktop:~/ctest$ g++ -c TestClass2.cpp -o TestClass2.o
kevin@kevin-desktop:~/ctest$ g++ -g TestClass.o TestClass2.o main.cpp -o main
kevin@kevin-desktop:~/ctest$ ./main
TestClass::Print()
TestClass2::Print()
This is my first C++ programming.
kevin@kevin-desktop:~/ctest$
[/color][/b]
也可以用Makefile 编译
[b][color=black]
start:main.o TestClass.o TestClass2.o
g++ -g main.o TestClass.o TestClass2.o -o main
main.o: main.cpp TestClass.cpp TestClass2.cpp
g++ -c main.cpp
TestClass.o:TestClass.cpp
g++ -c TestClass.cpp
TestClass2.o:TestClass2.cpp
g++ -c TestClass2.cpp
clean:
rm edit main.o TestClass.o TestClass2.o
[/color][/b]
或
[b][color=black]
objects=main.o TestClass.o TestClass2.o
cppfiles=main.cpp TestClass.cpp TestClass2.cpp
all:$(objects)
g++ -g $(objects) -o main
main.o:$(cppfiles)
g++ -c main.cpp
TestClass.o:TestClass.cpp
g++ -c TestClass.cpp
TestClass2.o:TestClass2.cpp
g++ -c TestClass2.cpp
clean:
rm main $(objects)
[/color][/b]
输入make
[b][color=black]
kevin@kevin-desktop:~/ctest$ make
g++ -c main.cpp
g++ -c TestClass.cpp
g++ -c TestClass2.cpp
g++ -g main.o TestClass.o TestClass2.o -o main
kevin@kevin-desktop:~/ctest$
[/color][/b]