Reputation: 1
I am trying to write a program that will play a game using strategies that I can easily modify and compile in multiple directories. Most of the source files of the game are immutable (i.e. they remain the same even when strategies are altered). Say my source files are a.c, b.c, c.c, d.c, e.c
, where a.c
through d.c
are immutable and e.c
is the only file that varies between different strategies. In addition, a.c
makes a call to e.c
, so they can't be fully compiled independently without invoking an undefined function error.
Is there any way to compile (cc/gcc/etc.) a.c
through d.c
into a single file, everything.o
, that I can then compile with the missing e.c
?
> <command here> everything.o a.c b.c c.c d.c
> cc -g -o e.o e.c
> cc -g -o foo.o everything.o e.o
where I can copy everything.o into different directories so that I can have programs to run different strategies, without copying all four (in my case, ~20) files into each directory?
Upvotes: 0
Views: 103
Reputation: 14193
You can compile each of the objects individually and add them to an archive
:
gcc -c foo.c bar.c baz.c
ar cr shared.a foo.o bar.o baz.o
And then finally compile with your extra source file:
gcc extra.c shared.a -o myprogram
See Creating a library with the GNU archiver for more information.
Upvotes: 5