Thread: #define INITSOCK if (WSAStartup(0x0202, ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    30

    #define INITSOCK if (WSAStartup(0x0202, ...

    Hi im solving problem of function of preprocesor, i dont understand why this cant be linked. Im speaking about funciton communication because there seems is the problem

    Code:
    #include <string.h>
    #include <sys/types.h>
    #ifdef _WIN32 // _WIN32 is defined by all Windows 32 and 64 bit compilers, but not by others.
    #include <winsock2.h>
    #define WSDATA WSADATA wsaData;
    #define INITSOCK if (WSAStartup(0x0202, &wsaData) == 0) { if (wsaData.wVersion == 0x0202) {
    #define ENDOFSOCK } WSACleanup(); }
    #define DECLARE SOCKET
    #define CON(x) (x != INVALID_SOCKET)
    #define CLOSE(x) closesocket(x);
    #else
    #include <sys/socket.h>
    #include <netinet/in.h>
    #define WSDATA ;
    #define INITSOCK ;
    #define ENDOFSOCK ;
    #define DECLARE int
    #define CON(x) (x >= 0)
    #define CLOSE(x) close(x);
    #endif
    
    typedef struct {
        char messege[255];
        char client_addr[22];
    } udppack;
    
    char messege[255];    //command - msg
    char rcvd_msg[255];   // recieved messege
    char server_address_str[22];
    int max_count_of_rcv = 10;
    struct udppack *pack;
    struct sockaddr_in server_socket_address;
    struct in_addr server_address;
    struct sockaddr_in client_socket_address;
    char *server_addr;
    char *server_port;
    int server_port_int;
    int addrlen;
    WSDATA
    DECLARE client_socket;
    //address
    
    
    int communicate() {
        INITSOCK
        client_socket = socket(AF_INET, SOCK_DGRAM, 0);
        if CON(client_socket) {
            client_socket_address.sin_family = AF_INET;
            client_socket_address.sin_addr.s_addr = INADDR_ANY;
            client_socket_address.sin_port = 0;
    
            if (bind(client_socket, (struct sockaddr*)&client_socket_address,
                sizeof(client_socket_address)) == 0) {
    
                server_addr = strtok(server_address_str,":");
                server_port = strtok(NULL,"");
                server_address.s_addr = inet_addr(server_addr);
                server_port_int = htons((u_short)atoi(server_port));
    
                printf("DatagramType sends to %s:%hu\n",
                    //inet_aton(server_address),
                    ntohs(server_port_int));
    
                addrlen = sizeof(client_socket_address);
                if (getsockname(client_socket, (struct sockaddr*)&client_socket_address,
                    &addrlen) == 0) {
    
                printf("as %s:%hu\n",
                    inet_ntoa(client_socket_address.sin_addr),
                    ntohs(client_socket_address.sin_port));
    
                    server_socket_address.sin_family = AF_INET;
                    server_socket_address.sin_addr = server_address;
                    server_socket_address.sin_port = server_port_int;
                }
            }
        }
        ENDOFSOCK
        return 0;
    }
    Last edited by Holymanus; 01-03-2011 at 12:38 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Holymanus View Post
    i dont understand why this cant be linked.
    Do you mean that you have a linker error?
    Perhaps you forgot to add Ws2_32.lib to your link library settings?
    Otherwise please post the linker error, or any other kind of error you get, here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    Code:
    gcc -oSEM.exe tab.o clientapp.o app.o
    clientapp.o:clientapp.c:(.text+0x17): undefined reference to `WSAStartup@8'
    clientapp.o:clientapp.c:(.text+0x2e): undefined reference to `WSACleanup@0'
    clientapp.o:clientapp.c:(.text+0x54): undefined reference to `socket@12'
    clientapp.o:clientapp.c:(.text+0x95): undefined reference to `bind@12'
    clientapp.o:clientapp.c:(.text+0xdb): undefined reference to `inet_addr@4'
    clientapp.o:clientapp.c:(.text+0xf9): undefined reference to `htons@4'
    clientapp.o:clientapp.c:(.text+0x10a): undefined reference to `ntohs@4'
    clientapp.o:clientapp.c:(.text+0x145): undefined reference to `getsockname@12'
    clientapp.o:clientapp.c:(.text+0x15f): undefined reference to `ntohs@4'
    clientapp.o:clientapp.c:(.text+0x16f): undefined reference to `inet_ntoa@4'
    collect2: ld returned 1 exit status
    Build error occurred, build is stopped

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you've got to link to the library those functions are defined in. For instance, WSAStartup is in ws2_32.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    you mean with parametr -l Ws2_32.lib ?

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    Code:
    gcc -LC:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib -oSEM.exe tab.o clientapp.o app.o -lWs2_32.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: cannot find -lWs2_32.lib
    collect2: ld returned 1 exit status
    Build error occurred, build is stopped

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    And if im trying to add this file with ld.exe

    Code:
    ld.exe "C:\Program Files\Microsoft SDKs\Windows\v6.0A
    \Lib\Ws2_32.lib"
    ld.exe: warning: cannot find entry symbol _mainCRTStartup; defaulting to 00401000

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You probably just need to do "-lws2_32".

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    same result like before

    Code:
    C:\Users\Martin\Desktop\C\SEM\SEM\SEM 2. verze>gcc -lws2_32 -o clapp clientapp.c
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x17):clientapp.c: undefined reference to `WSASt
    artup@8'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x4d):clientapp.c: undefined reference to `socke
    t@12'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x9b):clientapp.c: undefined reference to `bind@
    12'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0xe5):clientapp.c: undefined reference to `inet_
    addr@4'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x105):clientapp.c: undefined reference to `hton
    s@4'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x120):clientapp.c: undefined reference to `ntoh
    s@4'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x15d):clientapp.c: undefined reference to `gets
    ockname@12'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x173):clientapp.c: undefined reference to `ntoh
    s@4'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x186):clientapp.c: undefined reference to `inet
    _ntoa@4'
    C:\Users\Martin\AppData\Local\Temp/ccIDgaaa.o(.text+0x1c9):clientapp.c: undefined reference to `WSAC
    leanup@0'
    collect2: ld returned 1 exit status

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Put the -lws2_32 at the end (I think for the linker it matters).

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    right at the end it works
    and how to add it to makefile


    edit: i have it, simply
    Code:
    gcc $? -o $@ -lws2_32
    Last edited by Holymanus; 01-03-2011 at 04:50 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Holymanus View Post
    right at the end it works
    and how to add it to makefile
    If you have a list of libraries, add it in. If you just have a final linking command at the end, put your -l there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RS232 connection set up
    By cs342 in forum C Programming
    Replies: 25
    Last Post: 05-26-2010, 03:57 AM
  2. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 06:58 PM
  5. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM

Tags for this Thread