none
Problem with linking two static library with same function

    Question

  • Dear All,
     

    Problem Statement:

    I have an exe (windows console application) which imports 2 static libraries (let say Lib1 and Lib2).

    Lib1 has 2 functions

    • int Add(int a,int b)
    • int Sub(int a, int b)

    Lib2 has 2 functions

    • int Add(int a,int b)
    • int Div(int a, int b)

    This exe calls Add() which is present in both the libraries. When I build the exe then I get the error àfatal error LNK1169: one or more multiply defined symbols found”

     The problem is the library is 3rd party and so i cant change the name of the functions. please suggest me some workaround.

    Thanks in advance.

    Regards,
    Jeevanand.R

    Monday, September 22, 2008 6:48 AM

Answers

  • One solution is to wrap one or both of these static libraries in a dynamic library (.dll). Then you can change the exported names, e.g.

    // link to Lib1.lib
    int Add1(int a, int b) // exported
    {
      return Add(a, b);
    }

    int Sub1(int a, int b) // exported
    {
      return Sub(a, b);
    }

    David Wilkinson | Visual C++ MVP
    Monday, September 22, 2008 11:04 AM
  • Add
    /FORCE:MULTIPLE
    option in the Project property > Linker > Command Line.

    Though it will build but may not work as expected on runtime. Anyway worth trying.

    Javed

    Software Engineer, India
    Tuesday, September 23, 2008 9:00 AM

All replies

  • One solution is to wrap one or both of these static libraries in a dynamic library (.dll). Then you can change the exported names, e.g.

    // link to Lib1.lib
    int Add1(int a, int b) // exported
    {
      return Add(a, b);
    }

    int Sub1(int a, int b) // exported
    {
      return Sub(a, b);
    }

    David Wilkinson | Visual C++ MVP
    Monday, September 22, 2008 11:04 AM
  • hi davelik,

    What if the function definitions are different in the two cases. I mean is there a compiler option for this???

    Thanks and regards,
              Rahil
    Tuesday, September 23, 2008 6:35 AM
  • Add
    /FORCE:MULTIPLE
    option in the Project property > Linker > Command Line.

    Though it will build but may not work as expected on runtime. Anyway worth trying.

    Javed

    Software Engineer, India
    Tuesday, September 23, 2008 9:00 AM
  • rahilmaniyar said:

    hi davelik,

    What if the function definitions are different in the two cases. I mean is there a compiler option for this???

    Rahil:

    I was assuming they were different. But what difference does it make if you have moved one (or both) of them to a DLL with different exported names?



    David Wilkinson | Visual C++ MVP
    Tuesday, September 23, 2008 12:38 PM
  • hi Davelik,

    they aren't. It's like this:

    I am linking my application with two libraries: lib1 and lib2.
    lib1 is an abstraction layer over lib3 (another library). The lib3 functions should be called only through lib1 function calls.
    The problem is lib2 and lib3 have same function name/signatures.
    In dynamic linking, we can use .def file for lib1 to tell that only the wapper functions can be called and the direct functions of lib3 are not exposed to the application. So, when a call is made to some function say add(), although it's definition is present in lib3 but it is not directly exposed to application, they can be called only through function calls like add1 in lib1), the application will find only one function definition in lib2.
    But in static linking, the function definition in both lib2 and lib3 are visible to application, so there is multiple function definition error. Please tell how do we handle this in static linking? Is there anything similar to .def filefor static linking or any oter solution to this.

    Thanks and regards,
             Rahil
    Thursday, September 25, 2008 6:29 AM

  • Hi Rahil,

    As far as i know, .def file for static linking is not allowed, however, I think the solution of Javed and David are worth trying.

    Thanks,
    Rong-Chun Zhang

    Please mark the replies as answers if they help and unmark if they don't.
    Friday, September 26, 2008 9:45 AM