redefinition

J

john hrdo

Hey,


This might well be a naive question, please be patient.

Is it possible to compile a C program including two
libraries lib1 and lib2 which have both a function with same
name, e.g. functA? (Normally, the compiler complains about
redefinition.)

How would it be done? How would I call functA specifically
from lib1?


Thanks!


john
 
P

Peter Pichler

john hrdo said:
Is it possible to compile a C program including two
libraries lib1 and lib2 which have both a function with same
name, e.g. functA?
No.

(Normally, the compiler complains about redefinition.)

It's most likely the linker that complains, not the compiler.
How would it be done? How would I call functA specifically
from lib1?

You can't. There are ways around it, but not for the feint-hearted.
By far the easiest way is to call your functions different names.
You can also declare them as static and hide them behind structures,
but why?

Peter
 
D

Derk Gwen

(e-mail address removed) (john hrdo) wrote:
# Hey,
#
#
# This might well be a naive question, please be patient.
#
# Is it possible to compile a C program including two
# libraries lib1 and lib2 which have both a function with same
# name, e.g. functA? (Normally, the compiler complains about
# redefinition.)

You might be able to temporarily divert the declaration elsewhere
#include "lib1.h"
#define functA HIDE_THIS_functA
#include "lib2.h"
#undef functA

Also the linker won't like two externals with the same name. If you have
references to functA in lib2 that need to go to lib2, you might have
to build a special library that creates a single monolithic module that
does not export functA. Or other obscure linker skuldrudgery. It's not
going to be straightforward or portable.

Best technique is to get the source code to one library or the other,
rename all exported symbols so they have a distinguised prefix, and then
drive by the programmer's house and throw rocks through his windows.
 
J

Julian V. Noble

john said:
Hey,

This might well be a naive question, please be patient.

Is it possible to compile a C program including two
libraries lib1 and lib2 which have both a function with same
name, e.g. functA? (Normally, the compiler complains about
redefinition.)

How would it be done? How would I call functA specifically
from lib1?

Thanks!

john

You might want to use pointers to functions. It's not exactly
redefinition, per se, but if you define two functions and
let a telegraphically named pointer point to one or the other,
that might accomplish what you are trying to do.

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything and thereby take away
our free will and that share of glory that rightfully belongs
to us." -- N. Machiavelli, "The Prince".
 
J

Joe Wright

Derk said:
(e-mail address removed) (john hrdo) wrote:
# Hey,
#
#
# This might well be a naive question, please be patient.
#
# Is it possible to compile a C program including two
# libraries lib1 and lib2 which have both a function with same
# name, e.g. functA? (Normally, the compiler complains about
# redefinition.)

You might be able to temporarily divert the declaration elsewhere
#include "lib1.h"
#define functA HIDE_THIS_functA
#include "lib2.h"
#undef functA

Also the linker won't like two externals with the same name. If you have
references to functA in lib2 that need to go to lib2, you might have
to build a special library that creates a single monolithic module that
does not export functA. Or other obscure linker skuldrudgery. It's not
going to be straightforward or portable.

Best technique is to get the source code to one library or the other,
rename all exported symbols so they have a distinguised prefix, and then
drive by the programmer's house and throw rocks through his windows.
Linkers don't complain much. In resolving externals, the linker will
find a match with the first library entry it finds and then stop looking
for that one. Once the linker finds functA in any of the libraries, it
stops looking for it and will not know whether functA occurs in any
subsequent libraries.
 
D

Dan Pop

In said:

Are you sure? Let's try:

fangorn:~/tmp 377> cat lib1.c
#include <stdio.h>

void functA(void) { puts("I'm functA from lib1"); }
fangorn:~/tmp 378> gcc -c lib1.c
fangorn:~/tmp 379> ar -r lib1.a lib1.o
fangorn:~/tmp 380> cat lib2.c
#include <stdio.h>

void functA(void) { puts("I'm functA from lib2"); }
fangorn:~/tmp 381> gcc -c lib2.c
fangorn:~/tmp 382> ar -r lib2.a lib2.o
fangorn:~/tmp 383> cat test.c
void functA(void);

int main()
{
functA();
return 0;
}
fangorn:~/tmp 384> gcc test.c lib1.a lib2.a
fangorn:~/tmp 385> ./a.out
I'm functA from lib1
fangorn:~/tmp 386> gcc test.c lib2.a lib1.a
fangorn:~/tmp 387> ./a.out
I'm functA from lib2

So, the right answer is: it depends on your implementation. Mine was
certainly happy with two libraries defining the same function.
It's most likely the linker that complains, not the compiler.

No one complained on my system. Should I ask for a refund? ;-)
You can't. There are ways around it, but not for the feint-hearted.

My example above clearly shows that you can and even the faint of heart
can do it. So, the right answer is, again: it depends on your
implementation.
By far the easiest way is to call your functions different names.
You can also declare them as static and hide them behind structures,
but why?

The usual reason is that you have to use two libraries defining the same
function, without having control over the source code of any of them.

If the linker is less tolerant than the typical Unix linker, there may be
no solution to this problem, short of editing the binary of one of the
libraries, to alter the function name.

Dan
 
D

Dan Pop

In said:
Linkers don't complain much. In resolving externals, the linker will
find a match with the first library entry it finds and then stop looking
for that one. Once the linker finds functA in any of the libraries, it
stops looking for it and will not know whether functA occurs in any
subsequent libraries.

Can you provide a chapter and verse for this? I thought it was a case of
undefined behaviour.

As far as my experience is involved, non-Unix linkers tend to complain
about redefinitions of library defined symbols. The OP's one certainly
did.

Dan
 
C

CBFalconer

Peter said:

Yes. However it depends on your linker or other system software,
and is thus OT here, as not being language dependant. You should
normally search lib1 first to use the functA stored therein, after
which the linker will stop searching for it. With gcc that will
mean things such as -l1 -l2 in the command line.
 
J

Joe Wright

Dan said:
Can you provide a chapter and verse for this? I thought it was a case of
undefined behaviour.

As far as my experience is involved, non-Unix linkers tend to complain
about redefinitions of library defined symbols. The OP's one certainly
did.
You're probably right. My observation is strictly annecdotal. The last
linkers I was intimately with were L80 and LINK and SLRNK for i8080/z80
machines under CP/M.
 
P

Peter Pichler

Dan Pop said:
In <[email protected]> "Peter Pichler"

Are you sure? Let's try:

So, the right answer is: it depends on your implementation. Mine was
certainly happy with two libraries defining the same function.

Thank you. This was new to me. One learns something new every day.
No one complained on my system. Should I ask for a refund? ;-)

I personally believe that you should. But I don't think that my personal
beliefs count ;-)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top