Cannot compile a C program in Visual C++ Express

This is a discussion on Cannot compile a C program in Visual C++ Express within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to compile a C program in Visual C++ 2005 Express but I get some errors. Can ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    24

    Cannot compile a C program in Visual C++ Express

    Hi,

    I am trying to compile a C program in Visual C++ 2005 Express but I get some errors. Can someone tell me how I can resolve this?

    I use the following C code:

    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
        {
             
              WORD versie;
              versie = MAKEWORD ( 2, 2);
              WSADATA wsaData;
              int resultaat;
              
              resultaat = WSAStartup(versie, &wsaData);
              
              if ( resultaat != 0 ){
                   printf("WSAStartup error");
                   }
              
              SOCKET mijnsocket;
    
              
              mijnsocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
              
              if ( socketresultaat == INVALID_SOCKET){
                   printf("WSASocket errror");
                   }
                   
              struct sockaddr_in gegevens;
              
              
    	  gegevens.sin_family = AF_INET;
              gegevens.sin_port = htons(7777);
              gegevens.sin_addr.s_addr = inet_addr("127.0.0.1");
              memset(&(gegevens.sin_zero), '\0', 8);
      
              
    		  int bindresultaat;        
              
    		  bindresultaat = bind( mijnsocket, (struct sockaddr *) &gegevens, sizeof( struct sockaddr_in ) );
              
    		  if (bindresultaat == SOCKET_ERROR ){
                                printf("Bind error");
                                }
              
    		  listen(mijnsocket, 1);
    
                         
              accept(mijnsocket, (struct sockaddr *)&gegevens, sizeof(struct sockaddr_in));
    
              return 0;
              
              }
    The errors are:

    Code:
    c:\documents and settings\tex\bureaublad\wsa2.c(13) : error C2275: 'WSADATA' : illegal use of this type as an expression
            c:\program files\microsoft platform sdk\include\winsock2.h(426) : see declaration of 'WSADATA'
    c:\documents and settings\tex\bureaublad\wsa2.c(13) : error C2146: syntax error : missing ';' before identifier 'wsaData'
    c:\documents and settings\tex\bureaublad\wsa2.c(13) : error C2065: 'wsaData' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(14) : error C2143: syntax error : missing ';' before 'type'
    c:\documents and settings\tex\bureaublad\wsa2.c(16) : error C2065: 'resultaat' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(16) : warning C4133: 'function' : incompatible types - from 'int *__w64 ' to 'LPWSADATA'
    c:\documents and settings\tex\bureaublad\wsa2.c(22) : error C2275: 'SOCKET' : illegal use of this type as an expression
            c:\program files\microsoft platform sdk\include\winsock2.h(98) : see declaration of 'SOCKET'
    c:\documents and settings\tex\bureaublad\wsa2.c(22) : error C2146: syntax error : missing ';' before identifier 'mijnsocket'
    c:\documents and settings\tex\bureaublad\wsa2.c(22) : error C2065: 'mijnsocket' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(25) : warning C4244: '=' : conversion from 'SOCKET' to 'int', possible loss of data
    c:\documents and settings\tex\bureaublad\wsa2.c(27) : error C2065: 'socketresultaat' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(31) : error C2143: syntax error : missing ';' before 'type'
    c:\documents and settings\tex\bureaublad\wsa2.c(34) : error C2065: 'gegevens' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(34) : error C2224: left of '.sin_family' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(35) : error C2224: left of '.sin_port' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(36) : error C2224: left of '.sin_addr' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(37) : error C2224: left of '.sin_zero' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(37) : error C2198: 'memset' : too few arguments for call
    c:\documents and settings\tex\bureaublad\wsa2.c(40) : error C2143: syntax error : missing ';' before 'type'
    c:\documents and settings\tex\bureaublad\wsa2.c(42) : error C2065: 'bindresultaat' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(51) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'size_t'
    c:\documents and settings\tex\bureaublad\wsa2.c(51) : warning C4024: 'accept' : different types for formal and actual parameter 3
    Thanks for your help.

    Regards,

    Ben

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I _think_ you should move this line down to below your variable declarations:
    Code:
              versie = MAKEWORD ( 2, 2);
    In standard C, it's not legal to mix code and variable declarations. The above line of code is not a variable declaration, so should be after the declarations.

    I could be wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    24
    Thanks matsp,

    That got rid of most of the erros. I have to place
    Code:
    SOCKET mijnsocket;
    at the variable declarations also. I also had to change
    Code:
    if ( socketresultaat == INVALID_SOCKET){
    to
    Code:
    if (  mijnsocket == INVALID_SOCKET){
    I now get the following errors.

    Code:
    c:\documents and settings\tex\bureaublad\wsa2.c(32) : error C2143: syntax error : missing ';' before 'type'
    c:\documents and settings\tex\bureaublad\wsa2.c(35) : error C2065: 'gegevens' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(35) : error C2224: left of '.sin_family' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(36) : error C2224: left of '.sin_port' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(37) : error C2224: left of '.sin_addr' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(38) : error C2224: left of '.sin_zero' must have struct/union type
    c:\documents and settings\tex\bureaublad\wsa2.c(38) : error C2198: 'memset' : too few arguments for call
    c:\documents and settings\tex\bureaublad\wsa2.c(41) : error C2143: syntax error : missing ';' before 'type'
    c:\documents and settings\tex\bureaublad\wsa2.c(43) : error C2065: 'bindresultaat' : undeclared identifier
    c:\documents and settings\tex\bureaublad\wsa2.c(52) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'size_t'
    c:\documents and settings\tex\bureaublad\wsa2.c(52) : warning C4024: 'accept' : different types for formal and actual parameter 3

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    or make it one line initialization
    instead of
    Code:
    WORD versie;
              versie = MAKEWORD ( 2, 2);
    use
    Code:
    WORD versie = MAKEWORD ( 2, 2);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I now get the following errors.
    And what is the current code?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    24
    Ok thanks

    The current code is:

    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <stdio.h>
    
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
        {
             
              WORD versie;
              SOCKET mijnsocket;
              WSADATA wsaData;
              int resultaat;
              
    		  versie = MAKEWORD ( 2, 2);
              resultaat = WSAStartup(versie, &wsaData);
              
              if ( resultaat != 0 ){
                   printf("WSAStartup error");
                   }
              
             
    
              
              mijnsocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
              
              if ( mijnsocket == INVALID_SOCKET){
                   printf("WSASocket errror");
                   }
                   
              struct sockaddr_in gegevens;
              
              
    		  gegevens.sin_family = AF_INET;
              gegevens.sin_port = htons(7777);
              gegevens.sin_addr.s_addr = inet_addr("127.0.0.1");
              memset(&(gegevens.sin_zero), '\0', 8);
      
              
    		  int bindresultaat;        
              
    		  bindresultaat = bind( mijnsocket, (struct sockaddr *) &gegevens, sizeof( struct sockaddr_in ) );
              
    		  if (bindresultaat == SOCKET_ERROR ){
                                printf("Bind error");
                                }
              
    		  listen(mijnsocket, 1);
    
                         
              accept(mijnsocket, (struct sockaddr *)&gegevens, sizeof(struct sockaddr_in));
    
              return 0;
              
              }

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    These

    struct sockaddr_in gegevens;
    int bindresultaat;

    also are declarations - that should be at the beginning of the block
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
        struct sockaddr_in gegevens;
    ...
        int bindresultaat;
    Try moving that to the top also along with the other variable declarations.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    24
    Thank you very much guys

    If you could help me with the last errror messages that would be really great.

    The following error messages seem to be related to:

    Code:
    accept(mijnsocket, (struct sockaddr *)&gegevens, sizeof(struct sockaddr_in));

    Code:
    c:\documents and settings\tex\bureaublad\wsa2.c(54) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'size_t'
    c:\documents and settings\tex\bureaublad\wsa2.c(54) : warning C4024: 'accept' : different types for formal and actual parameter 3
    Linking...
    MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    C:\Documents and Settings\Tex\Bureaublad\ziek\WSA\Debug\WSA.exe : fatal error LNK1120: 1 unresolved externals

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    third parameter should be NULL or pointer to the var that will receive the actual lenght of the address

    The integer referred to by addrlen initially contains the amount of space pointed to by addr. On return it will contain the actual length in bytes of the address returned.
    so it should be like
    Code:
    int sockadrlen = (int)sizeof(struct sockaddr_in);
    accept(mijnsocket, (struct sockaddr *)&gegevens, &sockadrlen );
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    24
    Thanks vart

    That does the job indeed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM
  5. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 01:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21