How do I set up a multi-project solution in C++?

Beijerinc

I'm new to C++ and I'm having a hard time getting my dll references to work. I've been trying to get it to work for a couple of days, but the few explainations I've found often refer to doing x or y, but don't tell me how to do x or y. Since I'm not a C++ veteran, I need someone to walk me through it. What I want to do is the following:

MySolution
    MyExe (Win32 .exe)
        Main.h
        Main.cpp
            (constructs ImplementationB calls the methods as defined by InterfaceA, then deletes the instances)
            (calls/fills HelperC.Foobar)
    MyInterfaces (dll)
        InterfaceA.h
            ~InterfaceA();
            virtual void DoSomething();
    MyUtils (dll)
        HelperC.h
            static float Foobar;
        HelperD.cpp
            float HelperC::Foobar = 1.0f;
    MyImplementations (dll)
        ImplementationB : InterfaceA
            (uses the value from HelperC.Foobar)

The MyExe and MyImplementations projects contain most of the executing code. But, I need an interface, so I need an interface project (MyInterfaces). I need some helper classes that need to be accessible from both MyExe and MyImplementations, hence MyUtils. I would like this helper class to be statically available, though it is not mandatory.

I had a compiling version before I started adding MyUtils with the HelperC class. I had to mark the interface destructor with __declspec(dllexport), along with the DoSomething method. I also had to mark the constructor of ImplementationB in order to instantiate it from MyExe, which makes sense. However, when I tried to mark the entire class (both the implementation and the interface) with __declspec(dllexport), the example wouldn't compile (which does not make sense).

From what I've read, having static fields in a dll and using them from external code doesn't really work all too well. So, as an alternative, I made Foobar non-static and passed a HelperC instance to the method as described by InterfaceA. Since I had already gotten simple classes to work, I figured that should work as well. However, now the compiler is throwing errors on the constructor of ImplementationB (LNK2019).

In short: I'm getting link errors all over the place in sections that have nothing to do with my changes, and there's little documentation describing the specific steps I need to perform in order to get a simple dll reference to work.
Can someone point out what I need to add and where I need to add it in order to make it compile? Also, some do's and don't's about C++ dll references would help a lot (e.g. don't use statics across projects).

Beijerinc

After much digging, I found out that the culprit was a magical project setting. It is called Ignore Import Library and is located at Project Properties->Linker->General, and is set to Yes by default, while it should be set to No in most cases. The setting tells the executable project to use the dll's lib file during compilation. This still sounds strange to me (sounds like duplicate build output), but as far as I understand it, the lib file describes how to link to the dll. If your dll produces a lib during build, you probably want to set the setting to No.

I also learned that to be able to use the HelperC class as a statically accessible helper, I needed to use dllimport in combination with the macro trick, as described by @drescherjm. The dllimport declaration is only ever needed to be able to use data members across libraries (static class fields or globally defined variables). It may be applied to functions as well, though it is not required, in which case it provides a slight performance boost during library linking.

For completeness, my project structure after getting it to work:

MySolution
    MyExe (Win32 .exe, Debugger Type=Mixed)
        Main.h
        Main.cpp
            (constructs ImplementationB calls the methods as defined by InterfaceA, then deletes the instances)
            (calls/fills HelperC::Foobar)
    MyInterfaces (dll, Ignore Import Library=Yes, because there is no .lib after building)
        InterfaceA.h
            class __declspec(dllexport) InterfaceA
                ~InterfaceA() {};
                virtual void DoSomething() = 0;
    MyUtils (dll, Ignore Import Library=No)
        HelperC.h
            class __declspec(dllimport/dllexport) HelperC // (see macro trick)
                static float Foobar;
        HelperD.cpp
            float HelperC::Foobar = 1.0f;
    MyImplementations (dll, Ignore Import Library=No)
        ImplementationB.h
            class __declspec(dllexport) ImplementationB : public InterfaceA
                ImplementationB();
                ~ImplementationB();
                void DoSomething();
        ImplementationB.cpp
            ImplementationB::ImplementationB() {};
            ImplementationB::~ImplementationB() {};
            ImplementationB::DoSomething() { /* Omitted */ };
                (uses HelperC::Foobar in implementation)

