Tomassino
Tomassino

Reputation: 305

G++ Undefined Reference std::

I've been working on porting one of my games to Linux and can't seem to figure out the reasons for the errors I'm received. The game was originally written in Visual Studio 2010 and I have extracted all of the needed content (headers, cpp, textures) and am trying to compile.

Compilation of files using g++ -c -o exampleFile.o exampleFile.cpp works fine without any errors. However upon linking I am greeted with hundreds of errors regarding std functions, an example:

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()'

Full output can be found on PasteBin

The Bmp.cpp file is a library function written by someone else, it can be found here The code eluded to above is:

#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
}

Not really sure what the issue is, but it strikes me that the linker can't reach the std functions? Thanks in advance for any help.

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

Upvotes: 3

Views: 18846

Answers (2)

sehe
sehe

Reputation: 393064

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

Upvotes: 26

Tomassino
Tomassino

Reputation: 305

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.

Upvotes: 3

Related Questions