Prime
Prime

Reputation: 4241

Linker error giving me a headache

I'm getting a linker error I can't seem to resolve… I'm using Qt version 4.7 with the i686-apple-darwin10-g++-4.2.1 compiler on Mac OS X 10.6.8

I can't seem to find the problem, although I'm sure its just a dumb mistake attributable to my own naivete…

I posted the compiler output and the 2 files involved (most of them) just to make sure I don't leave out something important.

The compiler gives the following output:

Linking CXX executable GLBall
Undefined symbols:
  "BallGLWidget::resizeGL(int, int)", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "_main", referenced from:
      start in crt1.10.6.o
  "BallGLWidget::paintGL()", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "BallGLWidget::~BallGLWidget()", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "BallGLWidget::~BallGLWidget()", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "non-virtual thunk to BallWindow::~BallWindow()", referenced from:
      vtable for BallWindowin moc_DesktopMain.cxx.o
  "non-virtual thunk to BallGLWidget::~BallGLWidget()", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "non-virtual thunk to BallWindow::~BallWindow()", referenced from:
      vtable for BallWindowin moc_DesktopMain.cxx.o
  "BallWindow::~BallWindow()", referenced from:
      vtable for BallWindowin moc_DesktopMain.cxx.o
  "non-virtual thunk to BallGLWidget::~BallGLWidget()", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "BallGLWidget::initializeGL()", referenced from:
      vtable for BallGLWidgetin moc_DesktopMain.cxx.o
  "BallWindow::~BallWindow()", referenced from:
      vtable for BallWindowin moc_DesktopMain.cxx.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [GLBall] Error 1
make[1]: *** [CMakeFiles/GLBall.dir/all] Error 2
make: *** [all] Error 2

Here's DesktopMain.cpp: (NOTE: headers omitted for brevity)

//-----------------------------------------
//FILE-SCOPE POINTERS
//-----------------------------------------

static BallWindow* mainwindow;
//-----------------------------------------

//-----------------------------------------
//class BallGLWidget IMPLEMENTATIONS
//-----------------------------------------

void BallGLWidget::initializeShaders()
{
    char* vs, fs;

    vertexShaderHandle   = glCreateShader(GL_VERTEX_SHADER);
    fragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);

    vs = readFile(VERTEX_SHADER_FILE_NAME);
    fs = readFile(FRAGMENT_SHADER_FILE_NAME);

    const char* vv = vs, *ff = fs;

    glShaderSource(vertexShaderHandle  , 1, &vv, NULL);
    glShaderSource(fragmentShaderHandle, 1, &ff, NULL);

    delete[] vs; delete[] fs;

    glCompileShader(vertexShaderHandle);
    glCompileShader(fragmentShaderHandle);

    programHandle = createProgram();

    glAttachShader(programHandle, vertexShaderHandle);
    glAttachShader(programHandle, fragmentShaderHandle);

    glLinkProgram(programHandle);
    glUseProgram(programHandle);
}

void BallGLWidget::deleteShaders()
{
    glDetachShader(programHandle, vertexShaderHandle);
    glDetachShader(programHandle, fragmentShaderHandle);

    glDeleteShader(vertexShaderHandle);
    glDeleteShader(fragmentShaderHandle);

    glDeleteProgram(programHandle);
}

GLuint BallGLWidget::loadTexture(const char* fptr)
{
    QImage* img = new QImage();
    if(!img->load(fptr))
    {
        //error loading image, handle error
    }

    //bind the texture to the current context
    GLuint texHandle = bindTexture(&img);

    delete img;

    return texHandle;
}

void BallGLWidget::initializeGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    initializeShaders();

    glBindAttribLocation(VERTEX_POS_NUM, VERTEX_POS_ATTRIB_NAME);
    glBindAttribLocation(TEX_POS_NUM, TEX_COORD_ATTRIB_NAME);
    glBindAttribLocation(COLOR_POS_NUM, COLOR_ATTRIB_NAME);

    glEnableVertexArray(VERTEX_POS_NAME);
    glEnableVertexArray(TEX_POS_NUM);
    glEnableVertexArray(COLOR_POS_NUM);

    ball_texture_handle = loadTexture(BALL_IMAGE_PATH);

    samplerUniformLocation = 
            glGetUniformLocation(programHandle, BALL_SAMPLER_NAME);

    glActiveTexture(GL_TEXTURE0 + samplerUniformLocation);

    //bind it in initialization because we're only using
    //1 texture in the program
    glBindTexture(GL_TEXTURE_2D, ball_texture_handle);

    //construct C++ objects
    ball = new Ball(BALL_DIAMETER);
    colorTrail = new ColorTrail(programHandle);
}

void BallGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);

    ball      ->draw();
    colorTrail->add_segment(ball->getTopLeftCornerCoord(),
                            ball->getLeftCornerCoord());

    ball->updatePhysics();
}

void BallGLWidget::resizeGL(int width, int height)
{
    //should this be the constants or the parameters?
    //where are the camera functions in OpenGL ES 2...
    glViewport(0, 0, BOX_WIDTH, BOX_HEIGHT);
}

void BallGLWidget::cleanupGL()
{
    deleteShaders();
}

BallGLWidget::BallGLWidget(QWidget *parent = 0)
{

}

