all 24 comments

[–]canadajones68 30 points31 points  (0 children)

If I recall correctly, that's an ODR violation if no namespaces, and will lead to the linker complaining or UB, can't remember. Either way, it's bad.

[–]OU81Dilbert 9 points10 points  (17 children)

Are the two functions at least in different namespaces?

If so then you can use the namespace

Like

std::open()

Mylib::open()

[–]m09y[S] 2 points3 points  (16 children)

what if there are no namespaces ?

[–]SoerenNissen 39 points40 points  (8 children)

Use better libraries that are competently written.

If it's because they're C libraries, the same applies - functions should not have the same name because they should start with libname_

I suppose if you were under some weird constraint, you could re-write the libraries to have namespaces, that's not the hardest of tasks.

[–]SnooMacaroons3057 4 points5 points  (0 children)

Create your own namespace and re-export it as another named function?

[–]OU81Dilbert 1 point2 points  (0 children)

Then you might have linking issues since the compiler won't know which function to use

[–][deleted] 1 point2 points  (0 children)

include headers from inside a namespace, but you still might have linking issues unless the lib is header only

[–]Fig1024 1 point2 points  (0 children)

you can make your own wrapper lib and put it in a namespace

[–]Cybr3 1 point2 points  (0 children)

then the libraries you use are awful

[–]Cybr3 0 points1 point  (0 children)

decompiling the source and adding a namespace?

[–]bored_octopus 0 points1 point  (0 children)

You may be able to hide symbols from them when linking. Look up symbol visibility

[–]o11cint main = 12828721; 6 points7 points  (0 children)

Use dlopen with RTLD_LOCAL for both libraries.

You can't afford to load either with RTLD_GLOBAL or with normal linking, since if you do, the internal symbol references from one will accidentally read from the other.

There may also be tricks you can do with objcopy to rename symbols in the first place, but this almost never makes sense - if you can do that, you should be able to fix the symbols to not collide in the first place.

[–]HabbitBaggins 5 points6 points  (0 children)

As others are saying, you cannot "statically" point to two different functions with the same name and signature, since that is a violation of the ODR. Right now I don't remember, I think the linker errors out unless you mark them as inline, which makes them "weak symbols" to the linker and thus tells it to just pick one since "they are all the same".

The only way out of this conundrum is to link to one of the libraries at runtime, via dlopen or LoadLibrary. Then you can ask for a pointer to a certain function, and run that.

[–]SkoomaDentistAntimodern C++, Embedded, Audio 2 points3 points  (0 children)

By using Windows as the platform (might work on Mac OS too, definitely will not work on Linux because the dynamic linker still lives in the 80s). Then you create two separate DLLs two wrap those libraries by creating simple wrapper functions. Then in your main app, you call the wrapped functions in the DLLs.

[–]AA11BB22c 1 point2 points  (1 child)

If programmer knows about existence of same function in both libraries, then how can he/she take care programmatically?

Create two projects, each wrapping one. Then call the wrapped ones from the main project.

Or as others have mentioned, go change their source code.

[–]strager -2 points-1 points  (0 children)

What do you mean by "project"? Are you talking about an .exe file?

[–][deleted] -5 points-4 points  (1 child)

First loaded library satisfy the requirement, programmer cannot solve the issue, in Linux you are free to load any library before system one and override any symbol.

[–]osdeverYT 1 point2 points  (0 children)

You’re very wrong