Thread: Compilation and Execution issue

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Exclamation Compilation and Execution issue

    Hi,

    I am facing a strange issue in C programming. I have a function which is defined in two different libraries. It has difference in number of arguments and functionality.

    Now i have taken reference of the function in and used in new program file, which has included both the libraries.
    To my surprise it is compiling OK without any warning. As in C we know there can not be name mangling.

    While execution it is showing undefined behavior also. Somewhere it calls exact function of same argument and sometimes with different.

    Need experts help on this.
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am not surprised that you did not get compile errors: as long as you included the header with the declaration of the function that you are actually using in a given source file, there is no problem. What surprises me is that you did not get a linking error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    S_cess is waiting for u. Go Ahead, put u there.
    Don't we have to spell it successfully, first?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    You See
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    See, Sea, and C.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if the functions are both in libraries, the linker may pull in the first one it finds and ignore the other one. you would have to examine the link map to see which one you got.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Seems interesting! How can see the link map?
    S_ccess is waiting for u. Go Ahead, put u there.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what tools are you using. if Visual Studio, check the linker option to generate a map file. if GCC, use the 'nm' binutil on the executable to get a link map. in both cases just search for your function name in the map.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    nm shows me something like:
    [2358] | 3974288| 0|FUNC |GLOB |0 |UNDEF |FunctionName. I can not map it with library from which it is!
    S_ccess is waiting for u. Go Ahead, put u there.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you had a file called foo1.c, containing foo(int,int) and you compiled it to lib1.a

    Then you had a file called foo2.c, containing foo(char*) and you compiled it to lib2.a

    Then doing something like
    gcc prog.c -l1 -l2
    to link against both libraries will just result in your program seeing foo(int,int), as that was the first library containing a foo() function.

    You can pretent to call foo("hello") if you want, but you're still going to get the foo(int,int) function dealing with it (garbage parameters and all).

    So long as each instance of foo() originated from a separate compilation unit, then the linker will naturally omit the 2nd instance of foo() in the second library. This is exactly what it is supposed to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Quote Originally Posted by Salem View Post
    So long as each instance of foo() originated from a separate compilation unit, then the linker will naturally omit the 2nd instance of foo() in the second library. This is exactly what it is supposed to do.
    Is there any way to avoid such type of mistakes while compiling or linking??
    S_ccess is waiting for u. Go Ahead, put u there.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A common way is to use a naming prefix for each library, so foo(int,int) in library 1 would be one_foo(int) and foo(char *) in library 2 would be two_foo(char *). That way you don't have two functions with the same name, and you know what library provides that function.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh sure! Get organized and cheat, why don't ya? < RoFL >

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    .. Thanks a lot guys! I have Understood the basic problem. using "ldd" i have also traced out the sequence problem.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overloading compilation issue in GCC
    By redneon in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2011, 07:53 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. Halting execution
    By rickyoswaldiow in forum C++ Programming
    Replies: 1
    Last Post: 10-19-2006, 07:10 AM
  4. Script execution
    By cboard_member in forum Game Programming
    Replies: 3
    Last Post: 08-16-2006, 02:29 AM
  5. Brakes in execution
    By cnoob in forum C++ Programming
    Replies: 8
    Last Post: 06-06-2005, 07:40 PM