0

In an attempt to have modular code, that is easy to maintain, I have split code from a previous VC++ solution into separate other projects. That was I can maintain the separate code bases, whilst still being able to include the working code in a solution. My solution explorer looks like this:

Solution explorer

I have followed this (Project config/VC++/Extra Include paths) SO answer here, to allow fodHelperBypass.cpp to access the methods within registry.cpp. VS allows me to then import the header file for the RegistryTools project:

Import declarations

In fact, if I Ctrl + click the registry.h import declaration VS will open up the header file, and I can confirm it is being loaded from the correct location. In that same directory resides the registry.cpp file.

I can also confirm that in the project build settings, both projects are set to an x64 build configuration (at an earlier point this was not the case, and VS could not locate registry.h as RegistryTools build configuration was set to x86). VS also recognises the external functions within the fodHelper.cpp file, and doesn't give any undefined error.

Yet when trying to build the project, I am receiving a LNK2019 error for each method declared in registry.h and subsequently registry.cpp.

Linker error

I have read this SO post, but I don't feel as this applies, as I am not declaring any classes. Furthermore when I simply copied and pasted the registry.h and registry.cpp into the solutions directory, and added them to the solution, the code built without error.

Can someone please explain what I am doing incorrectly to cause this error with the linker?

EDIT

I have compiled the two subprojects involved in this solution as .lib files, and that has had the desired effect. Although I am still confused, must I do this to be able to use methods from another C++ project? Must the referenced functions be part of a static library?

From reading the first link, I was under the impression that all I had to do was add the include directory in the project configuration, and then I would be able to use the methods from with RegistryTools.

RegistryTools is a project that contains one cpp file with four functions, and a header file that declares them.

RandomHash
  • 669
  • 6
  • 20
  • The SO post is correct. You use method built in another project. You need to link to the project. In VS one normally adds it via References section. – ALX23z Feb 20 '21 at 10:53
  • I have also tried that, but that gave me further linker errors. Adding RegistryTools to the references section causes 46 linker errors `MSVCRTD.lib`, and it was my understanding I only had to add references that way if I was using .lib files./static libraries. I was not aware I was doing this when trying to use code from multiple projects in one solution – RandomHash Feb 20 '21 at 11:00
  • There are also no .lib files in the RegistryTools project structure, even when I have built it. Do i need to do something to generate these? – RandomHash Feb 20 '21 at 11:08
  • 1
    @RandomHash You are meant to link the two projects together. So even if that gave you some more errors I would concentrate on solving those errors, not this error (which is happening because you didn't link the two projects). Also could you clarify what kind of projects you have. You implied that you aren't using static libraries. – john Feb 20 '21 at 11:50
  • @john I have attempted to explain the situation further, and provided more information surrounding the projects – RandomHash Feb 20 '21 at 19:38
  • @RandomHash I'll try and clear up a couple of things. If you want to use any code from one project in another project you have two options, static libraries or dynamic libraries (DLLs). Even if you use DLLs you (usually) would generate an import library to load the DLL. This is all covered on the Microsoft site, [here](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-static-library-cpp?view=msvc-160) and [here](https://learn.microsoft.com/en-us/cpp/build/dlls-in-visual-cpp?view=msvc-160) for instance – john Feb 20 '21 at 21:04

1 Answers1

1

For any programmers who come to C++ from languages such as Python, Java or any other interpreted language. This post may help with you with some misconceptions you may have. When trying to import code from other managed projects, it is not enough to add the secondary project to the include directory of the solution.

As @john pointed out, when trying to use code from another project within a VC++ solution, there are two options. Compiling the code to import as a .lib (Static Library) file, which will get included in the compiled executable. The process is described here.

The other option is to compile the project code you want to import as a .dll(Dynamic library) file which is not compiled into the final executable as described here.

Both methods have pro's and con's, and it is up to the programmer to decide based on their needs.

RandomHash
  • 669
  • 6
  • 20
  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Barrnet Chou Feb 24 '21 at 07:11