Getting a IP Address

closed account (Gy7oizwU)
Hi all

Im getting an error when compiling my project that gets the IP address of a host. Please see if you can spot the issue?

Code:

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace System;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{  
	const char* const host = "www.google.com" ;  
	const hostent* host_info = 0 ;   
	
	for( int attempt=0 ; (
		host_info==0) && (attempt<3) ; ++attempt )    
		host_info = gethostbyname(host) ;   
	
	if(host_info)  
	{    
		std::cout << "host: " << host_info->h_name << '\n' ;
 		
		for( int i=0 ; host_info->h_addr_list[i] ; ++i )    
		{      
			const in_addr* address = (in_addr*)host_info->h_addr_list[i] ;
			std::cout << " address: " << inet_ntoa( *address ) << '\n' ;
		}  
	}  
}


Error:

1>Log Access.obj : error LNK2028: unresolved token (0A00029C) "extern "C" char * __stdcall inet_ntoa(struct in_addr)" (?inet_ntoa@@$$J14YGPADUin_addr@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>Log Access.obj : error LNK2028: unresolved token (0A00029E) "extern "C" struct hostent * __stdcall gethostbyname(char const *)" (?gethostbyname@@$$J14YGPAUhostent@@PBD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

1>Log Access.obj : error LNK2019: unresolved external symbol "extern "C" char * __stdcall inet_ntoa(struct in_addr)" (?inet_ntoa@@$$J14YGPADUin_addr@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

1>Log Access.obj : error LNK2019: unresolved external symbol "extern "C" struct hostent * __stdcall gethostbyname(char const *)" (?gethostbyname@@$$J14YGPAUhostent@@PBD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

1>C:\Tools\Log Access\Debug\Log Access.exe : fatal error LNK1120: 4 unresolved externals
Are you linking with ws2_32.lib?
closed account (Gy7oizwU)
Do i add that in additional library directories in the general linker settings for the project?
If using Visual Studio, Linker->Input Additional Dependencies. Remember to set it for Debug and Release.
closed account (Gy7oizwU)
Works. Thanks!
Topic archived. No new replies allowed.