3

我一直在努力将我的一款游戏移植到 Linux 上,但似乎无法弄清楚我收到错误的原因。该游戏最初是在 Visual Studio 2010 中编写的,我已经提取了所有需要的内容(标题、cpp、纹理)并正在尝试编译。

使用文件编译g++ -c -o exampleFile.o exampleFile.cpp工作正常,没有任何错误。然而,在链接时,我遇到了数百个关于 std 函数的错误,例如:

Bmp.o: In function `Image::Bmp::Bmp()':
Bmp.cpp:(.text+0x58): undefined reference to `std::allocator<char>::allocator()'
Bmp.cpp:(.text+0x74): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
Bmp.cpp:(.text+0x80): undefined reference to `std::allocator<char>::~allocator()'
Bmp.cpp:(.text+0x91): undefined reference to `std::allocator<char>::~allocator()'

完整的输出可以在PasteBin上找到

Bmp.cpp文件是别人写的库函数,可以在这里找到上面省略的代码是:

#include <fstream>
#include <iostream>
#include <cstring>
#include "Bmp.h"
using std::ifstream;
using std::ofstream;
using std::ios;
using std::cout;
using std::endl;
using namespace Image;

///////////////////////////////////////////////////////////////////////////////
// default constructor
///////////////////////////////////////////////////////////////////////////////
Bmp::Bmp() : width(0), height(0), bitCount(0), dataSize(0), data(0), dataRGB(0),
         errorMessage("No error.")
{
}

Bmp::Bmp(const Bmp &rhs)
{
    // copy member variables from right-hand-side object
    width = rhs.getWidth();
    height = rhs.getHeight();
    bitCount = rhs.getBitCount();
    dataSize = rhs.getDataSize();
    errorMessage = rhs.getError();

    if(rhs.getData())       // allocate memory only if the pointer is not NULL
    {
        data = new unsigned char[dataSize];
        memcpy(data, rhs.getData(), dataSize); // deep copy
    }
    else
        data = 0;           // array is not allocated yet, set to 0

    if(rhs.getDataRGB())    // allocate memory only if the pointer is not NULL
    {
        dataRGB = new unsigned char[dataSize];
        memcpy(dataRGB, rhs.getDataRGB(), dataSize); // deep copy
    }
    else
        dataRGB = 0;        // array is not allocated yet, set to 0
}

不太确定问题是什么,但让我觉得链接器无法访问 std 函数?提前感谢您的帮助。

Edit Linking command: gcc -o LDTux Bmp.o character.o chickenList.o chicken.o farmList.o farm.o fieldList.o field.o generall_utils.o landscape.o object.o SZ_NumberList.o SZ_Sprite.o worm.o wormList.o wormSpawn.o wormSpawnList.o GameWorld.o HelloOpenGL.o -lGL -lglut -lm

4

2 回答 2

26

As pointed out earlier by Dietmar Kühl in the comments, you should change the linker command from gcc to g++.

于 2013-09-25T22:50:49.177 回答
3

As pointed out by Dietmar Kühl in the comments, I was using gcc to link, rather than g++.

Upon amending the linking command, I received ...undefined reference to 'gluLookAt' which was fixed by adding -lGLU.

于 2013-09-25T22:51:01.750 回答