So I have several files for a University Project I'm working on; each file has several functions and a main
function for testing purposes. However some of the files need to use functions from the others which gives linker complaints about multiple main
declarations as to be expected. I was wondering if there was a way to compile these files to an object file without compiling the main
function along the way, basically stripping out main
to leave only the usable functions.
An example (not one of my actual files which are quite large), for each I'm assuming the existence of a header file which contains a declaration of foo
and bar
respectively:
//File foo.c
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int foo(int x, int y)
{
return x + y;
}
//Assumes the user gives 2 integer inputs
int main(int argc, char **argv)
{
int x, y;
sscanf(argv[1], "%d", &x);
sscanf(argv[2], "%d", &y);
printf("foo(%d, %d) = %d\n", x, y, foo(x,y));
}
//File bar.c
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
#include "bar.h"
int bar(int x, int y, int z);
{
return foo(x, y) * z;
}
//Assums the user gives 3 integer inputs
int main(int argc, char **argv)
{
int x, y, z;
sscanf(argv[1], "%d", &x);
sscanf(argv[2], "%d", &y);
sscanf(argv[3], "%d", &z);
printf("bar(%d, %d, %d) = %d\n", x, y, z, bar(x, y, z));
}
Is it possible to compile foo.c
to an object file so that I could call gcc bar.c foo.o
and get an executable for bar.c
?