0

I have a C++ project in Visual Studio where libs (oldVersion.lib, newVersion.lib) are added via project properties -> Linker -> Input -> Additional Dependencies.

Both libs contain the same function (e.g. foo()). The functions are included by their header files and called via ::foo().

The code itself does not show an error but at runtime it is not clear if foo() from oldVersion or newVersion is executed.

Is it possible to differentiate between same named functions from the two different .libs? (Like aliases in C#)

What I tried:

  • Wrapping external libraries in namespaces
    Not working because in line coollib_coolthing(howcool); it is not clear where howcool (which would be foo) comes from.

  • namespace newVersion{
    #inlcude "newVersion.h"
    }
    

    Here, the newVersion namespace is added to the information from the .h file but newVersion::foo() still executes the first foo it can find somewhere.

karleOtto
  • 3
  • 4
  • I am afraid that there is no way to distinguish directly. I suggest that you could encapsulate one of the static libraries. Create a new static library and wrap the existing static library. You could refer to this [link](https://stackoverflow.com/questions/678254/what-should-i-do-if-two-libraries-provide-a-function-with-the-same-name-generati). – Barrnet Chou Jul 06 '20 at 07:55
  • Thanks @BarrnetChou for the reply. That seems to be a working and easy way to do it. I'll give it try. – karleOtto Jul 16 '20 at 07:53
  • If there are any questions during your attempt, welcome to ask again. I am pleased to help you. – Barrnet Chou Jul 16 '20 at 08:29
  • The comments in your referenced solution state [the problem](https://stackoverflow.com/a/678312/13870112) I am running into, atm. I can now call `wrapper::foo()` from wrapper.lib but as the new lib links the old ones, the linker again has to decide between `foo()` functions from original .libs at some point. Do you know how I can achieve "The wrapper [...] should not export the offending symbol" ? – karleOtto Jul 23 '20 at 14:41

1 Answers1

0

I am sorry for that I should provide a better plan rather than a complicated method.

Here is the method: Just tell the linker that the symbols in a library are known by some other name and use that name. You need to make a def file and provide the name translation in the EXPORTS section. The process would be to make a def file for one of the libraries then use this def file to build a lib file and then link with that lib file.

Also, you could refer to this link for more information about this method which is provided by Jim Monte.

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7
  • Thanks, that worked very well. Additionally, my .lib linked a .dll. After changing the functions names in the .lib they weren't found in the .dll anymore. I ended up opening the .dll with an editor and changed its names (important: new names have to be of same length than original name) there. Then [created a .lib from .dll](https://stackoverflow.com/a/16127548/13870112). – karleOtto Jul 27 '20 at 18:16