[英]How to compile multiple cpp file header file using gcc/g++?
我正在嘗試編譯多個.cpp和.h文件。 但是它顯示錯誤。
我的文件是:
#include "ApplicationWindow.h"
void ApplicationWindow::DrawContents ()
{
GetView()->DrawOn(this);
}
#ifndef APPLICATION_WINDOW_H
#define APPLICATION_WINDOW_H
#include "Window.h"
class ApplicationWindow : public Window
{
public:
// ...
virtual void DrawContents();
};
#endif /* APPLICATION_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 */
#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;
}
#ifndef VIEW_H
#define VIEW_H
class ApplicationWindow;
class View {
public:
void DrawOn(const ApplicationWindow* w) const;
};
#endif /* VIEW */
#include "View.h"
#include <iostream>
using namespace std;
void View::DrawOn(const ApplicationWindow* w) const
{
cout << "DrawOn(" << w << ")" << endl;
}
我也有WindowImp.h和WindowSystemFactory.h文件。
我嘗試了從其他stackoverflow
答案中找到的所有方法( 使用G ++編譯多個.cpp和.h文件 。
但它顯示以下錯誤:
"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
文件而不會出現上述錯誤。 但是我試圖以上面我粘貼在這里的鏈接的答案中提到的方式鏈接所有其他.cpp和.h文件。 預先感謝....
您在Window中聲明了許多虛函數。 標頭,但您沒有定義任何實現。
您可以編譯每個.cpp文件,因為其中沒有語法錯誤。 但是,鏈接器找不到您要聲明的函數,因此無法生成可執行文件。
http://www.parashift.com/c++-faq/link-errs-missing-vtable.html
只需從隱藏所有未使用的虛擬函數開始,然后再定義即可,然后重試。 (或向其中添加空的實現)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.