Eärendil Baggins
Eärendil Baggins

Reputation: 572

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?

Upvotes: 0

Views: 246

Answers (2)

laurisvr
laurisvr

Reputation: 2822

I know this is an old question, but I ran in this issue today as well. And for me the issue was that the item type of the file was set to a source file. I managed to resolve it by going to the properties of the file and setting the item type to C/C++

Upvotes: 1

chqrlie
chqrlie

Reputation: 144715

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...

Upvotes: 1

Related Questions