I think you're running into trouble because packed
is an attribute that can be applied to structures to indicate that they should be stored with no padding between structure elements. That is more or less consistent with the error messages you indicate.
To prove or disprove this theory, rename packed
to a different name, such as Packed
and see whether that helps. Do it in defs.h
and packed.c
. It helps if the error messages change more than just as a result of the rename operation. If the message said something about packed
before and then says Packed
instead, it hasn't helped. If the error message changes substantially (or is no longer present at all), then the packed
keyword is the trouble.
In GCC, you'd need to use a notation such as __attribute__((packed))
to get a packed structure; you can't use that by accident.
Note that you do not normally compile a header; you compile the source files that use the header. It is not clear why you would be trying to link with the header. You link object files and libraries.
I can confirm that your defs.h
file compiles cleanly on Mac OS X 10.9.2 with GCC 4.8.2. I have a script that does a check compilation on a header to test for self-sufficiency and idempotency. Effectively, it creates a source file (for example, x3981f.c
) containing (in this case):
#include "defs.h" // Self-sufficiency
#include "defs.h" // Idempotency
int main(void) { return 0; } // Non-empty file
It compiles without errors or warnings with:
gcc -c -Wall -Wextra x3981f.c
The pack.c
source file needs #include <math.h>
and both source files need #include <stdlib.h>
added. However, with those additions, the code compiles cleanly using these even more stringent compilation options:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror -c pack.c
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror -c unpack.c
$
This practically convinces me that your problem is the name packed
.
OTOH, MSDN shows that you need to use #pragma pack(2)
, for example, to pack structures. That too can't be used by accident. Nevertheless, the code you show compiles cleanly on a Unix system when the missing headers are included. If it does not compile cleanly in MSVC, the problem is inherent in the MSVC environment, and not in C generically.