Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linker error with c++ static library which references another library

I'm pretty new to c++ and I'm having a problem debugging a linker error. I'm using wxDev-c++ with the g++ compiler. I've created a static library which I'm linking to a basic project. The library originally contained no references to external header files or libraries, it was just a couple of simple test functions adding together 2 doubles and returning the value. This worked fine when linked to the test project. However I'm now trying to incorporate FTP into that library and I'm now getting linker errors.

At the moment the test function is just trying to access the same simple addition function for testing, I'm not even calling the FTP functionality yet since I'm just trying to get the test project compiling correctly.

The library code:

DaFTPLib.h:

#ifndef WAVE_H
#define WAVE_H
#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#else
#include <wx/wxprec.h>
#endif

#include <wx/protocol/ftp.h>

namespace Wave
{
    class DaFTP
    {
        public:
        DaFTP(char *url, char *login, char *password);
        ~DaFTP();
        const int Download(char* fileName);
        static const void DownloadNCWD(char *url, char *fileName, char *login, char *password);
        static const void DownloadLAMP();
        static double FuncA(double a, double b);
        static double FuncB(double a, double b);

        private:
        char* url, login, password;
        wxFTP ftp;
    };
}

#endif

DaFTP.cpp:

#include "DaFTPLib.h"

using namespace std;
char* _url;
char* _login;
char* _password;
wxFTP ftp;

namespace Wave
{
    DaFTP::DaFTP(char *url, char *login, char *password)
    {
       _url = url;
       _login = login;
       _password = password;
    }

    DaFTP::~DaFTP(){}

    const int DaFTP::Download(char *fileName)
    {
        int fileSize;

        ftp.SetPassive(true);
        ftp.SetUser(_login);
        ftp.SetPassword(_password);
        ftp.Connect(_url);

        fileSize = ftp.GetFileSize(fileName);
        return fileSize;
    }

    const void DaFTP::DownloadNCWD(char *url, char *fileName, char *login, char *password)
    {
        DaFTP daftp(url, login, password);
        daftp.Download(fileName);
    }

    const void DaFTP::DownloadLAMP() {}

    double DaFTP::FuncA(double a, double b)
    {
         return a + b;
    }
    double DaFTP::FuncB(double a, double b)
    {
         return a - b;
    }
}

The test project code:

#include <cstdlib>
#include <iostream>

#include "../libDaFTP/DaFTPLib.h"

using namespace std;

int main(int argc, char *argv[])
{
    double a, b, c;

    a = 23.32;
    b = 26.68;

    c = Wave::DaFTP::FuncA(a, b);
    cout << "FuncA val: " << c << endl;

    c = Wave::DaFTP::FuncB(a, b);
    cout << "FuncB val: " << c << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

The compile log output for the library (compiles ok):

Executing make... mingw32-make.exe -f "Makefile.win" all g++.exe -c DaFTPLib.cpp -o Objects/MingW/DaFTPLib.o -I"C:/Program Files (x86)/Dev-Cpp/lib/gcc/mingw32/3.4.5/include" -I"C:/Program Files (x86)/Dev-Cpp/include/c++/3.4.5/backward" -I"C:/Program Files (x86)/Dev-Cpp/include/c++/3.4.5/mingw32" -I"C:/Program Files (x86)/Dev-Cpp/include/c++/3.4.5" -I"C:/Program Files (x86)/Dev-Cpp/include" -I"C:/Program Files (x86)/Dev-Cpp/" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/msw" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/generic" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/html" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/protocol" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/xml" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/xrc" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx" -I"C:/Program Files (x86)/Dev-Cpp/include/common"

ar rcu "Output/MingW/libDaFTP.a" "Objects/MingW/DaFTPLib.o"

Execution terminated Compilation successful Compilation took 1 second to complete

The compile log output for the test project (compilation fails)

Executing make... mingw32-make.exe -f "Makefile.win" all g++.exe -c main.cpp -o Objects/MingW/main.o -I"C:/Program Files (x86)/Dev-Cpp/lib/gcc/mingw32/3.4.5/include" -I"C:/Program Files (x86)/Dev-Cpp/include/c++/3.4.5/backward" -I"C:/Program Files (x86)/Dev-Cpp/include/c++/3.4.5/mingw32" -I"C:/Program Files (x86)/Dev-Cpp/include/c++/3.4.5" -I"C:/Program Files (x86)/Dev-Cpp/include" -I"C:/Program Files (x86)/Dev-Cpp/" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/msw" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/generic" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/html" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/protocol" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/xml" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/xrc" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx" -I"C:/Program Files (x86)/Dev-Cpp/include/common"

g++.exe "Objects/MingW/main.o" -o "Output/MingW/LibTest.exe" -L"C:/Program Files (x86)/Dev-Cpp/Lib" ../libDaFTP/Output/MingW/libDaFTP.a

Objects/MingW/main.o:main.cpp:(.rdata$_ZTV20wxThreadHelperThread[vtable for wxThreadHelperThread]+0xc): undefined reference to wxThread::TestDestroy()' Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD1Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16): undefined reference towxThread::~wxThread()' Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD0Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16): undefined reference to wxThread::~wxThread()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x10): undefined reference towxFTP::wxFTP()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x3e): undefined reference to wxFTP::wxFTP()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x6c): undefined reference towxFTP::~wxFTP()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x82): undefined reference to wxFTP::~wxFTP()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x1e3): undefined reference towxFTP::Connect(wxString const&)' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x24e): undefined reference to wxFTP::GetFileSize(wxString const&)' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x43f): undefined reference towxFTP::wxFTP()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x45a): undefined reference to wxFTP::~wxFTP()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN8wxStringaSERKS_[wxString::operator=(wxString const&)]+0x14): undefined reference towxStringBase::operator=(wxStringBase const&)' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN12wxStringBaseC2EPKc[wxStringBase::wxStringBase(char const*)]+0x7): undefined reference to wxStringBase::npos' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN12wxStringBaseC2EPKc[wxStringBase::wxStringBase(char const*)]+0x25): undefined reference towxStringBase::InitWith(char const*, unsigned int, unsigned int)' collect2: ld returned 1 exit status

mingw32-make.exe: * [Output/MingW/LibTest.exe] Error 1

Execution terminated Compilation Failed. Make returned 2


I apologize for the long post but hopefully this is sufficient information for someone to point me in the right direction. I assume the issue lies with incorporating the wx/protocol/ftp library but I was under the assumption that when compiling my library any supporting libraries such as the ftp stuff would be compiled in with it.
like image 559
TheOx Avatar asked Nov 25 '11 19:11

TheOx


1 Answers

but I was under the assumption that when compiling my library any supporting libraries such as the ftp stuff would be compiled in with it.

What you are saying is only true for dynamically linked libraries: if you build a static library that references other libraries, the application or DLL that links to your static library still has to link to the other libraries. A static library is nothing more than a collection of compiled object code. External functions referenced inside the library code have not been linked in.

Either make your helper library a DLL and link that to the wx library, or keep it static and link every application that uses your static library to the wx library.

like image 111
Ralf Avatar answered Oct 18 '22 13:10

Ralf