I have two numerical maths libraries given by DLLs (I am under Windows) and header files. (I can't modifiy the headers and anyway there are dozens of them. I also don't have have access to a code that I could compile to obtain the DLLs.)

The libraries are the same in the following sense : they are both without any namespace (except the global one) and all declarations are the same : if a function is in one library it is also in the other with same signature and return type, constness or other "qualifiers" or anything included -- exporting for instance -- memory layout for return objects etc, same for classes, structs, enums, variables etc etc. (One could imagine for instance, even if it is not the case, that both libraries are just different versions of the same library with only internal differences.)

So far, for legacy reasons, I was using these libraries in different visual studio solutions but now I need to use both libraries in the same file. (There is some function f with better performance in one library and other function g with better performance in the other library and I need to use f and g let's say in the definition of a function I am implementing.)

Of course, if in code I do a #include "headerfromlib1.h" followed with #include "headerfromlib2.h", I immediately have clashes.

I can't imagine that there is no solution to my problem but so far I don't see any. (I would prefer an OS-independent solution as I am not expert at all in the C++ Windows "layer" even if needed I wouldn't be against using it.)

7 Replies 7

Seems the biggest issue you have is that headerfromlib1.h and headerfromlib2.h don't have namespaces (or at least versioned namespaces). OR are you using "using namespace" in your own headers?
Do the dll's at least have different filenames? If so you might try to write 2 dll's of your own. In each dll give your dll a header file WITH namespace that has function declarations matching your original functions ... then forward to the original dll in implementation (and in those dll's you can include the headerfromlib1.h). In the end you pay a small price for 2 extra (small) dll's and a little overhead for each call.

Note you flagged your question as "best practice", but this is kind of a full question. The best practice would have been for headerfromlib1.h to have used namespaces. (Which still is an issue if you're using "C" libraries which don't have them)

If can't modify the source, then the easiest solution is to invoke "LoadLibrary" directly on the DLL and "GetProcAddress" to obtain the exported function you seek at runtime instead of directly linking with these libraries

Now if these functions are name mangled instead of exported with extern "C", it gets a little messier.

What's the function signature you are trying to invoke?

@selbie I don't know if that really is the most easy, I still think having conflicting header files is the most important issue. Maybe having two static libs and pimpl pattern may also work

Create wrapper libraries, or using the LoadLibrary way seems the only ways if you cannot modify your external libraries.

It would be great to cherry pick some representative examples (of conflicting functions), so solution could be tailored to something more specific then general description. Also clearly state that both libraries are pure c++ not c. Many numeric libraries are C libraries and this is an important detail. If library is publicly available you should provide name of it.

I cannot comment anymore on your answers nor on my question, what the h is wrong with this site ? I guess it is because the type of question I chose to be "best practice" ? (I see other questions kept their "normal" appearance) If so, could someone alert a moderator who to please change the type to "vanilla question" type ? (I can't with "edit".) Thanks.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.