引用另一个库的c++静态库的链接器错误

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

本文关键字:错误 链接 c++ 另一个 引用 静态      更新时间:2023-10-16

我对c++很陌生,我在调试链接器错误时遇到了问题。我使用wxdev -c++和g++编译器。我已经创建了一个静态库,我链接到一个基本的项目。该库最初不包含对外部头文件或库的引用,它只是几个简单的测试函数,将两个双精度组合在一起并返回值。当链接到测试项目时,这工作得很好。然而,我现在试图将FTP合并到该库中,我现在得到链接器错误。

目前,测试函数只是试图访问相同的简单加法函数进行测试,我甚至还没有调用FTP功能,因为我只是想让测试项目正确编译。

库代码:

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;
    }
}
测试项目代码:
#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;
}

库的编译日志输出(编译ok):

执行使……Makefile. exe -f"Makefile. exe"赢得"所有g++.exe -c DaFTPLib.cpp -o Objects/MingW/DaFTPLib. cppo - i"C:/程序文件(x86)/Dev-Cpp/lib/gcc/mingw32/3.4.5/包括"我"C:/程序文件(x86)/Dev-Cpp/include/c++/3.4.5/落后"-"C:/程序文件(x86)/Dev-Cpp/include/c++/3.4.5 mingw32"-"C:/程序文件(x86)/Dev-Cpp/include/c++/3.4.5"-"C:/程序文件(x86)/Dev-Cpp包括"我"C:/程序文件(x86)/Dev-Cpp/"我"C:/程序文件(x86)/Dev-Cpp/include/共同/wx/垃圾"——"C:/程序文件(x86)/Dev-Cpp/include/共同/wx/通用的"我"C:/程序文件(x86)/Dev-Cpp/include/共同/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/wx"

ar rcu "Output/MingW/libDaFTP. exe "一个对象/MingW/DaFTPLib.o"

执行终止编译成功编译耗时1秒完成

测试项目的编译日志输出(编译失败)

执行使……Makefile. exe -f"Makefile. exe"赢得"所有g++.exe -c main.cpp -o Objects/MingW/main. exeo - i"C:/程序文件(x86)/Dev-Cpp/lib/gcc/mingw32/3.4.5/包括"我"C:/程序文件(x86)/Dev-Cpp/include/c++/3.4.5/落后"-"C:/程序文件(x86)/Dev-Cpp/include/c++/3.4.5 mingw32"-"C:/程序文件(x86)/Dev-Cpp/include/c++/3.4.5"-"C:/程序文件(x86)/Dev-Cpp包括"我"C:/程序文件(x86)/Dev-Cpp/"我"C:/程序文件(x86)/Dev-Cpp/include/共同/wx/垃圾"——"C:/程序文件(x86)/Dev-Cpp/include/共同/wx/通用的"我"C:/程序文件(x86)/Dev-Cpp/include/共同/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/wx"

g + + . exe"/MingW的主要对象。-L"C:/Program Files (x86)/Dev-Cpp/Lib" ../libDaFTP/Output/MingW/libDaFTP. o" -o "Output/MingW/libDaFTP. exe" -L"C:/Program Files (x86)/Dev-Cpp/Lib" . o"

对象/MingW/main.o: main.cpp:(。rdata$_ZTV20wxThreadHelperThread[vtable for wxThreadHelperThread]+0xc):未定义的wxThread::TestDestroy()' Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD1Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16): undefined reference to引用wxThread::~wxThread()'Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD0Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16):对wxThread::~wxThread()' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x10): undefined reference to的未定义引用wxFTP::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 to wxFTP::~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 to wxFTP::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 to wxFTP::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 to wxStringBase::operator=(wxStringBase const&)'. ./libDaFTP/输出/MingW libDaFTP.a (DaFTPLib.o): DaFTPLib.cpp:(。text$_ZN12wxStringBaseC2EPKc[wxStringBase::wxStringBase(char const*)]+0x7):对wxStringBase::npos' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN12wxStringBaseC2EPKc[wxStringBase::wxStringBase(char const*)]+0x25): undefined reference to的未定义引用wxStringBase::InitWith(char const*, unsigned int, unsigned int)'Collect2: ld返回1退出状态

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

执行终止编译失败。返回2


我很抱歉这篇文章很长,但希望这是足够的信息,有人指出我在正确的方向。我认为问题在于合并wx/protocol/ftp库,但我假设在编译我的库时,任何支持库(如ftp的东西)都会与它一起编译。

但是我的假设是,当编译我的库时,任何支持的库,如ftp的东西都会与它一起编译。

你所说的只对动态链接库是正确的:如果你建立一个引用其他库的静态库,链接到你的静态库的应用程序或DLL仍然必须链接到其他库。静态库只不过是编译后的目标代码的集合。库代码中引用的外部函数有而不是链接。

将您的helper库设置为DLL并将其链接到wx库,或者将其保持静态并将使用您的静态库的每个应用程序链接到wx库。

相关文章: