circular dependencies between dlls with visual studio

前端 未结 9 1121
忘掉有多难
忘掉有多难 2020-12-15 08:00

I have a circular dependency between two functions. I would like each of these functions to reside in its own dll. Is it possible to build this with visual studio?



        
相关标签:
9条回答
  • 2020-12-15 08:24

    The only way you'll get around this "cleanly" (and I use the term loosely) will be to eliminate one of the static/link-time dependencies and change it to a run-time dependency.

    Maybe something like this:

    // foo.h
    #if defined(COMPILING_BAR_DLL)
    inline void foo(int x) 
    {
      HMODULE hm = LoadLibrary(_T("foo.dll");
      typedef void (*PFOO)(int);
      PFOO pfoo = (PFOO)GetProcAddress(hm, "foo");
      pfoo(x); // call the function!
      FreeLibrary(hm);
    }
    #else
    extern "C" {
    __declspec(dllexport) void foo(int);
    }
    #endif
    

    Foo.dll will export the function. Bar.dll no longer tries to import the function; instead, it resolves the function address at runtime.

    Roll your own error handling and performance improvements.

    0 讨论(0)
  • 2020-12-15 08:26

    I deeply sympathise with your situation (as clarified by your edit), but as a firm believer in doing the correct thing, not the thing which works for now, if there's any possibility at all I think you need to refactor these projects.

    Fix the problem not the symptom.

    0 讨论(0)
  • 2020-12-15 08:27

    It's possible to use the LIB utility with .EXP files to "bootstrap" (build without prior .LIB files) a set of DLLs with a circular reference such as this one. See MSDN article for details.

    I agree with other people above that this kind of situation should be avoided by revising the design.

    0 讨论(0)
  • 2020-12-15 08:27

    It is not possible to do cleanly. Because they both depend on each other, if A changes, then B must be recompiled. Because B was recompiled, it has changed and A needs to be recompiled and so on.

    That is part of the reason circular dependencies are bad and whether you want to or not, you cannot leave that out of the discussion.

    0 讨论(0)
  • 2020-12-15 08:29

    Visual Studio will enforce the dependencies, generally speaking, since function addresses may change within the newly-compiled DLL. Even though the signature may be the same, the exposed address may change.

    However, if you notice that Visual Studio typically manages to keep the same function addresses between builds, then you can use one of the "Project Only" build settings (ignores dependencies). If you do that and get an error about not being able to load the dependency DLL, then just rebuild both.

    0 讨论(0)
  • 2020-12-15 08:31

    The reason it works on Unix-like systems is because they perform actual linking resolution at load time. A shared library does not know where its function definition will come from until it's loaded into a process. The downside of this is that you don't know either. A library can find and call functions in any other library (or even the main binary that launched the process in the first place). Also by default everything in a shared library is exported.

    Windows doesn't work like that at all. Only explicitly exported things are exported, and all imports must be resolved at library link-time, by which point the identity of the DLL that will supply each imported function has been determined. This requires an import library to link against.

    However, you can (with some extra work) get around this. Use LoadLibrary to open any DLL you like, and then use GetProcAddress to locate the functions you want to call. This way, there are no restrictions. But the restrictions in the normal method are there for a reason.

    As you want to transition from static libraries to DLLs, it sounds like you're assuming that you should make each static library into a DLL. That's not your only option. Why not start moving code into DLLs only when you identify it as a self-contained module that fits into a layered design with no circularity? That way you can begin the process now but still attack it a piece at a time.

    0 讨论(0)
提交回复
热议问题