好吧我累了找问题所以我想现在问时间:) 在我描述这个问题之前,我的项目在visual 2013上工作得很好但不是suse linux g ++ 4.6.2
我们假设使用一个库cio,它由三个文件console.h,console.cpp和keys.h组成。使用三个文件的主程序称为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”,但问题仍然存在。
答案 0 :(得分:0)
您必须将每个翻译单元(也称为.cpp
文件)链接在一起。
使用g++ happyface.cpp console.cpp
进行编译。
答案 1 :(得分:0)
extern Console console; // console object - external linkage
确保它确实在某处定义,例如在console.cpp
。我没有看到任何定义。
g++ -I ~/happy/console.h ~/happy/console.cpp ~/happy/keys.h
-I
标志不是必需的。假设您的目录如下所示:
~/happy
- console.h
- console.cpp
- keys.h
- happyface.cpp
你应该能够做到
cd ~/happy
g++ console.cpp happyface.cpp -o happyface
./happyface
答案 2 :(得分:0)
您在console.h中将变量控制台声明为:
extern Console console;
但你还没有在任何地方定义它。
在happy.cpp中添加定义:
namespace cio {
Console console;
}
另一个明显的问题:您没有编译和链接console.cpp文件。 '对于快速和脏编译,你可以这样做:
g++ happyface.cpp console.cpp