Trying to compile a sample http class with the SDK, and getting some strange link errors... I am sure its something to do with a missing option, or directory...
I am no expert in c++ as you can see, but looking for any assistance.
I included my sample class. I also did install the Windows SDK. If you need any other information about my setups or anything, please ask. I'd prefer someone point me to a working WinHttp SDK sample project.
//START OF utils.cpp
#pragma once
#include "stdafx.h"
class http
{
public:
http();
~http();
std::string getText();
};
//END OF utils.cpp
//START OF utils.cpp
#include "stdafx.h"
#include "utils.h"
http::http()
{
}
http::~http()
{
}
std::string http::getText()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, L"www.microsoft.com",
INTERNET_DEFAULT_HTTPS_PORT, 0 );
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE );
// Send a request.
if( hRequest )
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0 );
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest, NULL );
// Keep checking for data until there is nothing left.
if( bResults )
{
do
{
// Check for available data.
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
printf( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError( ) );
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
{
printf( "Out of memory\n" );
dwSize=0;
}
else
{
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else
printf( "%s", pszOutBuffer );
// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
} while( dwSize > 0 );
}
// Report any errors.
if( !bResults )
printf( "Error %d has occurred.\n", GetLastError( ) );
// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
return "";
}
//END OF utils.cpp
1>------ Build started: Project: winagent, Configuration: Debug Win32 ------
1>Compiling...
1>utils.cpp
1>Linking...
1> Creating library C:\winagent\Debug\winagent.lib and object C:\winagent\Debug\winagent.exp
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpCloseHandle#4 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpReadData#16 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpQueryDataAvailable#8 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpReceiveResponse#8 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpSendRequest#28 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpenRequest#28 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpConnect#16 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpen#20 referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall http::getText(void)" (?getText#http##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ)
1>C:\winagent\Debug\winagent.exe : fatal error LNK1120: 8 unresolved externals
1>Build log was saved at "file://c:\winagent\Debug\BuildLog.htm"
1>winagent - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you check the MSDN reference for the WinHttp* functions you will see that you need to link with the library Winhttp.lib.
Open the project settings, select the linker options then 'input' and add WinHttp.lib to the 'Additional Dependencies' list.
Or you could put
#pragma comment(lib, "winhttp.lib")
(as previously mentioned) in your source code.
You need to link to winhttp.lib
Change the project settings or add this line to your .cpp file
#pragma comment(lib, "winhttp")
You've not added the WinHttp library to your link list.
Make sure you are linking with Winhttp.lib.
Related
Iam modding in the source Engine and iam trying to add adobe Flash into it
i have already included header files and the libs from Microsoft DirectX SDK (June 2010) in visual studio 2010 but when Compiling the project it give me errors
1>FlashManager.obj : error LNK2019: unresolved external symbol _D3DXCreateSprite#8 referenced in function "public: bool __thiscall FlashManager::RecreateTargets(struct IDirect3DDevice9 *)" (?RecreateTargets#FlashManager##QAE_NPAUIDirect3DDevice9###Z)
1>FlashManager.obj : error LNK2019: unresolved external symbol _D3DXMatrixTransformation2D#28 referenced in function "public: void __thiscall FlashManager::SceneDataHook(void)" (?SceneDataHook#FlashManager##QAEXXZ)
1>FlashManager.obj : error LNK2019: unresolved external symbol "struct IFlashDX * __cdecl GetFlashToDirectXInstance(void)" (?GetFlashToDirectXInstance##YAPAUIFlashDX##XZ) referenced in function "public: __thiscall FlashManager::FlashManager(void)" (??0FlashManager##QAE#XZ)
1>.\Release_sdk\Client.dll : fatal error LNK1120: 3 unresolved externals
How Can i fix it
linked every thing
The code
bool FlashManager::RecreateTargets(IDirect3DDevice9* pD3DDevice)
{
HRESULT hr;
int movieIndex = GetPlayingMovieIndex();
int newWidth = -1, newHeight = -1;
if (movieIndex > -1)
{
newWidth = m_movieArray[movieIndex].GetWidth();
newHeight = m_movieArray[movieIndex].GetHeight();
}
else
{
newWidth = w;
newHeight = h;
}
hr = pD3DDevice->CreateTexture(newWidth, newHeight, 1, 0, transparency_mode ? D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &g_Texture, NULL);
if (FAILED(hr))
return false;
D3DXCreateSprite(pD3DDevice, &Sprite); //the problem
g_pD3DDevice = pD3DDevice;
if (m_flashPlayer)
m_flashPlayer->ResizePlayer(newWidth, newHeight);
for (int i = 0; i < num_textures_in_rotation; ++i)
{
hr = pD3DDevice->CreateTexture(newWidth, newHeight, 1, 0,
transparency_mode ? D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_texturesRotation[i], NULL);
if (FAILED(hr))
return false;
}
recreatedTargets = true;
return true;
}
Link error 2
D3DXMatrixTransformation2D(&Mat, NULL, 0, &Scaling, NULL, 0, &Translation); // The problem
The Last link error
FlashManager::FlashManager()
{
engine->GetScreenSize(w, h);
// Flash init
m_flashDX = GetFlashToDirectXInstance(); //the problem
m_flashPlayer = m_flashDX->CreatePlayer(w, h);
if (!m_flashPlayer)
{
MessageBox(NULL, "Flash Player failed to initialize.", "Error", MB_OK);
abort();
}
m_playerASI = new ASInterface(m_flashPlayer);
I found this email program and I can't get it to run. I am using visual studio 2013 c++. I would take any other programs too if you know of any. All I need is to be able to put this in my existing code to email a number to a gmail account. My error is:
error:
1>------ Build started: Project: sending email, Configuration: Debug Win32 ------
1>cl : Command line warning D9007: '/Gm' requires '/Zi or /ZI'; option ignored
1> Source.cpp
1>c:\users\kyle\documents\visual studio 2013\projects\sending email\sending email\source.cpp(127): warning C4715: 'MailIt' : not all control paths return a value
1>Source.obj : error LNK2019: unresolved external symbol _closesocket#4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _connect#12 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _htons#4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _recv#16 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _send#16 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _socket#12 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _gethostbyname#4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _WSAStartup#8 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>Source.obj : error LNK2019: unresolved external symbol _WSACleanup#0 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt##YAHPAD0000#Z)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\kyle\documents\visual studio 2013\Projects\sending email\Debug\sending email.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
code:
#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>
/*If you don't know the mail exchange server for an address for the following
"nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for
whatever email address you want. YOU MUST CHANGE THESE SETTINGS OR
IT WILL NOT WORK!!! */
#define BUFSIZE 800
#define waittime 500
#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "kmtompkinscode#gmail.com"
#define cemailfrom "kmtompkinscode#gmail.com"
#define LogLength 100
#define SMTPLog "smtp.log"
#define cemailsubject "pin"
int MailIt(char *mailserver, char *emailto, char *emailfrom, char *emailsubject,
char *emailmessage)
{
SOCKET sockfd;
WSADATA wsaData;
FILE *smtpfile;
#define bufsize 300
int bytes_sent; /* Sock FD */
int err;
struct hostent *host; /* info from gethostbyname */
struct sockaddr_in dest_addr; /* Host Address */
char line[1000];
char *Rec_Buf = (char*)malloc(bufsize + 1);
smtpfile = fopen(SMTPLog, "a+");
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
fputs("WSAStartup failed", smtpfile);
WSACleanup();
return -1;
}
if ((host = gethostbyname(mailserver)) == NULL) {
perror("gethostbyname");
exit(1);
}
memset(&dest_addr, 0, sizeof(dest_addr));
memcpy(&(dest_addr.sin_addr), host->h_addr, host->h_length);
/* Prepare dest_addr */
dest_addr.sin_family = host->h_addrtype; /* AF_INET from gethostbyname */
dest_addr.sin_port = htons(25); /* PORT defined above */
/* Get socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
/* Connect !*/
fputs("Connecting....\n", smtpfile);
if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1){
perror("connect");
exit(1);
}
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "helo me.somepalace.com\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "MAIL FROM:<");
strncat(line, emailfrom, strlen(emailfrom));
strncat(line, ">\n", 3);
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "RCPT TO:<");
strncat(line, emailto, strlen(emailto));
strncat(line, ">\n", 3);
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "DATA\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
Sleep(waittime);
strcpy(line, "To:");
strcat(line, emailto);
strcat(line, "\n");
strcat(line, "From:");
strcat(line, emailfrom);
strcat(line, "\n");
strcat(line, "Subject:");
strcat(line, emailsubject);
strcat(line, "\n");
strcat(line, emailmessage);
strcat(line, "\r\n.\r\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
strcpy(line, "quit\n");
fputs(line, smtpfile);
bytes_sent = send(sockfd, line, strlen(line), 0);
Sleep(waittime);
err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
fputs(Rec_Buf, smtpfile);
fclose(smtpfile);
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
}
I guess the problem is that in the project setting->C/C++->General Treat Warnings as Errors was set to "Yes".
To solve it you can set it to "No", And add in project setting->C/C++->Preprocessor in Preprocessor definitions the _CRT_SECURE_NO_WARNINGS
EDIT
this link can also help:
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
Here is my code
#include <iostream>
#include <Windows.h>
#include <WinInet.h>
using namespace std;
int main()
{
HINTERNET hSession, hURL;
char* Buffer = new char[1024];
DWORD BufferLen, BytesWritten;
HANDLE FileHandle;
hSession = InternetOpenA(NULL, 0, NULL, NULL, 0);
hURL = InternetOpenUrlA(hSession, "http://www.google.co.uk", NULL, 0, 0, 0);
FileHandle = CreateFileA("C:\\temp.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
BytesWritten = 0;
do
{
InternetReadFile(hURL, Buffer, 1024, &BufferLen);
WriteFile(FileHandle, Buffer, BufferLen, &BytesWritten, NULL);
} while (BufferLen != 0);
CloseHandle(FileHandle);
InternetCloseHandle(hURL);
InternetCloseHandle(hSession);
ShellExecuteA(0, "open", "C:\\temp.txt", NULL, NULL, 1);
cout << "Operation complete!";
system("PAUSE");
return 0;
}
Here are the errors I am encountering
error LNK2019: unresolved external symbol __imp__InternetOpenA#20 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetCloseHandle#4 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetOpenUrlA#24 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetReadFile#16 referenced in function _main
I don't understand where I am going wrong. I have imported Wininet.h and Windows.h. Why is it still having trouble locating these functions? Regards.
What you have done is include the header file, but not linked to the actual code which implements it.
Include Wininet.lib in your project.
Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies
First of all, I know this question is all over this site but I have looked at almost all of them and can't seem to find out what is wrong. This is in VS 2012. Thanks.
//Socket.h
#pragma once
#include <iostream>
#include <WinSock2.h>
using namespace std;
const int STRLEN = 256;
class Socket
{
protected:
WSADATA wsaData;
SOCKET mySocket;
SOCKET myBackup;
SOCKET acceptSocket;
sockaddr_in myAddress;
public:
Socket();
~Socket();
bool SendData( char* );
bool RecvData( char*, int );
void CloseConnection();
void GetAndSendMessage();
};
class ServerSocket : public Socket
{
public:
void Listen();
void Bind( int port );
void StartHosting( int port );
};
class ClientSocket : public Socket
{
public:
void ConnectToServer( const char *ipAddress, int port );
};
Here's Socket.cpp
//Socket.cpp
#include "stdafx.h"
#include "Socket.h"
Socket::Socket()
{
if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
{
cerr<<"Socket Initialization: Error with WSAStartup\n";
system("pause");
WSACleanup();
exit(10);
}
//Create a socket
mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( mySocket == INVALID_SOCKET )
{
cerr<<"Socket Initialization: Error creating socket"<<endl;
system("pause");
WSACleanup();
exit(11);
}
myBackup = mySocket;
}
Socket::~Socket()
{
WSACleanup();
}
bool Socket::SendData( char *buffer )
{
send( mySocket, buffer, strlen( buffer ), 0 );
return true;
}
bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );
buffer[i] = '\0';
return true;
}
void Socket::CloseConnection()
{
//cout<<"CLOSE CONNECTION"<<endl;
closesocket( mySocket );
mySocket = myBackup;
}
void Socket::GetAndSendMessage()
{
char message[STRLEN];
cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
cout<<"Send > ";
cin.get( message, STRLEN );
SendData( message );
}
void ServerSocket::StartHosting( int port )
{
Bind( port );
Listen();
}
void ServerSocket::Listen()
{
//cout<<"LISTEN FOR CLIENT..."<<endl;
if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
{
cerr<<"ServerSocket: Error listening on socket\n";
system("pause");
WSACleanup();
exit(15);
}
//cout<<"ACCEPT CONNECTION..."<<endl;
acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;
}
void ServerSocket::Bind( int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
myAddress.sin_port = htons( port );
//cout<<"BIND TO PORT "<<port<<endl;
if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
{
cerr<<"ServerSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(14);
}
}
void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( ipAddress );
myAddress.sin_port = htons( port );
//cout<<"CONNECTED"<<endl;
if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
{
cerr<<"ClientSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(13);
}
}
And here's stdafx.h
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include "Socket.h"
And here are my error messages:
1>------ Build started: Project: Client, Configuration: Debug Win32 ------
1> stdafx.cpp
1> Socket.cpp
1> Client.cpp
1> Generating Code...
1>Socket.obj : error LNK2019: unresolved external symbol __imp__accept#12 referenced in function "public: void __thiscall ServerSocket::Listen(void)" (?Listen#ServerSocket##QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__bind#12 referenced in function "public: void __thiscall ServerSocket::Bind(int)" (?Bind#ServerSocket##QAEXH#Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__closesocket#4 referenced in function "public: void __thiscall Socket::CloseConnection(void)" (?CloseConnection#Socket##QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__connect#12 referenced in function "public: void __thiscall ClientSocket::ConnectToServer(char const *,int)" (?ConnectToServer#ClientSocket##QAEXPBDH#Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__htons#4 referenced in function "public: void __thiscall ServerSocket::Bind(int)" (?Bind#ServerSocket##QAEXH#Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__inet_addr#4 referenced in function "public: void __thiscall ServerSocket::Bind(int)" (?Bind#ServerSocket##QAEXH#Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__listen#8 referenced in function "public: void __thiscall ServerSocket::Listen(void)" (?Listen#ServerSocket##QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__recv#16 referenced in function "public: bool __thiscall Socket::RecvData(char *,int)" (?RecvData#Socket##QAE_NPADH#Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__send#16 referenced in function "public: bool __thiscall Socket::SendData(char *)" (?SendData#Socket##QAE_NPAD#Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__socket#12 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket##QAE#XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__WSAStartup#8 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket##QAE#XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__WSACleanup#0 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket##QAE#XZ)
1>C:\Users\ajayp_000\documents\visual studio 2012\Projects\Client\Debug\Client.exe : fatal error LNK1120: 12 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The problem is you are not linking against the Ws2_32.lib library. To fix this you can add that to your additional dependencies tab of linker/Input settings for your project. Alternatively (as pointed out by SChepurin in the comments) you can add
#pragma comment(lib, "Ws2_32.lib")
to a source file of your project.
Another way is that click right in your project in visual studio and go to following path and add "Ws2_32.lib" in that.
Linker>Input> Additional Dependencies
Add runtimeobject.lib to the linker Additional Dependencies (in the project property page, under Linker->Input).
Even though this is a linker error. the problem can also occur if you set wrong "Runtime Library" option in
Properties > Configuration properties > C/C++ > Code Generation > Runtime Library
both called binary and calling binary should have the same values
if the property in the calling binary is MTd, then it should be MTd
in called binary.
if the property in the calling binary is MDd, then it should be MDd in called binary.
I've installed Windows 8 64bits and Visual Sudio 2012.
Later, in class, the teacher gave us a .cpp file to try a udp server on our computer.
It should be just run and use but, in my case, VS is giving me some unresolved externals
#include <stdio.h>
#include <winsock.h>
#define GET_TIME "TIME\r\n"
#define MAX_MSG_SIZE 100
void processClient(LPVOID param);
void main(int argc, char **argv)
{
SOCKET s, cliSocket;
WSADATA wsaData;
int iResult, len;
struct sockaddr_in serv_addr, cli_addr;
SECURITY_ATTRIBUTES sa;
DWORD thread_id;
if(argc != 2){
printf("Usage: %s <time_server_port>\n", argv[0]);
getchar();
exit(1);
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
getchar();
exit(1);
}
if((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR){
printf("Unable to create socket (error: %d)!\n", WSAGetLastError());
getchar();
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==SOCKET_ERROR){
printf("Unable to bind socket to port %s (error: %d)\n", argv[1], WSAGetLastError());
closesocket(s);
getchar();
exit(1);
}
listen(s, 5);
while(1){
printf("Waiting for a time request...\n");
len = sizeof(cli_addr);
if((cliSocket = accept(s, (struct sockaddr *)&cli_addr, &len))==SOCKET_ERROR){
printf("Unable to accept new connection (error: %d)\n", WSAGetLastError());
closesocket(s);
getchar();
exit(1);
}
printf("Connection from %s:%d accepted\n", inet_ntoa(cli_addr.sin_addr),
ntohs(cli_addr.sin_port));
sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=NULL;
if(CreateThread(&sa,0 ,(LPTHREAD_START_ROUTINE)processClient, (LPVOID)cliSocket, (DWORD)0, &thread_id)==NULL){
printf("Cannot start new slave thread (error: %d)\n", GetLastError());
closesocket(cliSocket);
}
printf("New slave thread started (id: %d)\n", thread_id);
}
}
void processClient(LPVOID param)
{
SOCKET s;
int nbytes, len;
struct sockaddr_in cli_addr;
char request[MAX_MSG_SIZE], response[MAX_MSG_SIZE];
SYSTEMTIME systemTime;
s = (SOCKET)param;
if((nbytes = recv(s, request, MAX_MSG_SIZE, 0)) == SOCKET_ERROR){
printf("Unable to receive request (error: %d)\n", WSAGetLastError());
closesocket(s);
return;
}
request[nbytes] = 0;
if(strcmp(GET_TIME, request)!=0){
printf("Unexpected request \"%s\" will be ignored\n", request);
closesocket(s);
return;
}
GetSystemTime(&systemTime);
sprintf(response, "%d:%d:%d", systemTime.wHour,
systemTime.wMinute, systemTime.wSecond);
if(send(s, response, strlen(response), 0) == SOCKET_ERROR){
printf("Unable to send reponse (error: %d)\n", WSAGetLastError());
closesocket(s);
return;
}
len = sizeof(cli_addr);
getpeername(s, (struct sockaddr *)&cli_addr, &len);
printf("\"%s\" sent to %s:%d\n", response, inet_ntoa(cli_addr.sin_addr),
ntohs(cli_addr.sin_port));
closesocket(s);
}
These are the errors:
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _accept#12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _bind#12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _closesocket#4 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _getpeername#12 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _htonl#4 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _htons#4 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _inet_ntoa#4 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _listen#8 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _ntohs#4 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _recv#16 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _send#16 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _socket#12 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _WSAStartup#8 referenced in function _main
1>TCPConcurrentServer.obj : error LNK2019: unresolved external symbol _WSAGetLastError#0 referenced in function "void __cdecl processClient(void *)" (?processClient##YAXPAX#Z)
1>c:\users\brunoc\documents\visual studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 14 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Does someone knows how to solve this?
Thanks
Open the Project properties, go to Linker -> Input, and the add the ws2_32.lib to Additional Dependencies edit line.