On a side note: if you added a default C++ class library project in Visual Studio, you may need to flip the Project Properties->Debugging->Debugger Type setting to Mixed before you will be able to set/use breakpoints in the dll code. See this.

I hope this helps others who are wrestling with dll's in C++ (and Visual Studio).

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I set up a server for SSH?

분류에서Dev

How do I set up an email server?

분류에서Dev

How do I set up a bash alias for a common working folder?

분류에서Dev

Set up project files

분류에서Dev

How do I set up live audio streams to a DLNA compliant device?

분류에서Dev

How do I set up Emacs on Linux to read/send mail from Gmail?

분류에서Dev

How do I set up an iAd that works for both iOS 5 and 6? and if possible compatible with all iOS

분류에서Dev

How do I set up a second display via DVI/viewport with a Lenovo T420S?

분류에서Dev

How do I set up Launchy's Runner plugin to launch Everything search

분류에서Dev

How do I set up a local SOCKS proxy that tunnels traffic through SSH?

분류에서Dev

How do I set up SSH to transfer files from my local machine to a shared server with rsync?

분류에서Dev

Flycheck and Clutter - how can I set it up?

분류에서Dev

How do I speed up this for loop in r

분류에서Dev

How do I get a DLL or EXE name from a C# project file?

분류에서Dev

How do you set up unit tests without Karma?

분류에서Dev

How can I set up logging for node-mongod-native?

분류에서Dev

How can I set up Git branch for automatic backups

분류에서Dev

How can I set up conditional associations in Rails?

분류에서Dev

How can I set up a house-wide sound system?

분류에서Dev

how do I open a solution that has a vdproj in it in VS2010?

분류에서Dev

How do I delete a solution from TFS 2012

분류에서Dev

How do I set basic options with meson?

분류에서Dev

how do I set keys for JSONObjects in PHP

분류에서Dev

How do I permanently set the affinity of a process?

분류에서Dev

How do I set combobox text

분류에서Dev

How do I set uget as a startup application

분류에서Dev

How do I set the at command shell to bash?

분류에서Dev

How to set up maven?

분류에서Dev

How do I look up man page references with square brackets?

Related 관련 기사

  1. 1

    How do I set up a server for SSH?

  2. 2

    How do I set up an email server?

  3. 3

    How do I set up a bash alias for a common working folder?

  4. 4

    Set up project files

  5. 5

    How do I set up live audio streams to a DLNA compliant device?

  6. 6

    How do I set up Emacs on Linux to read/send mail from Gmail?

  7. 7

    How do I set up an iAd that works for both iOS 5 and 6? and if possible compatible with all iOS

  8. 8

    How do I set up a second display via DVI/viewport with a Lenovo T420S?

  9. 9

    How do I set up Launchy's Runner plugin to launch Everything search

  10. 10

    How do I set up a local SOCKS proxy that tunnels traffic through SSH?

  11. 11

    How do I set up SSH to transfer files from my local machine to a shared server with rsync?

  12. 12

    Flycheck and Clutter - how can I set it up?

  13. 13

    How do I speed up this for loop in r

  14. 14

    How do I get a DLL or EXE name from a C# project file?

  15. 15

    How do you set up unit tests without Karma?

  16. 16

    How can I set up logging for node-mongod-native?

  17. 17

    How can I set up Git branch for automatic backups

  18. 18

    How can I set up conditional associations in Rails?

  19. 19

    How can I set up a house-wide sound system?

  20. 20

    how do I open a solution that has a vdproj in it in VS2010?

  21. 21

    How do I delete a solution from TFS 2012

  22. 22

    How do I set basic options with meson?

  23. 23

    how do I set keys for JSONObjects in PHP

  24. 24

    How do I permanently set the affinity of a process?

  25. 25

    How do I set combobox text

  26. 26

    How do I set uget as a startup application

  27. 27

    How do I set the at command shell to bash?

  28. 28

    How to set up maven?

  29. 29

    How do I look up man page references with square brackets?

뜨겁다태그

보관