Results 1 to 2 of 2

Thread: Compilation Error

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Compilation Error

    Hi Guys, I'm trying to compile this code:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <winsock2.h>
    3. #include <stdio.h>
    4.  
    5. // #include <netinet/in.h>
    6. // #include <arpa.inet.h>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. struct sockaddr_in peer; //"struct sockaddr_in" is a structure type and "peer" is a name of the variable. The "struct" word comes from C and it is not needed in C++
    11. int s;
    12. int rc;
    13. char buf[1];
    14.  
    15. peer.sin_family = AF_INET;
    16. peer.sin_port = htons(7500);
    17. peer.sin_addr.s_addr = inet_addr("127.0.0.1");
    18.  
    19. s = socket(AF_INET, SOCK_STREAM, 0);
    20.  
    21. if(s < 0)
    22. {
    23. perror("socket call failed");
    24. exit(0);
    25. }
    26.  
    27. rc = connect(s, (struct sockaddr *)&peer, sizeof(peer));
    28.  
    29. if(rc)
    30. {
    31. perror("connect call failed");
    32. exit(1);
    33. }
    34.  
    35. rc = send(s, "1", 1, 0);
    36.  
    37. if(rc <= 0)
    38. {
    39. perror("send call failed");
    40. exit(1);
    41. }
    42.  
    43. rc = recv(s, buf, 1, 0);
    44. if(rc <= 0)
    45. perror("recv call failed");
    46. else
    47. printf("%c\n", buf[0]);
    48. exit(0);
    49.  
    50. QCoreApplication a(argc, argv);
    51.  
    52. return a.exec();
    53. }
    To copy to clipboard, switch view to plain text mode 

    But I get undefined reference errors for all the functions like htons, inet_addr etc basically all the functions which are part of Ws2_32.lib with the header Winsock2.h as per:

    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Here is my .pro file:

    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2015-10-08T12:19:26
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core
    8.  
    9. QT -= gui
    10.  
    11. TARGET = TCP_IPClient
    12. CONFIG += console
    13. CONFIG -= app_bundle
    14.  
    15. TEMPLATE = app
    16.  
    17.  
    18. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

    According to stack overflow:

    "you can get 'Undefined reference' linker errors when attempting to compile C code with g++, if you don't tell the compiler to use C linkage."

    I'm guessing that this is my problem, so how do I tell the compiler to use C linkage?

  2. #2
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Compilation Error

    I think I'm slowly figuring out what needs to be done, I think I need to find the Ws2_32.lib somewhere and add it in my .pro file, will update if this works.


    Added after 1 19 minutes:


    Yep installed the microsft SDK and linked through .pro file, all is working now .

    As a side note, you need to uninstall the redistributable 2010 files prior to installation, otherwise you can't install the SDK.

    Here is a link to the explanation:

    https://wiki.qt.io/Cannot_Install_Windows_SDK
    Last edited by Atomic_Sheep; 9th October 2015 at 11:08.

Similar Threads

  1. Qt compilation error
    By saul.andrade in forum Newbie
    Replies: 1
    Last Post: 17th March 2011, 09:51
  2. Qt 4.6.2. compilation error
    By b1 in forum Installation and Deployment
    Replies: 3
    Last Post: 13th June 2010, 08:22
  3. compilation error
    By jjbabu in forum Qt Programming
    Replies: 30
    Last Post: 8th April 2009, 10:20
  4. Compilation Error
    By ^NyAw^ in forum General Programming
    Replies: 2
    Last Post: 30th September 2008, 17:37
  5. Qt 4.4.0 compilation Error?
    By mismael85 in forum Installation and Deployment
    Replies: 7
    Last Post: 3rd July 2008, 09:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.