0

I have a compiled C file, called Hello.o. in Hello.o: I have a main function and a function called int myfunc().

I wonder if I were to create a new file, hello2.c that contains a main function as well, and declare myfunc at the top of hello2.c, will i be able to compile hello2.c and link hello.o to it using a gcc command?

Thanks all in advance.

roy11911
  • 11
  • 4

2 Answers2

0

If you want to just use myfunc() in hello2.c you can try linking the two objects files using gcc.Declare the function in a header file Hello.h and include it in hello2.c and generate Hello.o and hello2.c before linking them together by $ gcc -o output Hello.o hello2.o

I think this should help you

How do I link object files in C? Fails with "Undefined symbols for architecture x86_64"

Community
  • 1
  • 1
c_mnon
  • 366
  • 4
  • 14
0

Object files are linked completely, or not at all. So this won't work.

GCC adds all the files specified in the command line as .o to the binary. Then the libraries (.a) are used to find needed symbols.

If there are duplicate symbols, an error is reported. (It doesn't know which main).

If a library contains more than one .o file, it can ignore the .O files which are not required. These may have duplicates with the binary.

mksteve
  • 12,614
  • 3
  • 28
  • 50