CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Libraries with the same function

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,721

    Libraries with the same function

    Let's assume I'm building a program which links to 2 libraries:- x.lib and y.lib

    And let's assume they both libs happen to contain a function with the same signature:- int a_func(void);

    If I'm lucky, they'll both do exactly the same thing - but what if they didn't? Is there any way to stipulate which library to use for that particular function? For example, if I preferred to use the version from y.lib could I specify that simply by placing y.lib earlier in my list of link libs?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,468

    Re: Libraries with the same function

    If using C++, use different namespaces.

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,465

    Re: Libraries with the same function

    Use LoadLibrary()/GetProcAddress() to specify which one to use.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.3)

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,721

    Re: Libraries with the same function

    Thanks guys - my original description was a bit simplified so let me explain what's actually happening...

    I'm building some 3rd party DLLs which still use module definition files, rather than __declspec. In one of the DLLs (for some strange reason) the builds complete okay - but an optimized Release build always fails to export one of the functions. It reappears if I build without optimization but of course, the runtime performance is then sluggish. It's as if the optimization decides that function isn't needed and filters it out.

    So I wondered what'd happen if I build an optimized version and also an unoptimized one - then just link to both of them. e.g. if I place the optimized one first (in the linker's list) will that give it priority and just use the unoptimized library for the missing function? I've tried it here and it does seem to work as expected - i.e. the 'lost' function does get found and yet the runtime performance doesn't seem compromised. But I just wondered if that's all a happy accident!!
    "A problem well stated is a problem half solved. - Charles F. Kettering

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,465

    Re: Libraries with the same function

    Where was the constellation of the planets and what was the phase of the moon when this event took place?

    I'm assuming because of the previous posts, that the function that isn't exported has the same name as one that is exported from elsewhere?

    As you're using a .def file, can't you change the definition name of the function not exported to a unique one - and change the name in the code where it is used. That way you get all unique names.

    Sorry I can't help much more. We stopped using .dll's some years ago as we concluded they are more trouble than they are worth.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.3)

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,721

    Re: Libraries with the same function

    Quote Originally Posted by 2kaud View Post
    As you're using a .def file, can't you change the definition name of the function not exported to a unique one - and change the name in the code where it is used
    No, that's not the problem.... I was thinking more about all the other functions (i.e. the hundreds of functions that'll be common in both DLL's). I wondered if there's some way to guarantee that the VC linker would favour the ones in the optimized DLL. It seems to be doing that anyway - but I'm just not sure why...

    Quote Originally Posted by 2kaud View Post
    We stopped using .dll's some years ago as we concluded they are more trouble than they are worth.
    VS2019 sure is !!
    Last edited by John E; February 28th, 2021 at 07:55 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)