如何使用gcc/g++编译多个cpp文件的头文件?
How to compile multiple cpp file header file using gcc/g++?
我正在尝试编译多个 .cpp 和 .h 文件。但是显示错误。
我的文件是:
ApplicationWindow.cpp
#include "ApplicationWindow.h"
void ApplicationWindow::DrawContents ()
{
GetView()->DrawOn(this);
}
ApplicationWindow.h
#ifndef APPLICATION_WINDOW_H
#define APPLICATION_WINDOW_H
#include "Window.h"
class ApplicationWindow : public Window
{
public:
// ...
virtual void DrawContents();
};
#endif /* APPLICATION_WINDOW_H */
Window.h
#ifndef WINDOW_H
#define WINDOW_H
#include "View.h"
class Point;
class WindowImp;
class Window {
public:
Window(View* contents);
// requests handled by window
virtual void DrawContents();
virtual void Open();
virtual void Close();
virtual void Iconify();
virtual void Deiconify();
// requests forwarded to implementation
virtual void SetOrigin(const Point& at);
virtual void SetExtent(const Point& extent);
virtual void Raise();
virtual void Lower();
virtual void DrawLine(const Point&, const Point&);
virtual void DrawRect(const Point&, const Point&);
virtual void DrawPolygon(const Point[], int n);
virtual void DrawText(const char*, const Point&);
protected:
WindowImp* GetWindowImp();
View* GetView();
private:
WindowImp* _imp;
View* _contents; // the window's contents
};
#endif /* WINDOW_H */
Window.cpp
#include "Window.h"
#include "WindowImp.h"
#include "WindowSystemFactory.h"
void Window::DrawRect (const Point& p1, const Point& p2) {
WindowImp* imp = GetWindowImp();
imp->DeviceRect(p1.X(), p1.Y(), p2.X(), p2.Y());
}
WindowImp* Window::GetWindowImp () {
if (_imp == 0) {
_imp = WindowSystemFactory::Instance()->MakeWindowImp();
}
return _imp;
}
View.h
#ifndef VIEW_H
#define VIEW_H
class ApplicationWindow;
class View {
public:
void DrawOn(const ApplicationWindow* w) const;
};
#endif /* VIEW */
View.cpp
#include "View.h"
#include <iostream>
using namespace std;
void View::DrawOn(const ApplicationWindow* w) const
{
cout << "DrawOn(" << w << ")" << endl;
}
我也有 WindowImp.h 和 WindowSystemFactory.h 文件。
我尝试了从其他 Whosebug
答案中找到的所有方法 (Using G++ to compile multiple .cpp and .h files.
但是显示如下错误:
"Window::DrawPolygon(Point const*, int)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Open()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Close()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Lower()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Raise()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::GetView()", referenced from:
ApplicationWindow::DrawContents() in ApplicationWindow.o
"Window::Iconify()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::DrawLine(Point const&, Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::DrawRect(Point const&, Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::DrawText(char const*, Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Deiconify()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::SetExtent(Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::SetOrigin(Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"View::DrawOn(ApplicationWindow const*) const", referenced from:
ApplicationWindow::DrawContents() in ApplicationWindow.o
"typeinfo for Window", referenced from:
typeinfo for ApplicationWindow in ApplicationWindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
还有我的g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
我正在使用 Mac OSX 10.9.4。我知道这是上述答案的重复,但我仍然无法解决。如果有人帮助我,那对我来说会很棒。
我只需要 运行 g++ ApplicationWindow.cpp
文件就不会出现上述错误。但我尝试 link 所有其他 .cpp 和 .h 文件按照我在上面粘贴的 link 的回答中提到的方式归档。提前致谢....
您在 Window 中声明了很多虚函数。 header 但您没有定义任何实现。
您可以编译每个 .cpp 文件,因为其中没有语法错误。但是,链接器无法找到您声明的函数,因此无法生成可执行文件。
http://www.parashift.com/c++-faq/link-errs-missing-vtable.html
只需先隐藏所有未使用且未定义的虚函数,然后再试一次。 (或向它们添加空实现)
我正在尝试编译多个 .cpp 和 .h 文件。但是显示错误。
我的文件是:
ApplicationWindow.cpp
#include "ApplicationWindow.h"
void ApplicationWindow::DrawContents ()
{
GetView()->DrawOn(this);
}
ApplicationWindow.h
#ifndef APPLICATION_WINDOW_H
#define APPLICATION_WINDOW_H
#include "Window.h"
class ApplicationWindow : public Window
{
public:
// ...
virtual void DrawContents();
};
#endif /* APPLICATION_WINDOW_H */
Window.h
#ifndef WINDOW_H
#define WINDOW_H
#include "View.h"
class Point;
class WindowImp;
class Window {
public:
Window(View* contents);
// requests handled by window
virtual void DrawContents();
virtual void Open();
virtual void Close();
virtual void Iconify();
virtual void Deiconify();
// requests forwarded to implementation
virtual void SetOrigin(const Point& at);
virtual void SetExtent(const Point& extent);
virtual void Raise();
virtual void Lower();
virtual void DrawLine(const Point&, const Point&);
virtual void DrawRect(const Point&, const Point&);
virtual void DrawPolygon(const Point[], int n);
virtual void DrawText(const char*, const Point&);
protected:
WindowImp* GetWindowImp();
View* GetView();
private:
WindowImp* _imp;
View* _contents; // the window's contents
};
#endif /* WINDOW_H */
Window.cpp
#include "Window.h"
#include "WindowImp.h"
#include "WindowSystemFactory.h"
void Window::DrawRect (const Point& p1, const Point& p2) {
WindowImp* imp = GetWindowImp();
imp->DeviceRect(p1.X(), p1.Y(), p2.X(), p2.Y());
}
WindowImp* Window::GetWindowImp () {
if (_imp == 0) {
_imp = WindowSystemFactory::Instance()->MakeWindowImp();
}
return _imp;
}
View.h
#ifndef VIEW_H
#define VIEW_H
class ApplicationWindow;
class View {
public:
void DrawOn(const ApplicationWindow* w) const;
};
#endif /* VIEW */
View.cpp
#include "View.h"
#include <iostream>
using namespace std;
void View::DrawOn(const ApplicationWindow* w) const
{
cout << "DrawOn(" << w << ")" << endl;
}
我也有 WindowImp.h 和 WindowSystemFactory.h 文件。
我尝试了从其他 Whosebug
答案中找到的所有方法 (Using G++ to compile multiple .cpp and .h files.
但是显示如下错误:
"Window::DrawPolygon(Point const*, int)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Open()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Close()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Lower()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Raise()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::GetView()", referenced from:
ApplicationWindow::DrawContents() in ApplicationWindow.o
"Window::Iconify()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::DrawLine(Point const&, Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::DrawRect(Point const&, Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::DrawText(char const*, Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::Deiconify()", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::SetExtent(Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"Window::SetOrigin(Point const&)", referenced from:
vtable for ApplicationWindow in ApplicationWindow.o
"View::DrawOn(ApplicationWindow const*) const", referenced from:
ApplicationWindow::DrawContents() in ApplicationWindow.o
"typeinfo for Window", referenced from:
typeinfo for ApplicationWindow in ApplicationWindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
还有我的g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
我正在使用 Mac OSX 10.9.4。我知道这是上述答案的重复,但我仍然无法解决。如果有人帮助我,那对我来说会很棒。
我只需要 运行 g++ ApplicationWindow.cpp
文件就不会出现上述错误。但我尝试 link 所有其他 .cpp 和 .h 文件按照我在上面粘贴的 link 的回答中提到的方式归档。提前致谢....
您在 Window 中声明了很多虚函数。 header 但您没有定义任何实现。
您可以编译每个 .cpp 文件,因为其中没有语法错误。但是,链接器无法找到您声明的函数,因此无法生成可执行文件。
http://www.parashift.com/c++-faq/link-errs-missing-vtable.html
只需先隐藏所有未使用且未定义的虚函数,然后再试一次。 (或向它们添加空实现)