Access to cpp file in another project in Visual Studio 2019 - c++

I have two C++projects(Visual Studio Community Edition 2019) called Game(e) and Tester in a solution map. Tester is a subdirectory of Game. Both projects creates exe files.
The projects Game hast two cpp and two header files(Foo.cpp, Foo.h, Foo2.cpp and Foo2.h).
Foo has two methods add1 and add2. Foo2 has the method plus_one. The method add2 from Foo uses plus_one.
In the project Tester there is a file called Main.cpp. I want to use all the files from Gamer in the project Tester. I have added ".." to "Additional Include Directories" so that I can include Foo.h in Tester\Main.cpp. I have added a reference to Games. The problem is that the linker doesn't like it:
Error LNK2019 unresolved external symbol "public: int __thiscall Foo::add(int,int)" (?add#Foo##QAEHHH#Z) referenced in function _main
Error LNK2019 unresolved external symbol "public: int __thiscall Foo::add2(int,int)" (?add#Foo##QAEHHH#Z) referenced in function _main
Does anybody know how to fix it? One solution is to import both cpp-files into the Tester project. But this solution is not acceptable for me. I am interested in a solution without importing all cpp files. I have uploaded it on github: Github Link

A possible way is to directly add the required object files as linker input for the Tester project.
Open the properties page with a right click on Tester project then Properties
Select Linker then Input
In Additional dependencies add (through Edit...)*:
$(SolutionDir)$(Platform)\$(Configuration)\Foo.obj
$(SolutionDir)$(Platform)\$(Configuration)\Foo2.obj
And everything should work as expected. As the main project is a dependency of the Tester one, VS will build it (if required) before trying to build Tester, and this is enough to ensure that the objects will be up to date.
* The path is the path of the intermediate directory of the main project. As you have chosen to put the main project and the solution in the same folder, the path is directly the solution path. If the main project was in its own folder, you would have to add that folder to the path.

Related

Resolving and understanding external symbol errors in Visual Studio (C++)

Very new to C++ and the VS IDE, and I'm trying to start working with Dear imGUI and openGL with a hello world function. I've added resources with lib and dll files before for c++ (for openGL), but not something like imGUI which only has .h and .cpp files - feels like I'm struggling to understand how to inform VS of where those are and how they link up.
Here's an example of an error I saw when building:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol _glfwGetWindowSize referenced in function "void __cdecl ImGui_ImplGlfw_NewFrame(void)" (?ImGui_ImplGlfw_NewFrame##YAXXZ) imGUITrial1 ..\Coding\imGUITrial1\imgui_impl_glfw.obj 1
So I opened up imgui_impl_glfw and found the function, and it appears to reference a function declared in glfw3.h...but that function looks like just a declaration, with no definition. I had previously added each folder with these files in it to the project additional directories, so I went ahead and added glfw3.h as a resource to my sources folder in the solution as well. That didn't change anything, so now I'm not sure how to find where the function is actually defined, or if the issue is that I need to somehow tell VS that this file exists in a another nested properties window?
Maybe it will help to have a screenshot of the solution. These are largely files copied from the github that I haven't changed (which I chose from looking through the backend examples), so I don't think I broke anything in the files themselves.
enter image description here

Why do I still get "unresolved external symbol" error after adding the reference to the test project?

I created a C++ project in Visual Studio 2019. I added two files to it: MyClass.hpp and MyClass.cpp. Then I created a test project and added the original project as a reference.
If I doubleclick on the reference in the test project, I can see MyClass. But if I try to run the tests, I get this error:
MyTest.obj : error LNK2019: unresolved external symbol "public: struct MyRef __thiscall MyClass::SetData(struct Data)" (?SetData#MyClass##QAE?AUMyRef##UData###Z) referenced in function "private: virtual void __thiscall MyTest_SetData_Test::TestBody(void)" (?TestBody#MyTest_SetData_Test##EAEXXZ)
What do I wrong? Why is MyClass.cpp not compiled, even if it is in the referenced project?
I can build the original project without error. I only get the link error, when I try to build the test project.
I came across this question about unresolved external symbol error, but it did not solve my issue. This is a Visual Studio specific problem.
If I add MyClass.cpp to the test project manually, then it works. But I do not want to do that for each file, because my project may have many more cpp source files, not just this one. I would like to make it work using the "references" feature.
When I created the test project, I selected the original project as a reference.
Adding project B to project A as a reference basically just assures that project B is compiled whenever project A is compiled.
You need extra setup:
make sure project B builds as a library (and not an executable)
consider two paths accessible from both projects. This will be where the project B library will be installed. This comprises of two things:
the binary (static library is the simplest setup)
the library headers
on compilation of project B you need to install both the resulting binary and the headers to these paths. For this you can make a post build step that copies the necessary files: Properties » Build Events » Post-Build Event
Alternately just use project B's build folder and source location.
in project A:
add the header path to the include paths:
Properties » C/C++ » General » Additional include directories
add the library to the list of libraries to link:
Properties » Linker » Input » Additional dependencies
add the binary path to the linker library search
Properties » Linker » General » Additional Library Directories
Just to add something to the previous comments/answers:
You didn't mention if you build your reference project as a static or dynamic library.
This can be set by going to the project's Properties->General->Configuration Type
Note that if you select Dynamic Library (.dll), your symbols (such as MyClass::SetData(struct Data)) will not be exported by default and you'd need to add the __declspec(dllexport) keyword. See here

I can't seem to link to the desired .lib file, resulting in an unresolved external symbol error

I have inherited a VC++ solution and from one project (named "IntersonWinDriver") I'm trying to link to a .lib file that is generated by a different project (named "Utility"). I noticed that in a different project, named "MasterVu", I found "Utility.lib" included among the linker command line options.
In order to duplicate this for the IntersonWinDriver project, under the properties window I selected Configuration Properties->C/C++->General. I added the folder containing the code for the Utility project under the "Additional Include Directories" option. I then added the Utility project as one of the project dependencies for IntersonWinDriver.
The problem is that when I look at the linker command line options, I don't see any reference to Utility.lib as with the MasterVu project. I could manually add a reference to Utility.lib under Configuration Properties->Linker->Input, but I would rather not. What am I missing here?
As you may guess, I'm getting an unresolved external symbol error due to any reference in IntersonWinDriver to code written under the Utility project.
Hmm...I see that under Common Properties->Framework and References I can add the Utility project. Now I feel silly for asking this question too soon...

Linking Issues in a Multiproject Visual Studio 2005 solution

I'm working on a solution file (VS 2005) which contains multiple projects.
There are two projects of my concern right now - one called core and another called log.
The core project is the startup project and the log project has core ticked in its project dependencies.
I have two files in core project - AB.h(declarations) and AB.cpp(definitions)
Many other cpp files from the core project refer to AB.h and has no issues finding the definitions in AB.cpp.
Now comes the issue. I have a cpp file called CD.cpp in the log project which requires referring to AB.h. I have added the include directory of AB.h to both the core and log projects (AB.cpp also resides in same folder)
In addition the log project has $(OutDir) in its "Additional Library Directories" parameter in the Linker part of project settings. Also have added "core.lib" to the Additional Dependencies field in Properties->Linker->Input
The core project compiles into a dll but there is also a core.lib in the same folder amongst other junk. Solution compiled as the Release version.
In CD.cpp, when I right click the #include "AB.h" line, it takes me to .h file in the core folder. So far everything is fine. However when I try to compile the solution, the log project does not build properly, showing these two linker errors.
log.obj : error LNK2001: unresolved external symbol "class myNamespace::myClass myInstance" (?myInstance##3VmyClass#myNamespace##A)
log.obj : error LNK2001: unresolved external symbol "bool infFlag" (?infFlag##3_NA)
The class is part of an h file included in AB.h (instance declared in AB.cpp and included as extern in CD.cpp. infFlag is declared in AB.cpp and declared in CD.cpp as extern.
What do I do to let log build properly? Thanks
OK. You guys need more details. Sure.
One Solution file. Multiple Projects.
[Core]
includes AB.h and AB.cpp
-AB.h
--includes EF.h from external lib (functions from this lib is working properly for files within the same core project).
--declares some functions
-AB.cpp
--defines those functions which was declared in AB.h
-some other cpp files which can use those functions from AB.h without issues
[/Core]
[Log -> Project Dependencies -> Core]
-No h files
-CD.cpp
--requires AB.h to work. Is included with a #include command and its linked properly (I can right click and click "open file" to open the real .h file)
--uses functions from AB.h
[/Log]
Hopefully this will better let you guys understand the project structure in my solution.
Issue comes when compiling Log project. It always comes up with a link error for any references to functions in AB.h saying "unresolved external symbol". I guess this comes because it cannot find the definitions for the functions declared in AB.h (probably because it is a different project). Since log is dependent on core, and core will have compiled as of then, I added in $(OutputDir) to Library directories (both core and log build into the same directory) so that it can find core.lib and also added "core.lib" to "Additional Dependencies" under Input tab under Linker (in project properties).
So there you go. Any ideas?
In addition the log project has $(OutDir) in its "Additional Library Directories" parameter in the Linker part of project settings.
Probably wrong. It should be that dir where "core.lib" resides, the $(OutDir) is where the log will be put.
The class is part of an h file included in AB.h (instance declared in AB.cpp and included as extern in CD.cpp. infFlag is declared in AB.cpp and declared in CD.cpp as extern.
So where are definitions?

How can I resolve "error LNK2019: unresolved external symbol"? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I've got this MFC application I'm working on that needs to have an embedded database. So I went hunting for a slick, fast "embeddable" database for it and stumbled accross SQLite.
I created a DB with it, and I created a static library project with Visual Studio 2008. the library project will be used in another main project.
In the library project, I created a class DBClass with a method AddFeedToDB(CFeed f). The library project uses the .lib file from codeproject (cppsqlite3.lib).
When compiling the static library, no error is detected, but when I try to use the library project file in the main project, I get these type of errors:
error LNK2019: unresolved external symbol "public:void __thiscall
CppSQLite3DB::close(void)" (?close#CppSQLite3DB##QAEXXZ
referenced in function "public: int __thiscall
CTalkingFeedsDB::AddFeedToDB(class CFeed,char const*)" (?
AddFeedToDB#CTalkingFeedsDB##QAEHVCFeed##PDB#Z
What am I missing?
I know it is already 2 years since this question... but i run in the same situation here. Added all the header files... added the lib directories.. and keep having this error.
So i added manually the lib to the Configuration Properties -> Linker -> Input -> Aditional Dependencies
and all works for me.
It happened to me more than once that I thought symbol XXX (i.e. ?close#CppSQLite3DB##QAEXXZ) was in the import lib, while the actual symbol was __impXXX (i.e. __imp?close#CppSQLite3DB##QAEXXZ).
The reason for the linker error is then to be found in the compilation step: the compiler will generate the ?close#CppSQLite3DB##QAEXXZ symbol to be imported, where it should generate __imp?close#CppSQLite3DB##QAEXXZ. This often means that the function declaration itself didn't have __declspec( dllimport ). Which may be caused by some preprocessor symbol not being defined. Or the __declspec not being there at all...
Don't know if it is your case, but the imp prefix may mean that you are compiling a x64 library in a Win32 project.
You either need to link the codeproject SQLite lib to your executable, or to include the sources files in your project directly. (Which one did you do ?)
I would follow these steps:
think about what library or .obj file you expect the symbol to be exported by.
check whether it actually does export that very symbol (check character-wise). Sometimes, it's the calling convention differs.
check if the library you expect to contain the symbol is known to the linker - first check for the 'additional libraries', then check if the library is actually found (I mostly do this by using filemon.exe from sysinternals, and look for link.exe to open the lib file. )
After thinking a while, you may find that your library project will not export the sought for function. That function is in the database lib. You should add that lib to your main project. It's no use adding it to your static lib project.
The compiler and linker will not link one library into another (unless one is a DLL). You need to specify both libraries (cppsqlite3.lib and your own static library) in your main project.