1

I have three C files in total. One is a header [.h] file, two are source [.c] files.

The .h file is called encryption.h and the corresponding source file is encryption.c. The encryption.c has logic, but no main() function. My second c file is called main.c. There I have the main() function that calls methods from encryption.c.

I am compiling these files within terminal on Mac OSx. I am confused on how to compile this, I have tried the following:

gcc -c main.c gcc -c encryption.c gcc -c encryption.h gcc main.o encryption.o encryption.g.gch -o encrypt 

This doesn't seem to work though, it says I have a precompiled-header already. I tried finding the answer online, I know it has to be simple, but I haven't had much luck. What is the issue here?

5
  • 1
    gcc -c encryption.h----> you don't compile header files. Commented Apr 15, 2015 at 12:17
  • Thank you @SouravGhosh. What would be the appropriate way of going about this? Commented Apr 15, 2015 at 12:18
  • 1
    Just don't compile the header file, and don't link with the precompiled header file, only the object files generated from the source files. Commented Apr 15, 2015 at 12:19
  • I don't know, evidently whatever happens to a compiled .h file with gcc.... Commented Apr 15, 2015 at 12:51
  • From what I looked up online they're precompiled header files used to optimize performance in some aspect. @JoachimPileborg Commented Apr 15, 2015 at 12:53

1 Answer 1

5

Don't compile the header file. Header files are meant to be included to the source files (using #include directive, in c). Just compile the source files and link them together. Something like

gcc -c main.c gcc -c encryption.c gcc main.o encryption.o -o encrypt 

or, for shorthand,

gcc main.c encryption.c -o encrypt 

Note: If you're bothered about the presence (or absence) of header files while compilation, check the pre-processed output of each source files using gcc -E option.

Sign up to request clarification or add additional context in comments.

3 Comments

What's the point of the header file then? I am really familiar with Java, and I see the header file like an interface. Is there no point in having it if it's not going to be used then? I mean unless there are constants in the header file, then that's a different story, but my professor wants the skeleton code for my functions in that file. . .
@MiguelJ. Updated the answer. Hope that helps. :-)
The point of header files is, basically, so that every code file which #includes it knows how the functions declared there work. Also knows about #defines and types ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.