开发者

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

开发者 https://www.devze.com 2023-02-18 05:11 出处:网络
The function ntohl gives a linker error for me: error LNK2001: unresolved external symbol _ntohl@4 fatal error LNK1120: 1 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>
开发者_如何学C

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


You need to link with Ws2_32.lib

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消