c++ 的链接器错误引用另一个库的静态库
我对 C++ 很陌生,在调试链接器错误时遇到问题。我正在使用 wxDev-c++ 和 g++ 编译器。我创建了一个静态库,将其链接到一个基本项目。该库最初不包含对外部头文件或库的引用,它只是几个简单的测试函数,将 2 个双精度数相加并返回值。当链接到测试项目时,这工作得很好。然而,我现在正尝试将 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;
}
库的编译日志输出(编译正常):
执行 make... mingw32-make.exe -f“Makefile.win”全部 g++.exe -c DaFTPLib.cpp -o 对象/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:/程序文件 (x86)/Dev-Cpp/include/c++/3.4.5" -I"C:/程序文件(x86)/Dev-Cpp/include" -I"C:/程序文件 (x86)/Dev-Cpp/" -I"C:/程序文件(x86)/Dev-Cpp/include/common/wx/msw" -I"C:/程序文件 (x86)/Dev-Cpp/include/common/wx/generic" -I"C:/程序文件 (x86) )/Dev-Cpp/include/common/wx/html" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/protocol" -I“C:/程序文件(x86)/Dev-Cpp/include/common/wx/xml”-I“C:/程序文件(x86)/Dev-Cpp/include/common/wx/xrc”-I “C:/程序文件(x86)/Dev-Cpp/include/common/wx”-I“C:/程序文件(x86)/Dev-Cpp/include/common”
ar rcu “Output/MingW/libDaFTP.a”“Objects/MingW/DaFTPLib.o”
执行终止 编译成功 编译耗时1秒完成
测试项目的编译日志输出(编译失败)
正在执行make... mingw32-make.exe -f“Makefile.win”全部 g++.exe -c main.cpp -o 对象/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:/程序文件 (x86)/Dev-Cpp/include/c++/3.4.5" -I"C:/程序文件(x86)/Dev-Cpp/include" -I"C:/程序文件 (x86)/Dev-Cpp/" -I"C:/程序文件(x86)/Dev-Cpp/include/common/wx/msw" -I"C:/程序文件 (x86)/Dev-Cpp/include/common/wx/generic" -I"C:/程序文件 (x86) )/Dev-Cpp/include/common/wx/html" -I"C:/Program Files (x86)/Dev-Cpp/include/common/wx/protocol" -I“C:/程序文件(x86)/Dev-Cpp/include/common/wx/xml”-I“C:/程序文件(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 “对象/MingW/main.o”-o“输出/MingW/LibTest.exe”-L“C:/Program Files (x86)/Dev-Cpp/Lib”../libDaFTP/Output/MingW/libDaFTP.a
Objects/MingW/main.o:main.cpp:(.rdata$_ZTV20wxThreadHelperThread[虚表wxThreadHelperThread]+0xc): 对 wxThread::TestDestroy()' 的未定义引用 Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD1Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16): 对 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): 对 wxFTP::wxFTP()' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x3e): 对 wxFTP::wxFTP()' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x6c): 对 wxFTP::~wxFTP()' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x82): 对 wxFTP::~wxFTP()' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x1e3): 未定义引用
wxFTP::Connect(wxString const&)' ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x24e): 对 wxFTP::GetFileSize(wxString const&)' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x43f): 对 wxFTP::wxFTP()' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x45a): 对 wxFTP::~wxFTP()' 的未定义引用 ../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN8wxStringaSERKS_[wxString::operator=(wxString const&)]+0x14):未定义引用
wxStringBase::operator=(wxStringBase const&)' ../libDaFTP/Output/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):对wxStringBase的未定义引用::InitWith(char const*, 无符号整型, 无符号整型)' collect2: ld 返回 1 退出状态
mingw32-make.exe: * [Output/MingW/LibTest.exe] 错误 1
执行终止 编译失败。使返回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.
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()'
wxThread::~wxThread()'
Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD1Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16): undefined reference to
Objects/MingW/main.o:main.cpp:(.text$_ZN20wxThreadHelperThreadD0Ev[wxThreadHelperThread::~wxThreadHelperThread()]+0x16): undefined reference to wxThread::~wxThread()'
wxFTP::wxFTP()'
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x10): undefined reference to
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x3e): undefined reference to wxFTP::wxFTP()'
wxFTP::~wxFTP()'
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x6c): undefined reference to
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x82): undefined reference to wxFTP::~wxFTP()'
wxFTP::Connect(wxString const&)'
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x1e3): undefined reference to
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x24e): undefined reference to wxFTP::GetFileSize(wxString const&)'
wxFTP::wxFTP()'
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x43f): undefined reference to
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text+0x45a): undefined reference to wxFTP::~wxFTP()'
wxStringBase::operator=(wxStringBase const&)'
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN8wxStringaSERKS_[wxString::operator=(wxString const&)]+0x14): undefined reference to
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN12wxStringBaseC2EPKc[wxStringBase::wxStringBase(char const*)]+0x7): undefined reference to wxStringBase::npos'
wxStringBase::InitWith(char const*, unsigned int, unsigned int)'
../libDaFTP/Output/MingW/libDaFTP.a(DaFTPLib.o):DaFTPLib.cpp:(.text$_ZN12wxStringBaseC2EPKc[wxStringBase::wxStringBase(char const*)]+0x25): undefined reference to
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所说的仅适用于动态链接库:如果您构建引用其他库的静态库,则链接到静态库的应用程序或 DLL 仍然必须链接到其他库。静态库只不过是编译后的目标代码的集合。库代码中引用的外部函数未被链接。
要么将你的辅助库设为一个 DLL 并将其链接到 wx 库,要么保持它静态并将每个使用静态库的应用程序链接到wx 库。
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.