0

I have looked at these links : This one and This and a couple of other similar ones. None of the answers given here are working methods are working.

I have a two source files a1.c , a2.c and two header files a1.h and a2.h . I want to include the header files in both these files (and a2.c in a1.c as there is a function I need to use from a2.c)

I have included

#include "a1.h"  
#include "a2.h"

in the source files of a1.c

I'm using GCC on Ubuntu. and using the command gcc a1.h -o a1.out -lm and that didn't work.

I tried with

gcc -c -I/Home/Documents/ctests/ a1.c -o a1.out

as well as

gcc -c a1.c -I/Home/Documents/ctests/ -o a1.out

My spellings are okay as well (there's hardly any room for error there with one letter and a number as the filename anyway).

Also, all the files are in the same folder.

I know this may be a trivial question but I am stuck on this one and would appreciate any help. I am relatively new to programming and completely new to Linux and Unix as far as using the command line goes.

Many thanks!

6
  • If all files are in the same folder and you start GCC from this folder there is not need to use option -I to find a1.hand a2.h. Commented Apr 1, 2013 at 14:01
  • What error messages does GCC issue? Commented Apr 1, 2013 at 14:02
  • I get the same error all the time: No such file or directory Commented Apr 1, 2013 at 14:07
  • Did you cd to the folder where your .h and .c files reside? Commented Apr 1, 2013 at 14:10
  • Why shouldn't I include .c files in other .c files? The whole idea behind creating different modules is that you shouldn't have to recompile your entire project every time. If you only change a2.c, you should only have to recompile a2.c and link the old, unchanged a1.o (which shouldn't be recompiled because it doesn't include a2.c) with the new a2.o, like in stardust_'s answer. Commented Apr 1, 2013 at 14:11

3 Answers 3

4
gcc -c

tells gcc to compile the file to object (the .o files you see everywhere). To be linked later with some other .o files to an executable.

So what you want to do is either compile the two files separately and link them later. like this.

gcc -I"/Home/Documents/ctests/" -c a1.c
gcc -I"/Home/Documents/ctests/" -c a2.c

gcc -o myprogram a1.o a2.o 

Or just compile and link at the same time.

gcc -I"/Home/Documents/ctests/" a2.c a1.c -o myprogram

And then run your program like

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

10 Comments

Don't you mean gcc a1.o a2.o -o myprogram ?
gcc -o a1.o a2.o myprogram is the same. isn't it?
No it isn't. It will try to compile/link a2.o and myprogram to create an executable a1.o. (At least my gcc 4.7.2 does)
Oh dear god. How did i do that. I have fixed it. Tanks.
I don't need to link them later. I just used the -c option as a force of habit. Even If I remove it, it gives me the same error. I just need those header files to be visible to the a1.c file. And that's the first step where it's showing me the error.
|
1

Compile everything, and link it together.

If all files are in one directory, this should work:

  gcc a1.c a2.c -o myapp

When you want to create separate object files, do this:

  gcc -c a1.c a2.c

Then you can then link together to create an application:

  gcc a1.o a2.o -o myapp

Comments

0

Your gcc command should be like this

gcc -I/Home/Documents/ctests/ -o a1.out a1.c

and you have to include a1.h and a2.h header file in your a1.c like this

#include "a1.h"
#include "a2.h"

If you are calling some function from a2.c in your a1.c then you have to build your program in this way

gcc -I/Home/Documents/ctests/ -o a1.out a2.c a1.c

3 Comments

Files are included this way and the above gcc code is the same as writing gcc -I/Home/Documents/ctests/ a1.c -o a1.out if i'm not mistaken. And this doesn't seem to be working.
@Ade did you use some function from a2.c in a1.c ? if it's the case I updated my answer with that case
Yes I did. And that was the problem. Many Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.