0

I am trying to figure out how to deal with multiple projects under the banner of a single solution and how to correctly link projects in order to make things work in conjunction.

I began by making two projects in one solution - for simplicity's sake, ProjA and ProjB. ProjB is set as the StartUp project. Furthermore, ProjA is configured to be built as a .dll file which I am to link with ProjB, which in turn is configured to be built as a .exe file. So far I've linked ProjA to ProjB by adding a dependency to ProjB via referencing (RMB>Add>Reference).

With all that out of the way, I wanted to test whether the setup worked as intended. So I did the following. In ProjA, I wrote the following Test code:

Test.h

#pragma once

namespace ProjA {

    _declspec(dllexport) void Print();
}

Test.cpp

#include "Test.h"
#include <stdio.h>

namespace ProjA {

    void Print()
    {
        printf("Testing\n");
    }
}

And in ProjB:

TestEntry.cpp

#include <stdio.h>

namespace ProjA {

    _declspec(dllimport) void Print();
}

void main()
{
    ProjA::Print(); //breakpoint1
    printf("Testing (2)\n"); //breakpoint2
}

I set two breakpoints at the commented lines for debugging, built the projects and ran the program. At breakpoint1, I expected the console to print "Testing" and proceed to the next line, followed by breakpoint2 printing "Testing (2)" and proceeding to the next line before exiting. However, the console was blank at breakpoint1. It only printed the line from the main function at breakpoint2.

So what am I doing wrong here? Is there something wrong with the way I included the projects? Was there a linking procedure I skipped? Is there a specific way I need to set up Visual Studios 2017 in order for it to work? Are there other plugins/APIs I should be installing?

To test it out further, I commented out everything in ProjA. This time I expected a linking error when building the projects, but everything ran in the exact same manner. This confused me even more. It's as if I didn't even link the projects and the main function is not identifying the Test codes via the include. That's the only thing I could deduce so far.

Al Abrar
  • 3
  • 2
  • Try a full re-build, it sounds like you are working against an old version of projA and for whatever reason it did not build after the changes. Also note that how you are doing this code (with a __declspec(dllimport) in projB's code is not at all the usual manner, normally you would use a define for the dll project that flips another macro between dllexport and dllimport), and you would then have a single declaration in projA's header that is used both to declare the export within projA and imports in any code using the library. – SoronelHaetir Mar 26 '20 at 06:54
  • One way to know for sure that a re-build is needed is to try doing a step into the function that is misbehaving, if you get a message that the file has changed then it is definitaly a case where VS has not noticed for some reason (I have occiassionally seen this, usually VS is good about that sort of thing). – SoronelHaetir Mar 26 '20 at 07:00
  • I did rebuild both projects multiple times, but that didn't solve the problem. That being said, I am aware that this is a crude way of doing it, and that you'd traditionally use DEFINEs to do it. I just wanted to run a test and see how it works. Considering I didn't get the expected result, I suspect that doing it by conventional means will bear no fruit either. I'll give it a go. If things don't go as planned, something might be wrong with the way my VS is set up. – Al Abrar Mar 26 '20 at 08:38
  • Ok, doing it using #defines and includes fixed the problem. Thank you. – Al Abrar Mar 26 '20 at 11:11

1 Answers1

0

As suggested by SoronelHaetir, I redid the whole thing using #defines and namespaces. It's working as intended now. In conclusion, the crude method was a bad idea from the getgo.

Al Abrar
  • 3
  • 2