Linking two files with 2 mains using objcopy

OP (Company: self) #3855787
Hi Experts,
I am working in a project to link two files with two mains.
i.e file 1 is having main()
file2 is having main()

Like this:
File1:
int main(){
printf("I am from main 1\n");
return 0;
}

File2:
int main(){
printf("I am from main 2\n");
return 0;
}
Compiling and linking with the help of makefile.

I know this is not possible since linker will throw when it sees 2 mains 
which results in error "Multiple definition".

I tried one way using objcopy to redefine symbols in one file .
i.e file2 sybmols is renamed to main2__main using
"objcopy --prefix-symbols main2_ main2.o".

But after doing this in linker stage it is giving error undefined symbol
main2__main. Why this is coming?
Any one can give there expert opinion?

If any body has a similar makefile can you please share it?
Is there any other alternative way? I don't want to use #ifdef ...#endif 
for any of files since I want this to happen @ at link time.

Regards
opt
Guest #3858106
Why do you have two main() routines?
What do you want to achieve with this?
Why don't you just rename one of them into main2()?
Do you realize that the C runtime can only call one main(), so having 
two main()s is doesn't even make sens?

Reply

A question of your own? This thread is over 12 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.