473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

redefinition

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
Nov 14 '05 #1
9 2796
"john hrdo" <hr**@myrealbox.com> wrote in message
news:36**************************@posting.google.c om...
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
Nov 14 '05 #2
hr**@myrealbox.com (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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Why are we here?
whrp
Nov 14 '05 #3
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.)

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
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
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".
Nov 14 '05 #4
Derk Gwen wrote:

hr**@myrealbox.com (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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5
In <40********@mk-nntp-2.news.uk.tiscali.com> "Peter Pichler" <pi*****@pobox.sk> writes:
"john hrdo" <hr**@myrealbox.com> wrote in message
news:36**************************@posting.google. com...
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.


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.
(Normally, the compiler complains about redefinition.)


It's most likely the linker that complains, not the compiler.


No one complained on my system. Should I ask for a refund? ;-)
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
Peter Pichler wrote:
"john hrdo" <hr**@myrealbox.com> wrote in message
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.


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
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.

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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c0***********@sunnews.cern.ch...
In <40********@mk-nntp-2.news.uk.tiscali.com> "Peter Pichler" <pi*****@pobox.sk> writes:
"john hrdo" <hr**@myrealbox.com> wrote in message
news:36**************************@posting.google. com...
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.


Are you sure? Let's try:


<example snipped for brevity>
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.
(Normally, the compiler complains about redefinition.)


It's most likely the linker that complains, not the compiler.


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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lomat | last post by:
Hello, While compiling a file, I get following error .... ================================= /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/type_traits.h:14 2: redefinition of...
1
by: A | last post by:
Hi, I created an array of user defined objects dynamically in a class Foo's constructor. class Foo{ Foo(){ Bar* b = new Bar(); }
1
by: squallions | last post by:
Hi I doing my c++ homework and stuck on error 'class' type redefinition. I have 5 classes. First class is base class. The next two classes are derived from the first class. The next two...
3
by: cody | last post by:
Hello fols. I'm doin a school project and have this stupid problem. We're on virtual functions and have to seperate out each class into a file. There are 9 classes and so 9 .h and .c files. the...
1
by: Bonj | last post by:
Hello I am trying to compile a dll with the below code used to search through an array of node structures. I am getting the following warning unfortunately: //wordsmain.c #include <windows.h>...
4
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see...
1
by: Alex | last post by:
Hello all, I have a very stupid problem that is driving me crazy...so plz if anyone ever saw this, I would like him to help me :) I have static MFC application in MSVC++ 6.0 (named Example)....
2
by: Mohammad Omer | last post by:
Hi, i am developing an application which uses WAB API's, for doing all this i am using vs2k5. I have wab.h header file included in my project to use WAB api's but after compilation one error...
9
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.