Search code examples
cvisual-studiovisual-studio-2017header-files

Visual Studio seems to compile headers as .c files


When I try to build a C project that contains .c and .h files, Visual Studio gives me weird errors, like this is the code for my header (which should be perfectly fine):

#ifndef _CLIENT_SOCKET_H_
#define _CLIENT_SOCKET_H_

#include "common.h"
#include "buffer.h"

#ifdef  __cplusplus
extern "C" {
#endif

typedef void* wsocket;

int socket_create(wsocket* sock, int port, const char* addr, struct sockaddr_in *s_addr); //This is line 28

int socket_connect(wsocket sock, struct sockaddr_in s_addr);

bool socket_recv_buffer(wsocket, buffer_t*);

bool socket_send_buffer(wsocket, buffer_t);

int socket_destroy(wsocket* sock);

#ifdef  __cplusplus
}
#endif

#endif  /* _CLIENT_SOCKET_H_ */

And I get errors like:

syntax error: missing '{' before '.'    

or

syntax error: '.'   

both at line 28.

It seems like it's trying to compile the header as if it was a .c file, since it wants a body for the function and such. Do you have any idea of what could be happening?


Solution

  • If line 28 in indeed the one you commented, the compilation error is quite surprising: there is no . on this line.

    A possible explanation is at least one of the identifiers on this line was defined in common.h or buffer.h as a macro and expanded into a structure member access.

    For example:

    #define socket_create    socket.create
    

    Check the contents of these header files.

    EDIT: the problem was indeed a spurious macro definition in a system header file:

    https://msdn.microsoft.com/en-us/library/windows/hardware/ff556972(v=vs.85).aspx

    s_addr is defined in as #define s_addr S_un.S_addr

    Using s_addr as the name of an argument (or a local variable) leads to a cryptic error message from the compiler, because of the macro expansion...