Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Linker Errors when Compiling C Socket Program in DevC++

Status
Not open for further replies.

shinsengumi

Newbie level 4
Joined
Dec 11, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,350
Hello all. I'm trying to compile a socket program in DevC++ but everytime I do it I get a lot of linker. I have previously run the socket program successfully in Linux but I have to export it to WindowsXP, and I must admit I'm relatively new to programming in WindowsXP. I also learned that to solve these linker problems I need to link the libraries to the program and that's my main problem now. How do I link the libraries in WindowsXP and make the program in DevC++ work? By the way I'm using C programming language. Please help me solve this problem. Thanks!
 

Hello all. I'm trying to compile a socket program in DevC++ but everytime I do it I get a lot of linker. I have previously run the socket program successfully in Linux but I have to export it to WindowsXP, and I must admit I'm relatively new to programming in WindowsXP. I also learned that to solve these linker problems I need to link the libraries to the program and that's my main problem now. How do I link the libraries in WindowsXP and make the program in DevC++ work? By the way I'm using C programming language. Please help me solve this problem. Thanks!

I guess you are directly compiling the Linux code using DevCpp.
For most of the things it works. But As you told you are writing socket program, I guess you are missing initializing the Winsock startup.

Try checking that.

Next: Try linking with Static lib(*.a) if it again gives you error then please contact me with detail output eror.

Hope this helps.
 

First off thanks for the reply.

I didn't compile the Linux program on windows as is. I've edited my program's header files such that the program may be compiled on windows. I attached them in my previous post but since I'm new here I wasn't allowed to post such links yet so I removed them(but after this post I'll be allowed to do so.) I used header files like windows.h winsock2.h and the lean_and_mean thing. I edited the source code according to the syntax understandable by windows but still I wasn't able to compile it. From reading articles I knew I needed to link the socket libraries into my program but that's my problem now, How do I link the libraries? Where will I find those *.a things? Please help me sort things out. Thanks
 

i assume that you have changed the include files for windows (as given in post) and you have properly initialised winsock ,

include -lws2_32 may solve your problem.
 
i assume that you have changed the include files for windows (as given in post) and you have properly initialised winsock ,

include -lws2_32 may solve your problem.

Thanks! Most of the linker errors were gone now. Some still remained though:
Code:
  [Linker error] undefined reference to `inet_pton' 
  [Linker error] undefined reference to `index'
I used inet_pton and *index in the Linux program. Do you know of their supposed counterparts for windows? These are the header files I used:
Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#include <winsock.h>
#include <winsock2.h>
#include <stdio.h>
#include <string.h>

#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
I have some questions though. I don't know how to properly check the winsock version of my windows but when I checked the system32 folder, I found the winsock.dll file so I think the windows I have has winsock version 1.1 right? But how come ws2_32 worked for me? Isn't it for winsock version 2.2?
Code:
WSAStartup(MAKEWORD(1,1), &wsaData);
How do I know whether to put 1,1 or 2,2 or 2,0? Thanks
 

inet_pton is not available in windows.
you have to use win32api ,
WSAStringToAddress(...)
to write the equivalent.


by writing ,
WSAStartup(MAKEWORD(1,1), &wsaData);
you are asking for winsock library of version 1.1 or "higher".
so you need not be concerned with the version in the system.
(though you can write a code to print the version no . for diagnostic purpose)
 

inet_pton is not available in windows.
you have to use win32api ,
WSAStringToAddress(...)
to write the equivalent.


by writing ,
WSAStartup(MAKEWORD(1,1), &wsaData);
you are asking for winsock library of version 1.1 or "higher".
so you need not be concerned with the version in the system.
(though you can write a code to print the version no . for diagnostic purpose)

I tried using WSAStringToAddressA:
Code:
int main(int argc, char **argv) {
  SOCKADDR_IN servaddr;
  char   AddrValue[256] = "10.36.7.172";
  char   AddrString[256]  = "Some dummy value";
  DWORD   dwSizeOfStr = sizeof(AddrValue);
  int    nSizeOfInput = sizeof(AddrString);
  WSADATA wsaData;
  SOCKET ConnectSocket = INVALID_SOCKET;
  int iResult;

  // Initialize Winsock
  iResult = WSAStartup(MAKEWORD(1,1), &wsaData);
  if (iResult != 0) {
      printf("WSAStartup failed with error: %d\n", iResult);
      //system("PAUSE");
      return 1;
  }

  ZeroMemory(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(80);
  //servaddr.sin_addr.ConnectSocket_addr = inet_addr("10.36.7.172");
  //inet_pton(AF_INET,"10.36.7.172",&servaddr.sin_addr);
  if(WSAStringToAddressA(AddrValue, AF_INET, NULL, (LPSOCKADDR)&servaddr, &nSizeOfInput) != 0)
  {
    printf("\nWSAStringToAddressA failed with error num %ld\n", WSAGetLastError());
    WSACleanup();
    //system("PAUSE");
    return 1;
  }

It didn't work though. I think I'm just missing something here. I just copied and edited the part where I used WSAStringToAddressA so there is a part of it that I didn't really understand. That part is the last parameter in the function "&nSizeOfInput". What is it really for and do I really need it? nSizeOfInput is just the size of "AddrString" which doesn't really hold the IP address string, but why should it be a parameter of WSAStringToAddressA? I just found a handful of tutorials in the internet and none really explained this function in detail. Could you explain to me how WSAStringToAddressA really works? Thanks a lot.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top