[Qt-interest] Static Lib dependecy/linking problem...

Dan O'Connor beguiledfoil at gmail.com
Tue Oct 13 02:45:13 CEST 2009


I am using multiple libraries in my project, and I've noticed that
functions or methods which are not called in the final project are not
being included for linking. Below I've attempted to reproduce a
concrete example of my problem:

The example consists of three projects: Two static libs and one simple
project with a main function. testlibone contains nothing but a
function in a namespace. testlibtwo contains a single class definition
calls that function in one of its class methods. The main function
calls the method in testlibtwo's class.

When compiling the testprogram.pro file (which contains the main
function) I get the following linking error:

/usr/bin/ld: Undefined symbols:
TestFunctions::DoSomethingToAnInt(int&)
collect2: ld returned 1 exit status

By adding the following lines to the main function linking completes
successfully:

int a = 0;
TestFunctions::DoSomethingToAnInt( a );

What is the correct way to create libraries which can be used by other
libraries?

testlibone.pro:

QT -= core \
    gui
TARGET = testlibone
TEMPLATE = lib
CONFIG += staticlib

HEADERS += testfunctions.h
SOURCES += testfunctions.cpp

testfunctions.h:

#ifndef __TESTFUNCTIONS_H
#define __TESTFUNCTIONS_H

namespace TestFunctions
{
    void DoSomethingToAnInt( int& an_int );
};

#endif // __TESTFUNCTIONS_H

testfunctions.cpp:

#include "testfunctions.h"

void TestFunctions::DoSomethingToAnInt( int& an_int )
{
    if( an_int == 0 )
    {
        ++an_int;
    }
    else
    {
        an_int = 15;
    }
}

testlibtwo.pro:

QT -= core \
    gui
TARGET = testlibtwo
TEMPLATE = lib
CONFIG += staticlib

HEADERS += testclass.h
SOURCES += testclass.cpp

LIBS += -L../testlibone -ltestlibone
INCLUDEPATH += ../testlibone

testclass.h:

#ifndef __TESTCLASS_H
#define __TESTCLASS_H

class TestClass
{
public:
    TestClass();
    ~TestClass();

    void Mutate();
private:
    int dataMember;
};

#endif // __TESTCLASS_H


testclass.cpp:

#include "testclass.h"
#include "testfunctions.h"

TestClass::TestClass()
    : dataMember( 0 )
{
}

TestClass::~TestClass()
{
}

void TestClass::Mutate()
{
    TestFunctions::DoSomethingToAnInt(dataMember);
}

testprogram.pro

QT -= core \
    gui

LIBS += -L../testlibone -ltestlibone \
    -L../testlibtwo -ltestlibtwo
INCLUDEPATH += ../testlibone \
    ../testlibtwo

SOURCES += main.cpp

main.cpp:

#include "testclass.h"
#include "testfunctions.h"

int main(int argc, char *argv[])
{
    int a = 0;
    TestFunctions::DoSomethingToAnInt( a );

    TestClass foo;
    foo.Mutate();
}



More information about the Qt-interest-old mailing list