24

I have two files, main.o and modules.o, and I'm trying to compile them so that main.o can call functions in modules.o. I was explicitly told not to try #include module.o. I really don't know what I should be doing instead. I tried a few different versions of gcc (such as gcc -x c driver main.o modules.o), but nothing I get works: the compiler continuously returns

error: called object is not a function

The .o files are my source code files (I was instructed to put my source code in files with extension .o.) What do I do to compile this?

vaindil
  • 7,536
  • 21
  • 68
  • 127
  • With regard to what specifically? The idea of putting source code directly into `.o` files? That seemed incredibly strange to me, especially since `gcc` doesn't recognize `.o` files, but that's why I came to you guys! – vaindil Sep 13 '13 at 02:32
  • 2
    You cannot compile `.o` files, you can link them. Same goes for `#include`: it goes into `.c` files, not in `.o` files. – Sergey Kalinichenko Sep 13 '13 at 02:32
  • You need to create a header that contains declarations of the functions available in `modules.c` that you can `#include` in `main.c`. Then compile the `modules.c` and `main.c` files together (omitting the `.o` stage) with `gcc main.c modules.c`. – dreamlax Sep 13 '13 at 02:34
  • 2
    @StevenH. `.o` files are what `.c` files are often compiled into. You should not name your files with a `.o` extension if they contain C source code. – dreamlax Sep 13 '13 at 02:34
  • @dreamlax That's exactly what I thought, but I didn't know if I was missing something stupid. Just checked and it does explicitly say to name the files with `.o` extensions. – vaindil Sep 13 '13 at 02:36
  • 1
    @StevenH: That's a very strange requirement indeed! – dreamlax Sep 13 '13 at 02:37
  • 1
    @dreamlax That's why I'm just going to do what you guys say! :D – vaindil Sep 13 '13 at 02:38
  • Read the [*Modern C*](https://modernc.gforge.inria.fr/download.html) book – Basile Starynkevitch May 19 '20 at 08:21

4 Answers4

40

If you have your two source files, you can compile them into object files without linking, as so:

gcc main.c -o main.o -c
gcc module.c -o module.o -c

where the -c flag tells the compiler to stop after the compilation phase, without linking. Then, you can link your two object files as so:

gcc -o myprog main.o module.o

This is all perfectly normal behavior, you'll usually get your makefile to compile things separately and link them at the end, so you don't have to recompile every single source file every time you change one of them.

Talking about main.o "calling functions in" module.o is perfectly fine, but an .o file is not a source file, it's a compiled object file. If "put my source code in files with extension .o" actually meant "compile my source code into files with extension .o" then the situation would make a whole lot more sense.

Crowman
  • 25,242
  • 5
  • 48
  • 56
24

You should define the functions that you want to call from modules.c into main.c into a header file, let us say modules.h, and include that header file in main.c. Once you have the header file, please compile both of the files together: gcc main.c modules.c -o output


Two additional notes. First, modules.o is an object file and it should not be included in a C source file. Second, we cannot have a C file have a .o extension. You should actually get an error when compiling a .o file. Something like:

$ cat t.o
int main() {
    int x = 1;
    return 0;
}
$
$ gcc t.o
ld: warning: in t.o, file is not of required architecture
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$
Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45
Manoj Pandey
  • 4,528
  • 1
  • 17
  • 18
  • 2
    You certainly shouldn't include `.o` files in C source files, which would just be really weird, and gcc won't *compile* `.o` files, but your command line in your example would work fine if there was a `main` function in it. If you have one source file, then `gcc -o main.o main.c -c`, and then `gcc main.o` will just link and create your `a.out` executable. – Crowman Sep 13 '13 at 02:55
  • @PaulGriffiths, Here is the kicker: I did have main() in the t.o file :-). – Manoj Pandey Sep 13 '13 at 03:11
  • 1
    Yes, I meant that gcc will behave properly with that command line if you have an actual object file with `main()` defined in it, rather than a source file with a misleading extension! – Crowman Sep 13 '13 at 03:13
  • AND WHAT IF I HAVE 60 SOURCES FILES? – Paul Bénéteau Feb 22 '20 at 16:30
3
program: main.o 
    gcc -o main main.c anotherSource.c

This works for me.

Farrukh
  • 61
  • 3
2

You should be including .h files which are "headers". So if your main file is using modules then you should include module's header file.

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103