Reputation: 681
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:
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:
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
.
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.
Upvotes: 0
Views: 770
Reputation: 681
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.
Upvotes: 1