使用g++或CC编译多个c++文件
Use g++ or CC to compile multiple C++ files
大家好,
我试图查找如何在*nix命令行上编译几个c++文件。
我试过这两个链接使用g++编译多个。cpp和。h文件
使用g++编译多个。cpp和。h文件
我有一个简单的抽象类: // Base class
class Shape
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
然后是派生的
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
这是驱动程序:
#include <iostream>
using namespace std;
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
return 0;
}
这是一个简单的,所以我不需要一个makefile,但这是我尝试过的:
> g++ Shape.cc - This creates a Shape.o
> g++ Shape.cc Rectangle.cc ShapeDriver.cc - This creates an error
> g++ ShapeDriver.cc Shape.cc Rectangle.ccc - This creates an error
结果是矩形。Cc不识别宽度和高度的定义,这是有意义的。
我还需要做什么来编译这个?我对c++一窍不通。
TIA,
coson
您需要在不同的文件中添加以下内容…
矩形顶部。cc
#include "Shape.cc"
ShapeDriver.cc顶部
#include "Rectangle.cc"
同样,在第三行gcc中,有一个错别字
g++ ShapeDriver.cc Shape.cc Rectangle.ccc - This creates an error
应该是Rectangle.cc
你的问题是,在你的每个文件中,不同的类从来没有被定义过,所以他们不知道如何使用它们。喜欢……"矩形"首先需要知道"形状"是什么,然后才能从它派生出来。您应该在w/类定义之间使用。h文件,并将它们包含在其他。cc文件中,以便它们知道它们正在调用的其他类结构。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到