Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C function **ntohl** gives linker error (unresolved externals)

The function ntohl gives a linker error for me:

error LNK2001: unresolved external symbol _ntohl@4
fatal error LNK1120: 1 unresolved externals

I have included

#include <winsock.h>

Is there a specific reason for that? (or can this function be easyly done manually?)

like image 305
Alex Kruger Avatar asked Dec 09 '22 10:12

Alex Kruger


2 Answers

You need to link with Ws2_32.lib

See the MSDN documentation for ntohl, which says "Library: Ws2_32.lib".

like image 129
RichieHindle Avatar answered Dec 20 '22 08:12

RichieHindle


I was also running into similar issues, and I was looking through project settings, linker settings, etc trying to figure out how to reference Ws2_32.lib and wondering why a Windows library wasn't linked through default settings in Visual studio.

I finally ran across this Windows article https://learn.microsoft.com/en-us/windows/win32/winsock/creating-a-basic-winsock-application and found that I could fix things through a pragma statement

#pragma comment(lib, "Ws2_32.lib")

Adding this to my header fixed the linking issue.

like image 29
TJ Rockefeller Avatar answered Dec 20 '22 06:12

TJ Rockefeller