BallGLWidget::~BallGLWidget()
{
    cleanupGL();
    delete ball;
    delete colorTrail;
}

//-----------------------------------------
//class BallWindow IMPLEMENTATIONS
//-----------------------------------------

void BallWindow::createWindow()
{
    //minimum size is defined in GlobalConstants.h
    setMinimumSize(QSize(BOX_WIDTH, BOX_HEIGHT));

    BallGLWidget* glWidget = new BallGLWidget;

    setAttribute(Qt::WA_DeleteOnClose);

    setCentralWidget(glWidget);
}

BallWindow::BallWindow(QWidget * parent = 0)
{
    createWindow();
}

BallWindow::~BallWindow()
{

}


//-----------------------------------------
//main FUNCTION
//-----------------------------------------
int main(int argc, const char* argv[])
{
    QApplication application(argc, argv);
    mainwindow = new BallWindow();
    BallWindow->show();

    return application.exec();
}

DesktopMain.h:

class BallGLWidget : public QGLWidget
{
    Q_OBJECT

private:
    //----------------------------
    //HANDLES
    //----------------------------
    GLuint ball_texture_handle;
    GLuint vertexShaderHandle, fragmentShaderHandle;
    GLuint programHandle;
    GLuint samplerUniformLocation;

    //----------------------------
    //PRIVATE VARIABLES
    //----------------------------
    Ball*       ball;
    ColorTrail* colorTrail;

//----------------------------
//PRIVATE METHODS
//----------------------------
void initializeShaders();
void deleteShaders();

GLuint loadTexture(const char* fptr);


protected:
     void initializeGL();
     void paintGL();
     void resizeGL(int width, int height);
     void cleanupGL();

public:
     BallGLWidget(QWidget *parent = 0);
     virtual ~BallGLWidget();
};


class BallWindow : public QMainWindow
{
    Q_OBJECT
private:
    void createWindow();
public:
    BallWindow(QWidget *parent = 0);
    ~BallWindow();
};

//void onProgramExit();

int main(int argc, const char* argv[]);

EDIT: I'm using CMake for this project. Sorry for forgetting to post this in the original question!

Here's the CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

project(GLBall)

SET(ENV{SOURCES} src/Ball.cpp src/Ball.h src/ColorTrail.cpp src/ColorTrail.h  src/DesktopMain.h src/DesktopMain.cpp src/GlobalConstants.h src/ShaderOps.h)

FIND_PACKAGE(Qt4 REQUIRED)
FIND_PACKAGE(OpenGL REQUIRED)

SET(QT_USE_QTOPENGL TRUE)

QT4_WRAP_CPP(HEADERS_MOC src/DesktopMain.h)

INCLUDE(${QT_USE_FILE})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

ADD_DEFINITIONS(${QT_DEFINITIONS})

SET(CMAKE_EXE_COMPILER_FLAGS -g -v)

ADD_EXECUTABLE(GLBall ${SOURCES} ${HEADERS_MOC})

TARGET_LINK_LIBRARIES(GLBall ${QT_LIBRARIES})

Upvotes: 0

Views: 937

Answers (3)

Sam Hartsfield
Sam Hartsfield

Reputation: 1451

I believe that

SET(ENV{SOURCES} src/Ball.cpp src/Ball.h src/ColorTrail.cpp src/ColorTrail.h  src/DesktopMain.h src/DesktopMain.cpp src/GlobalConstants.h src/ShaderOps.h)

should be

SET(SOURCES src/Ball.cpp src/Ball.h src/ColorTrail.cpp src/ColorTrail.h  src/DesktopMain.h src/DesktopMain.cpp src/GlobalConstants.h src/ShaderOps.h)

since you're referencing ${SOURCES} later, not an environment variable. That could certainly do it. Actually the "sources" should not include the headers (pass the header files with Q_OBJECT macros to qt4_wrap_cpp as you're doing). Here's the modified file:

cmake_minimum_required(VERSION 2.6)

project(GLBall)

set(SOURCES src/Ball.cpp src/ColorTrail.cpp src/DesktopMain.cpp)

find_package(Qt4 REQUIRED)
find_package(OpenGL REQUIRED)
set(QT_USE_QTOPENGL TRUE)
include(${QT_USE_FILE})

include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_definitions(${QT_DEFINITIONS})

set(CMAKE_EXE_COMPILER_FLAGS -g -v)

qt4_wrap_cpp(HEADERS_MOC src/DesktopMain.h)

add_executable(GLBall ${SOURCES} ${HEADERS_MOC})
target_link_libraries(GLBall ${QT_LIBRARIES})

Also, instead of setting the compiler flags manually, you could change the CMAKE_BUILD_TYPE to "Debug" (or "Release") when you run ccmake or cmake-gui.


Probably has nothing to do with it, but just in case, I usually put qt4_wrap_cpp calls after include(${QT_USE_FILE}).

Upvotes: 2

Shantanu Tushar
Shantanu Tushar

Reputation: 113

Like Luca said, make sure you're running qmake properly, here are the usual steps-

$ qmake -project
$ qmake
$ make

($ is the shell prompt)

If that doesn't fix it, maybe you should let us know your directory structure.

Upvotes: 2

Luca Carlon
Luca Carlon

Reputation: 9986

Try to clean the project a run qmake. Then rebuild.

Upvotes: 1

Related Questions