Operating System - HP-UX
1753747 Members
4950 Online
108799 Solutions
New Discussion юеВ

"almost" empty source file, object file size ?

 
Banibrata Dutta
Frequent Advisor

"almost" empty source file, object file size ?

i was facing a problem with my ansi-C compiler, so i decided to give it a try using aCC,
and the behaviour turned out to be the same.

consider the following piece of code in a file:

#ifdef _AS
#include
#include
#define XMAS christmas()
int x = 2;
void gogetit(int a) {
printf("%d", a);
}
#endif /* _AS */

compile it using "aCC -Aa -g -c 1.c", and it says:
Warning 336: Unknown position # Entire translation unit was empty.

compile it using "cc -Aa -g -c 1.c", and it says:
cc: "1.c", line 1: warning 501: Empty source file.

but, if i move my #ifdef _AS *after* the 2 #include's i.e.

#include
#include
#ifdef _AS
#define XMAS christmas()

the file compiles fine, i.e. no warnings/errors, and a finite size 1.o is created,
of 552 bytes (using aCC) and 580 bytes (using cc i.e. anis-C compiler).
if i run "nm" over it, it says "no symbols".

my questions are:

(1) how is the inclusing of stdio.h giving the compiler "code" to assume the
translation unit to be non-empty ?
(2) what exactly is this code, since the 1.o is non-empty ?
(3) since 1.o is non-empty, why is "nm" saying "no symbol" for a finite sized
1.o and source compiled with -g option ?
2 REPLIES 2
Elmar P. Kolkman
Honored Contributor

Re: "almost" empty source file, object file size ?

The source is parsed by the C pre processor before going through the 'real' compiler. This preporcessor will parse the lines starting with the hashes, like #ifdef etc. When compiling, _AS is not defined normally, resulting in everything, including, between the #ifdef and the #endif to be skipped.

This means that nothing has to be compiled, which in turn results in your errors/warnings

When moving the #include's to the top, the contents of those included files will be compiled.

To prevent this, add some line, like a line containing revision info, at the top of your C-sources.
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: "almost" empty source file, object file size ?

When compiling stdio.h, an object file will be generated with empty parts ('cat -tv' shows as much, though it might be hard to read). The input not being empty just tells the compiler to generate valid output. And this .o is an 'empty' output.

There are no routines, no variables, just empty blocks.

Though not for the faint of heart, take a look at the manual pages of a.out and elf.
Every problem has at least one solution. Only some solutions are harder to find.