C语言 C - 对 WSAStartup@8' 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34384803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - Undefined Reference to WSAStartup@8'
提问by Paulo
I am using Code::Blocks, MinGW, and Windows. Im trying to initialize the winsock so that I can work on a project. I keep getting the error Undefined Reference to WSAStartup@8Anyone know how to go about fixing this?
我正在使用 Code::Blocks、MinGW 和 Windows。我正在尝试初始化 winsock,以便我可以处理一个项目。我不断收到错误Undefined Reference to WSAStartup@8有人知道如何解决这个问题吗?
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc , char *argv[]){
WSADATA wsa;
int output;
output=WSAStartup(MAKEWORD(2,2),&wsa);
if(output != 0) {
printf("Startup failed %d\n", output);
return 1;
} else {
printf("Initialized");
return 0;
}
}
回答by 4pie0
Linker looks for dependencies after the code was loaded. If library appeared in the building process before the symbols were needed, because source files appeared after that, then no symbols were used and later when they appear in source files they will be unresolved. Place the winsock library -lws2_32that you link with AFTER the source and object files.
链接器在代码加载后查找依赖项。如果库在需要符号之前出现在构建过程中,因为源文件在此之后出现,那么没有符号被使用,之后当它们出现在源文件中时,它们将无法解析。将-lws2_32您链接的 winsock 库放在源文件和目标文件之后。
gcc prog.c -o prog -lws2_32
回答by Kien.VietNam
I made another way, I find the library that contain funtion that compiler can't link to, then I add to linker of compiler. and almost of librarys are in the lib folder of MINGW (often be C:/MinGW/lib); like thisThese are libraries I add when I got some errors with DlibOr you can do this instruction for auto regconite missing lib. Building a wxWidgets program in Code::Blocks
我做了另一种方式,我找到包含编译器无法链接的功能的库,然后我添加到编译器的链接器。并且几乎所有的库都在 MINGW 的 lib 文件夹中(通常是 C:/MinGW/lib);像这样这些是我在 Dlib 遇到一些错误时添加的库,或者您可以执行此指令来自动 regconite 丢失的库。在 Code::Blocks 中构建 wxWidgets 程序
回答by Runker Hamming
You may should check your compiler options, add -lws2_32to add linker options when linking.
I use TDM-GCC, works well after that.
您可能应该检查您的compiler options,添加-lws2_32到add linker options when linking. 我使用 TDM-GCC,之后效果很好。
回答by Jay
2019 update for those using Codeblocks on windows:
针对在 Windows 上使用代码块的用户的 2019 年更新:
First, on the menubar, click settings, then compiler, then switch to the tab that says "linker settings". From here, click add, open the file explorer, go to C:/ directory, and type "ws2_32" into the search bar. One file should show up: "libws2_32.a". Add this file by clicking open, then ok, (the file should now appear in the box on the left) and then ok again. Now remove the #PRAGMA line from your code and try to compile, and things should work just fine.
首先,在菜单栏上,单击设置,然后单击编译器,然后切换到显示“链接器设置”的选项卡。从这里,单击添加,打开文件资源管理器,转到 C:/ 目录,然后在搜索栏中键入“ws2_32”。应该显示一个文件:“libws2_32.a”。通过单击打开添加此文件,然后单击确定(该文件现在应该出现在左侧的框中),然后再次确定。现在从您的代码中删除 #PRAGMA 行并尝试编译,事情应该可以正常工作。
回答by Keith Marshall
Your source code shows that you use the very specific, (to Microsoft's compiler), #pragma comment(lib,"ws2_32.lib")statement. There are two problems with this:
您的源代码显示您使用了非常具体的(针对 Microsoft 的编译器)#pragma comment(lib,"ws2_32.lib")语句。这有两个问题:
- This pragma isn't valid in GCC, (i.e. MinGW compilers), so it is simply ignored, both at compile and link time.
- In MinGW, (in common with GCC convention on most (maybe all?) other platforms), there is no "ws2_32.lib"; the correct name for the library, (which is an import library for ws2_32.dll), is "libws2_32.a".
- 此编译指示在 GCC(即 MinGW 编译器)中无效,因此在编译和链接时都会被忽略。
- 在 MinGW 中(与大多数(也许所有?)其他平台上的 GCC 约定一样),没有“ws2_32.lib”;库的正确名称(它是 ws2_32.dll 的导入库)是“libws2_32.a”。
To resolve your issue, you must not rely on MSVC specific pragmas, in your source code; rather, you must specify the library correctly on the linking command line, (almost) as tinky_winky shows:
要解决您的问题,您不得依赖源代码中特定于 MSVC 的编译指示;相反,您必须在链接命令行上正确指定库,(几乎)如 tinky_winky 所示:
gcc prog.c -o prog.exe [...other .c and .o files...] -lws2_32 ...
(and ensure that any libraries you specify come afterthe object files, or source files, which require them).
(并确保您指定的任何库都在需要它们的目标文件或源文件之后)。

