How to import code from .exe project for unit test - c++

I use Visual Studio 2015 to develop C++ app.
I have solution with two projects: one is the .exe project and another - project to test code via Boost Unit Tests.
Everything compiled, but linker fails to build second project. It says error LNK2019: unresolved external symbol.
How to link second project to the code of the first project?

You cannot import code from exe (i.e. link against exe), at least not easily. You can create a third project with the common code, either static or dynamic library, and let the exe and the units test both depend and link against the library.
Another option would be to have everything in a single project, but that is more suitable for makefile or CMake project (and then have build targets "all" and "tests", for example).

Related

Use XrSceneLib_uwp in a native HoloLens 2 OpenXR application

I'm trying to create a native HoloLens 2 C++ application with OpenXR. In the Microsoft OpenXR-MixedReality example I discovered the XrSceneLib_uwp project which has the configuration type set as static library (.lib) in the Visual Studio properties. Accordingly to that I tried to add it as a library in my solution which also has a Core Application (C++/WinRT) in it where I want to use the functions from XrSceneLib_uwp. In my thinking it makes sens because I can use the library for example to create a holographic title scene without much effort. The problem here is that when I build my solution I'm getting linker errors and I don't know how to fix them. I'm also relatively new to Visual Studio (22 in my case) and most likely I'm just doing someting wrong in the linking process.
Linker error when I try to build:
Error LNK2019 unresolved external symbol xrGetInstanceProcAddr referenced in function "public: __cdecl `anonymous namespace'::ImplementXrApp::ImplementXrApp(struct engine::XrAppConfiguration)" (??0ImplementXrApp#?A0x934f03db##QEAA#UXrAppConfiguration#engine###Z) CoreApp3 \repos\CoreApp3\CoreApp3\XrSceneLib_uwp.lib(XrApp.obj) 1
My solution explorer
Here is what I do:
I copy and paste the shared and openxr_preview folders from the OpenXR-MixedReality .zip file in my project folder.
Add XrSceneLib_uwp.vcxproj in /shared/XrSceneLib as an existing project to my solution.
Add the path to the copied shared and openxr_preview folders in Properties->Configuration Properties->C/C++->General->Additional Include Directories.
Add also the path to XrSceneLib_uwp.vcxproj in Additional Include Directories in my main project (CoreApp).
Add reference in CoreApp to XrSceneLib_uwp (Add->Reference->Projects).
After that everything works fine and I can even build the solution. But when I try to use the library I'm getting these linker errors. I don't understand why it works in the OpenXR-MixedReality examples but not when I try to implement it on an different project. What am I missing? I assume that XrSceneLib may not have been designed to be used externally in other projects but at the same time I think it should be possible somehow.
I also tried to add Additional Library Directories and the missing object files but either I have done something wrong or it just simply does not work.
I really appreciate any help.
I think the linker is failing to find the OpenXR Loader. The sample projects in the microsoft/OpenXR-MixedReality github repo link to the OpenXR Loader using the NuGet package manager. I get the same linker error if I purposely remove the NuGet package references from the sample projects.
You can either link to the OpenXR Loader using NuGet or you can build the OpenXR Loader youself. Take a look at the "Using OpenXR in an existing project" section on the microsoft site:
https://learn.microsoft.com/en-us/windows/mixed-reality/develop/native/openxr-getting-started#using-openxr-in-an-existing-project

Including an external library to my own library project c++ visual studio

Currently i have 3 visual studio projects:
ConsoleApplication
Logic
Test
These projects are all in the same solution. In the Logic project i use an external library named curl. My logic project is a static library made with the "new project" wizard in visual studio. This project includes a pch.h file. I added following things to my Logic project properties:
Set C/C++ > General > Additional Include Directories to the folder with the header files of curl
Set Libarian > General > Additional Library Directories to the folder with curl.lib in it.
Set Libarian > General > Additional Dependencies to curl.lib.
Now when i build the Logic project the output is a Logic.lib file. i checked with DUMPBIN /SYMBOLS /EXPORTS Logic.lib if the Curl functions are actually in the lib file, and they are.
To include the Logic project into the Console application i did the same 3 steps and added the Logic.lib to the Console application project. Everything works fine untill the moment i start using classes that use the external curl library. When i use these classes i get a link error: Unresolved external symbol (LNK 2019). I have tried much to fix this, but it seems that i am not capable of solving it. Am i doing something wrong that causes this not to work?
Also i would like to be able to use my logic project the same way as i do in my ConsoleApplication in my test project. For more context why i splitted up those projects can be found in my previous question Use c++ classes from console application class in other project visual studio
So after hours of research i found this post and i think this also applies to my case: Linking static libraries to other static libraries
TL;DR static libraries do not link with other static libraries
So i think the best solution in my case is to convert the static logic library to a dynamic lib (DLL)

Build FLTK Multi-threaded (MT) project with Visual studio

I use fltk lib in my project.
My goal is to compile my project with all libraries dependencies in it (now problem is only with FLTK), so I don't need to install vcredist libraries on each machine, where I want to use it.
So I read that for this I need to compile my project with parameters c/c++ --> code generation --> Runtime library --> Multi-threaded.
Now I'm using Multi-threaded DLL and everything works fine, but when I change it to MT I get error list (they got all similar code LNK2001):
Code LNK2001 Description unresolved external symbol __imp___wgetcwd file ConsoleApplication2\fltk.lib(fl_utf8.obj)
What I should do to compile my project with MT setup?
Solved.
My solution was recompiling FLTK static library with MT setup.

Linking DLL error visual studio

I walked through this tutorial and tried to recreate the example I get this error:
Error 1 error LNK2019: unresolved external symbol __imp__GetXyz#0 referenced in function _main app.obj app
If I download the source from the website and run the project in visual studio it works, but If I even copy the code from the original project in my own visual studio solution it still doesn't work. I guess it must be some project settings, I don't know. What do do?
You need to add the XyzLibrary.lib to your Linker settings within project settings.
You can also do it directly in code by using #pragma comment(lib,"xyzlibrary.lib") if you are using VisualStudio.
The reason why it works in the given sample without these 2 approaches is that in the solution file, the dependencies are set from XyzExe to XyzLib, and XyzProject has 'Link Library Dependencies' in Linker settings set to true. However, I am not too keen on this approach, as I don't like to store data important for building inside solution files (for larger projects, developer's solution files can differ from build system solution files)
Seems like you are missing some external library that needs to be linked in. Whatever provides the GetXyz function is missing.
Open the Solution that works, right click the project, Properties, Linker, Input, and check what their Additional Dependencies list up. You need to add the same to your own project.

