如何通过g++链接wsock32库?

发布于 2024-12-28 20:53:32 字数 350 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2025-01-04 20:53:32

正如 @Joshua 评论的那样,您可能需要 ws2_32.dll

GNU 编译器集合使用 ranlib 存档(A 文件)而不是 Visual Studio LIB 文件。

w32headers 项目为大多数标准 Windows DLL(包括 ws2_32.dll>)提供与 gccg++ 兼容的标头和存档。存档的名称通常是 DLL 的名称减去 .dll 扩展名,前缀为 lib,后缀为 .a(遵循*nix 存档命名约定)。因此,ws2_32.dll 的符号位于 libws2_32.a 中,可以使用 -lws2_32 进行链接。

默认情况下,w32headers 存档位于 GNU ld 库路径中,因此为了能够与标准 Windows DLL 链接,无需使用 -L 添加库目录> 选项。在您的情况下,您需要的唯一选项是 ‑lws2_32

g++ C:\Program.cpp -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- and g++-compatible headers and archives for most standard Windows DLLs, including ws2_32.dll. The name of the archive is usually the name of the DLL minus the .dll extension, prefixed with lib and suffixed with .a (following the *nix archive naming convention). Thus, the symbols for ws2_32.dll are in libws2_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:

g++ C:\Program.cpp -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.

深府石板幽径 2025-01-04 20:53:32

-L 选项用于设置链接器应在其中查找库/dll 的目录。

-l 选项用于命名要链接的库/dll。

这意味着

g++ C:\Program.cpp -LC:\Windows\System32 -lwsock32

应该是从常规 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

g++ C:\Program.cpp -LC:\Windows\System32 -lwsock32

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文