Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compiling header files 1

Status
Not open for further replies.

gio2888

Technical User
Oct 26, 2000
64
US
I want to know how to compile header files with gcc...thanks in advance

I tried gcc something.h
it didn t work, which option do I use?
 
You don't compile header files. You just include them in your code when you need them. For example, the usual "Hello World" program needs the header file [tt]stdio.h[/tt]. So you would use it as follows...
[tt]
/* hello.c */

#include <stdio.h>

void main()
{
printf(&quot;\n\nHello World!\n\n&quot;);

return;
}
[/tt]
Then just compile this program. The [tt]#include[/tt] statement will pull it in.

Hope this helps.

 
i think the question was for user written header files

you have to use the compiler option to compile header files
though i am not sure but it must be

gcc -h something.h
 
Generally you should not compile any header files even user writen. Of cause you can put function implementation in header file and then you have to compile it, but this is WRONG design and you will not be able to include this file anywere, cause you will have double function difinitions during the linking.

There is a lot of way to messed up your programm, so just don't do it and do not try to make normal source file .c file looks like a header file.
 
You still don't compile header files. Header files don't, and shouldn't have executable code in them. They are for things like definitions, constants, function prototypes and such. If you have your own user written header file, you include it in your code with the following...
[tt]
#include &quot;myheader.h&quot;
[/tt]
The quotes make the preprocessor look for the header file exactly where you say, and not in the normal header directories.

The [tt]#include[/tt] statement causes the preprocessor to bring in the header file at that point, so it will be compiled when the module including it gets compiled.

Also, if you have executable statements in your header file, this should not be a header file. This is a misuse of what a header file is.

Hope this helps.

 
Unless you happen to want to use a precompiled header...

After a quick look through the gcc man page, it doesn't look like gcc supports precompiled headers.


If that's not what you were looking for (i.e. if you've never seen/heard the words &quot;precompiled header&quot;), just ignore this post.
 
When you use precompiled headers (for example MS C++, and HP aCC support it) you don't compile it directly anyway. You still have to create .c file and include your common header file and then compile it. But I am agree it's not a normal object file since there will be only empty declarations, so in a way you are compiling header files.
 
Right.

It's more like saving the state of the compiler after it has processed the contents of a header file (or several), then using that state as a starting point for all files that include that header.

A more correct term would be something like preprocessed header or preparsed header.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top