【问题标题】:C winsock2.h WS2_32.lib linking undefined refernce toC winsock2.h WS2_32.lib 链接未定义的引用
【发布时间】:2017-09-23 12:32:42
【问题描述】:

我正在尝试用winsock2 编写一个简单的服务器。我没有让它编译正确。

#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "dump.h"

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

#define PORT 7890

int main(void)
{
    //fd --> file descriptor
    int sockfd, new_sockfd; //warten an sockfd, neue Verbindung an new_sockfd
    struct sockaddr_in host_addr, client_addr; //Addressinformationen
    //sockaddr_in aus winsock.h

    //Laenge des Inputs --> winsock Alternative suchen
    socklen_t sin_size;

    int recv_length = 1, yes = 1;
    char buffer[1024];
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        //PF_INET --> Protocol family
        //AF_INET --> Addres family
        printf("%s\n", "in socket");
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(int)) == -1)
        printf("%s\n", "setting socket option SO_REUSEADDR");

    //vorbereiten auf den bind-Befehl
    host_addr.sin_family = AF_INET;
    host_addr.sin_port = htons(PORT);
    //htons --> host to network short
    //Wandelt bei 16 Bit int Hostbytereihenfolge in Netzwerkbytereihenfolge
    host_addr.sin_addr.s_addr = 0; //Automatisch mit meiner IP fuellen
    memset(&(host_addr.sin_zero), '\0', 8); // Rest der Struktur mit 0 fuellen

    if (bind(sockfd, (struct sockaddr *) &host_addr, sizeof(struct sockaddr)) == -1)
        printf("%s\n", "binding to socket");
    if (listen(sockfd, 5) == -1)
    {
        printf("%s\n", "listening on socket");
    }

    //Schleife um am PORT zu lauschen und Verbindungen zu akzeptieren
    while (1)
    {
        sin_size = sizeof(struct sockaddr_in);
        //accept gibt neuen sockfd zurueck !
        new_sockfd =

         accept(sockfd, (struct sockaddr *) &client_addr, &sin_size);
        if (new_sockfd == -1)
        {
            printf("%s\n", "accepting connection");
        }
        printf("server: got connection from %s port %d\n",
               inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        send(new_sockfd, "Hello, world!\n", 13, 0);
        recv_length = recv(new_sockfd, buffer, 1024, 0);
        while (recv_length > 0)
        {
            printf("RECV: %d bytes \n", recv_length);
            dump(buffer, recv_length);
            recv_length = recv(new_sockfd, buffer, 1024, 0);
        }
        closesocket(new_sockfd);
    }
    return 0;
}

这是第一个使用 socket.h 的 Linux,但我尝试在 windows7 下使用 winsock2 实现它。我手动安装了 Windows SDK 并找到了我必须链接的库 Ws2_32.lib。 我在 CLION 中使用 gcc (mingw32) 进行编译。我设置了参数标志

-lws2_32 -lwsock32

我知道这里有很多帖子回答了这个问题,但似乎没有什么对我有用。

CLION 的输出:

