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!

Community
  • 1
  • 1
umayneverknow
  • 190
  • 1
  • 3
  • 13
  • 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.h`and `a2.h`. – alk Apr 01 '13 at 14:01
  • What error messages does GCC issue? – alk Apr 01 '13 at 14:02
  • I get the same error all the time: No such file or directory – umayneverknow Apr 01 '13 at 14:07
  • Did you `cd` to the folder where your `.h` and `.c` files reside? – alk Apr 01 '13 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. – autistic Apr 01 '13 at 14:11
  • @alk: yes I did cd to the same folder. I cd'd to that folder and created the files from there. So that's not the problem. @modifiablelvalue: I didn't quite understand exactly what you were saying. I just intend to make one .out file at the end. I want to use functions stored in `a2.c` in `a1.c` and variables and macros defined in `a1.h` to be used in these two functions. But the compiler doesn't acknowledge the `a1.h` file at all. – umayneverknow Apr 01 '13 at 14:25

3 Answers3

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 
  • Don't you mean `gcc a1.o a2.o -o myprogram` ? – Bart Friederichs Apr 01 '13 at 14:09
  • `gcc -o a1.o a2.o myprogram` is the same. isn't it? –  Apr 01 '13 at 14:11
  • 3
    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) – Bart Friederichs Apr 01 '13 at 14:12
  • Oh dear god. How did i do that. I have fixed it. Tanks. –  Apr 01 '13 at 14:15
  • 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. – umayneverknow Apr 01 '13 at 14:19
  • Are the headers in the same folder as .c files? or in the folder `/Home/Documents/ctests/`? Also put the exact error message gcc gives you. Add an edit to your question if you need space –  Apr 01 '13 at 14:21
  • btw `a2.c` probably needs to include the .h files too. It is not enough to include it just in `a1.c` –  Apr 01 '13 at 14:22
  • @stardust_ : Both are in the same folder. But I realized where I goofed up. Thanks for reminding me that a2.c needs the header files as well. I had written it as `#include` in the second source file. Silly mistake. – umayneverknow Apr 01 '13 at 14:31
  • And I didn't need to use the -I directory option either – umayneverknow Apr 01 '13 at 14:32
  • Yes. for this example you don't need -I option. I think you have figured out the differece between <> and "". –  Apr 01 '13 at 14:34
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
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
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
MOHAMED
  • 41,599
  • 58
  • 163
  • 268