如何通过g++链接wsock32库?
我在 Windows 上使用 minGW,尝试编译 C++ 程序。我在那里使用了套接字,所以我尝试链接(不包括...我已经包括了winsock.h)wsock32 库。我知道 -L 开关用于链接,但以下命令均不起作用:
g++ C:\program.cpp -Lwsock32.lib
g++ C:\program.cpp -LC:\windows\system32\wsock32.dll
g++ C:\program.cpp -lC:\windows\system32\wsock32.dll
g++ C:\program.cpp -LC:\windows\system32\wsock32.lib
我应该做什么?
I'm using minGW on windows, trying to compile a c++ program. I've used sockets in there, so I'm trying to link (not include... I've already included winsock.h) the wsock32 library. I know that the -L switch is for linking but none of the following commands work:
g++ C:\program.cpp -Lwsock32.lib
g++ C:\program.cpp -LC:\windows\system32\wsock32.dll
g++ C:\program.cpp -lC:\windows\system32\wsock32.dll
g++ C:\program.cpp -LC:\windows\system32\wsock32.lib
what should I do??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @Joshua 评论的那样,您可能需要
ws2_32.dll
。GNU 编译器集合使用 ranlib 存档(A 文件)而不是 Visual Studio LIB 文件。
w32headers 项目为大多数标准 Windows DLL(包括
ws2_32.dll>)提供与
gcc
和g++
兼容的标头和存档。存档的名称通常是 DLL 的名称减去.dll
扩展名,前缀为lib
,后缀为.a
(遵循*nix 存档命名约定)。因此,ws2_32.dll
的符号位于libws2_32.a
中,可以使用-lws2_32
进行链接。默认情况下,w32headers 存档位于 GNU
ld
库路径中,因此为了能够与标准 Windows DLL 链接,无需使用-L
添加库目录> 选项。在您的情况下,您需要的唯一选项是‑lws2_32
:假设编译和链接成功,输出将为当前目录中的
a.exe
。您可以使用-o
选项覆盖输出二进制文件的名称。注意:我在答案中使用了不间断的连字符。如果您复制 &粘贴命令行选项时,请务必将所有看起来像连字符的字符替换为常规连字符。
As @Joshua commented, you probably want
ws2_32.dll
.The GNU Compiler Collection uses ranlib archives (A files) rather than Visual Studio LIB files.
The w32headers project provides
gcc
- andg++
-compatible headers and archives for most standard Windows DLLs, includingws2_32.dll
. The name of the archive is usually the name of the DLL minus the.dll
extension, prefixed withlib
and suffixed with.a
(following the *nix archive naming convention). Thus, the symbols forws2_32.dll
are inlibws2_32.a
, which can be linked with using‑lws2_32
.By default, the w32headers archives are in the GNU
ld
library path, so to be able to link with standard Windows DLLs, there is no need to add library directories with an‑L
option. In your case, the only option that you need is‑lws2_32
:Assuming that compilation and linking succeed, the output will be
a.exe
in the current directory. You can override the name of the output binary with the‑o
option.NOTE: I used non-breaking hyphens in my answer. If you copy & paste the command line options, be sure to replace all hyphen-looking characters with regular hyphens.
-L
选项用于设置链接器应在其中查找库/dll 的目录。-l
选项用于命名要链接的库/dll。这意味着
应该是从常规 Windows 命令提示符编译程序的命令。
我怀疑您的编译器可能会自动查找 system32,因此您可能只想尝试跳过
-L
选项。The
-L
option is for setting the directory where the linker should look for libraries/dlls.The
-l
option is for naming the libraries/dlls you want to link with.That would mean
should be the command to compile your program from your regular windows command prompt.
I suspect your compiler may look in system32 automatically, so you may want to just try to skip the
-L
option.