"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marcel\Desktop\Projekte\C-Projekte\Sequenz-Uebungen\Simple_Server\cmake-build-debug --target Simple_Server -- -j 2
[ 50%] Linking C executable Simple_Server.exe
CMakeFiles\Simple_Server.dir/objects.a(main.c.obj): In function `main':
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:49: undefined reference to `socket@12'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:53: undefined reference to `setsockopt@20'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:58: undefined reference to `htons@4'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:64: undefined reference to `bind@12'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:66: undefined reference to `listen@8'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:78: undefined reference to `accept@12'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:84: undefined reference to `ntohs@4'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:83: undefined reference to `inet_ntoa@4'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:85: undefined reference to `send@16'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:86: undefined reference to `recv@16'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:91: undefined reference to `recv@16'
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:93: undefined reference to `closesocket@4'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\Simple_Server.dir\build.make:95: recipe for target 'Simple_Server.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Simple_Server.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Simple_Server.dir/rule' failed
mingw32-make.exe[3]: *** [Simple_Server.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Simple_Server.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Simple_Server.dir/rule] Error 2
Makefile:117: recipe for target 'Simple_Server' failed
mingw32-make.exe: *** [Simple_Server] Error 2

我现在使用以下命令用 gcc 编译:

gcc -Wl,-verbose -lws2_32 -lwsock32 -o simple_server main.c > linker.txt

它给出以下输出:

==================================================
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o succeeded
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o succeeded
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/ws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libws2_32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/ws2_32.lib failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libws2_32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/ws2_32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0\libws2_32.a failed
attempt to open c:/mingw/bin/../lib/gcc/libws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/ws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/libws2_32.a failed
attempt to open c:/mingw/bin/../lib/gcc/ws2_32.lib failed
attempt to open c:/mingw/bin/../lib/gcc/libws2_32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/ws2_32.dll failed
attempt to open c:/mingw/bin/../lib/gcc\libws2_32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/ws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libws2_32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/ws2_32.lib failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libws2_32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/ws2_32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib\libws2_32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../ws2_32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libws2_32.a succeeded
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libwsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/wsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libwsock32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/wsock32.lib failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libwsock32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/wsock32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0\libwsock32.a failed
attempt to open c:/mingw/bin/../lib/gcc/libwsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/wsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/libwsock32.a failed
attempt to open c:/mingw/bin/../lib/gcc/wsock32.lib failed
attempt to open c:/mingw/bin/../lib/gcc/libwsock32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/wsock32.dll failed
attempt to open c:/mingw/bin/../lib/gcc\libwsock32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libwsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/wsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libwsock32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/wsock32.lib failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libwsock32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/wsock32.dll failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib\libwsock32.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libwsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../wsock32.dll.a failed
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libwsock32.a succeeded

开头是这样的:

using internal linker script:
==================================================
/* Default linker script, for normal executables */
/* Copyright (C) 2014-2017 Free Software Foundation, Inc.
   Copying and distribution of this script, with or without modification,
   are permitted in any medium without royalty provided the copyright
   notice and this notice are preserved.  */
OUTPUT_FORMAT(pei-i386)
SEARCH_DIR("/mingw/mingw32/lib"); SEARCH_DIR("/mingw/lib"); SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib"); SEARCH_DIR("/usr/lib");

【问题讨论】:

  • 我们无法判断您的链接命令出了什么问题,因为我们看不到它。将 clion 输出替换为详细输出 (make VERBOSE=1)
  • 如何开启详细模式?

标签: c linker mingw winsock clion


【解决方案1】:

首先删除 #pragma comment(lib, "WS2_32.lib") 这只是 Visual C++ 编译器指令。

第二个,您没有提供 CMakeLists.txt,您知道 CLion 的目标是跨平台解决方案,并从 CMake 为您指定的环境(toolchain 选项卡)配置 make 文件。这意味着您不需要手动调用 gcc 和链接库,您只需要在 add_executable(myexecutable) 之后的 target_link_libraries(myexecutable ws2_32)

第三,您不需要将wsock32.lib 实际链接到您的项目,除非您希望向后兼容 Win95(我相信您不会)。更多详情请查看this answer

您的顶级 CMake 文件应如下所示:

# system
cmake_minimum_required(VERSION 3.6)
set(CMAKE_C_STANDARD 99)

# project
project(WinsockExample C)

# sources
set(source_files
    main.c
    )

# build
add_executable(${CMAKE_PROJECT_NAME} ${source_files})
target_link_libraries(${CMAKE_PROJECT_NAME} ws2_32)

【讨论】:

  • 非常感谢它帮助了我很多:)
  • @MNCODE 很高兴为您提供帮助。顺便说一句,我犯了一个小错误,add_executable() 应该使用source_files 变量,现在已修复:)
猜你喜欢
  • 1970-01-01
  • 2010-11-25
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2012-12-06
相关资源
最近更新 更多