さて、質問を探すのに疲れたので、今質問する時間だと思います:) 問題を説明する前に、私のプロジェクトはVisual 2013で正常に動作しますが、Linux g ++ 4.6.2を使用しません
console.h、console.cpp、keys.h の 3 つのファイルで構成されるライブラリ cio を使用するとします。3 つのファイルを使用するメイン プログラムは happy.cpp と呼ばれます。
Visual Studio 2013 では、すべて正常に動作します。しかし、Linux でコンパイルしようとすると、多くのエラーが発生します。
以下は、プロジェクトの簡単なコードの説明です
//console.h
namespace cio {
// Console holds the state of the Console Input Output Facility
//
class Console {
//some varialbes and functions
int getRows() const;
};
extern Console console; // console object - external linkage
} // end namespace cio
===============================================================================
//console.cpp
/* table of platforms */
#define CIO_LINUX 1
#define CIO_MICROSOFT 2
#define CIO_BORLAND 3
#define CIO_UNIX 4
/* auto-select your platform here */
#if defined __BORLANDC__
#define CIO_PLATFORM CIO_BORLAND
#define CIO_LOWER_LEVEL_H_ <conio.h>
#elif defined _MSC_VER
#define CIO_PLATFORM CIO_MICROSOFT
#include <windows.h>
#define CIO_LOWER_LEVEL_H_ <conio.h>
#elif defined __MACH__
#define CIO_PLATFORM CIO_UNIX
#define CIO_LOWER_LEVEL_H_ <curses.h>
#elif defined __GNUC__
#define CIO_PLATFORM CIO_LINUX
#define CIO_LOWER_LEVEL_H_ <ncurses.h>
#elif !defined __BORLANDC__ && !defined _MSC_VER && !defined __GNUC__ && !defined __MACH__
#error CONSOLE_PLT is undefined
#endif
extern "C" {
#include CIO_LOWER_LEVEL_H_
}
#include "console.h"
#include "keys.h"
namespace cio { // continuation of cio namespace
// getRows retrieves the number of rows in the output object
//
int Console::getRows() const {
return bufrows;
}
} // end namespace cio
================================================================================
//////happy.cpp
#include "console.h"
#include "keys.h" // for ESCAPE
using namespace cio;
int main() {
int key, rows, columns;
// get screen dimensions
rows = console.getRows();
}
「g++ happyface.cpp」コマンドを使用してコンパイルすると、次のエラーが発生します
happyface.cpp:(.text+0xd): cio::console'
happyface.cpp:(.text+0x12): undefined reference to
cio::Console::getRows() constへの未定義の参照
ここで何が間違っているのかわかりませんか?また、パス「g++ -I ~/happy/console.h ~/happy/console.cpp ~/happy/keys.h」を含めてみましたが、それでも同じ問題が発生します。