VC++ project never up-to-date

I'm using Visual Studio 2008.
I have a solution with two projects. One builds a DLL, which is my "production code". The other builds a console app exe, which is my unit test suite.
For my unit test project, I have listed as linker inputs the names of the source modules from the DLL. I.e., I have a DLLMain.cpp in the DLL project, and a linker input "DLLMain" in the exe project. This allows the exe to link with the obj files produced by the DLL project, preventing recompilation of those modules just for the unit tests. (Saves a lot of build time.)
THE PROBLEM IS that because the exe is produced later than the obj's, and by a different project, its timestamp is always newer than the obj's. So when you try to run or debug, it ALWAYS says the exe project is out of date and needs to be rebuilt.
Is there some way I can configure the exe project to ignore the timestamps? Or is there some other, perhaps more general, solution I'm not seeing here?
Seems like you are creating foo.obj in the DLL's project, linking foo.obj in the DLL project to produce the DLL, and then linking foo.obj to your EXE project without first recompiling it.
I have never done this before, but first thing I would check is to make sure the Intermediate Directory settings are the same for both the EXE project and the DLL project.
Go to the project that is not needed for unit testing
Right click on it and press Properties
Click on Configuration manager
Add a new configuration using the first drop down list
Click ok
Select that configuration in the window that is now focused
then in configuration properties set "Excluded From Build" to yes