如何解决:错误LNK2001:未解析的外部符号" public:static void * [英] How to resolve: error LNK2001: unresolved external symbol "public: static void *
问题描述
Hi All,
我正在尝试使用WinInet API创建一个C ++ DLL,但由于一些奇怪的原因,我在编译期间没有出现任何错误,但是在我的类中获得有关变量的外部链接错误。我知道如何解决这个问题吗?
----------------------------- -
test.h --- ---------------------------
// #include" stdafx。 h"
using namespace std;
// #include< Winineti.h>
#include< windows.h>
#include< strsafe.h>
#include< wininet.h>
#include< math.h>
#include< cstring>
#include< WinInet.h>
// #include< string>
#pragma comment(lib," wininet.lib")
#pragma comment(lib," user32.lib")
#define FTP_FUNCTIONS_BUFFER_SIZE MAX_PATH + 8
命名空间Win32_FTP
{
类FTP_Win32_ClientV1
{
private:
public:
静态LPCWSTR sFileSearch;
static char * sIpAddress;
静态LPCWSTR sUsername;
静态LPCWSTR sPassword;
static string sRemoteDir;
static string sLocalDir;
static string sFilename;
static DWORD dwError;
static HINTERNET hFTPConnect;
静态HINTERNET hFind;
static HINTERNET hConnect;
static HANDLE hFile;
静态WIN32_FIND_DATA FindFileData;
static string strTemp;
static __declspec(dllexport)void Test1();
static __declspec(dllexport)void FtpCloseConnection();
};
}
------------------------------
test.cpp ----------------------- -------
// using namespace std;
#include< iostream>
#include" test.h"
// using namespace std;
命名空间Win32_FTP
{
void FTP_Win32_Client :: Test1()
{
cout<<"这是从Cplus到Csharp DLL的测试";
}
void FTP_Win32_Client:
{
InternetCloseHandle(
InternetCloseHandle(
InternetCloseHandle(
}
}
$
********************* ****** LINK ERRORS ************************
Test.obj:错误LNK2001:未解析的外部符号" public:static void * Win32_FTP:
Test.obj:错误LNK2001:未解析的外部符号" public:static void * Win32_FTP:
Test.obj:错误LNK2001:未解析的外部符号" public:static void * Win32_FTP:
<块标记>
您好,
在头文件中您只是"声明"静态变量,而在源文件中您必须"定义"它们。
声明意味着编译器知道有一个这个名称和类型的变量。
定义意味着为变量保留了内存。
在源文件中,没有定义静态类变量。您必须添加definit离子:
test.cpp
命名空间Win32_FTP
{
LPCWSTR FTP_Win32_ClientV1 :: sFileSearch =。 ..;
char * FTP_Win32_ClientV1 :: sIpAddress = ...;
LPCWSTR FTP_Win32_ClientV1 :: sUsername = ...;
LPCWSTR FTP_Win32_ClientV1 :: sPassword = ...;
string FTP_Win32_ClientV1 :: sRemoteDir = ...;
string FTP_Win32_ClientV1 :: sLocalDir = ...;
string FTP_Win32_ClientV1 :: sFilename = ...;
DWORD FTP_Win32_ClientV1 :: dwError = ...;
HINTERNET FTP_Win32_ClientV1 :: hFTPConnect = ...;
HINTERNET FTP_Win32_ClientV1 :: hFind = ...;
HINTERNET FTP_Win32_ClientV1 :: hConnect = ...;
HANDLE FTP_Win32_ClientV1 :: hFile = ...;
WIN32_FIND_DATA FTP_Win32_ClientV1 :: FindFileData = ...;
string FTP_Win32_ClientV1 :: strTemp = ...;
而不是"..."你应该给所有变量一个第一个值。
问候,Guido
编辑:阅读:https://en.cppreference.com/w/cpp/language/static
Hi All,
I'm trying to create a C++ DLL using the WinInet API, but for some odd reason, I get no errors during compiling but get external link errors regarding variables within my class. Any idea how I can resolve this?
------------------------------
// #include "stdafx.h"
using namespace std;
// #include <Winineti.h>
#include <windows.h>
#include <strsafe.h>
#include <wininet.h>
#include <math.h>
#include <cstring>
#include <WinInet.h>
//#include <string>
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "user32.lib")
#define FTP_FUNCTIONS_BUFFER_SIZE MAX_PATH+8
namespace Win32_FTP
{
class FTP_Win32_ClientV1
{
private:
public:
static LPCWSTR sFileSearch;
static char * sIpAddress;
static LPCWSTR sUsername;
static LPCWSTR sPassword;
static string sRemoteDir;
static string sLocalDir;
static string sFilename;
static DWORD dwError;
static HINTERNET hFTPConnect;
static HINTERNET hFind;
static HINTERNET hConnect;
static HANDLE hFile;
static WIN32_FIND_DATA FindFileData;
static string strTemp;
static __declspec(dllexport) void Test1();
static __declspec(dllexport) void FtpCloseConnection();
};
}
------------------------------
//using namespace std;
#include <iostream>
#include "test.h"
// using namespace std;
namespace Win32_FTP
{
void FTP_Win32_Client::Test1()
{
cout << "This is a test from Cplus to Csharp DLL";
}
void FTP_Win32_Client:
{
InternetCloseHandle(
InternetCloseHandle(
InternetCloseHandle(
}
}
***************************LINK ERRORS ************************
Test.obj : error LNK2001: unresolved external symbol "public: static void * Win32_FTP:
Test.obj : error LNK2001: unresolved external symbol "public: static void * Win32_FTP:
Test.obj : error LNK2001: unresolved external symbol "public: static void * Win32_FTP:
Hello,
in the header file you only "declare" the static variables and in the source file you must "define" them.
Declaration means that the compiler knows there is a variable of this name and type.
Definition means that memory is reserved for the variable.
In your source file, no static class variable is defined. You must add the definitions:
test.cpp
namespace Win32_FTP { LPCWSTR FTP_Win32_ClientV1::sFileSearch = ...; char * FTP_Win32_ClientV1::sIpAddress = ...; LPCWSTR FTP_Win32_ClientV1::sUsername = ...; LPCWSTR FTP_Win32_ClientV1::sPassword = ...; string FTP_Win32_ClientV1::sRemoteDir = ...; string FTP_Win32_ClientV1::sLocalDir = ...; string FTP_Win32_ClientV1::sFilename = ...; DWORD FTP_Win32_ClientV1::dwError = ...; HINTERNET FTP_Win32_ClientV1::hFTPConnect = ...; HINTERNET FTP_Win32_ClientV1::hFind = ...; HINTERNET FTP_Win32_ClientV1::hConnect = ...; HANDLE FTP_Win32_ClientV1::hFile = ...; WIN32_FIND_DATA FTP_Win32_ClientV1::FindFileData = ...; string FTP_Win32_ClientV1::strTemp = ...;Instead of "…" you should give all variables a first value.
Regards, Guido
Edit: Read this: https://en.cppreference.com/w/cpp/language/static
这篇关于如何解决:错误LNK2001:未解析的外部符号" public:static void *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!