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

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)

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

C++ detours linking issue

I have problems building my code that is using static lib detours. I am trying to do an old basic CTF. For that I want to get into detours.
Whenever I try to build my .dll file I get an issue
LNK2019 unresolved external symbol _DetourTransactionBegin#0 referenced in function _DllMain#12
Now, I have built the detours library using 3 different version of the visual studio dev console.
I have tried firing 'vcvars32.bat' and then using nmake to build the library which was able to build it, but I get the above error during linking my .dll. I have also tried building it with 'vcvarsamd64_x86.bat' and then using nmake to build it which also was able to build the library, but I still get the same error as above during linking.
I have tried the usual stuff: the include folder for detours.h is added to C++/General/Additional Include Directories.
Under Linker/Additional Library Directories I added them as follows: "C:\temp\det_retry\lib.X64";"C:\temp\det_retry\lib.X86";%(AdditionalLibraryDirectories).
And also under Linker/Input/Additional Dependencies I have the following: detours.lib;%(AdditionalDependencies)
What am I missing here? This is a blocker for me for a couple of days and I am reiterating the same steps trying to figure out what's missing but I cannot see. I'd really appreciate the input.
I am sure I am using the newest version because I have downloaded (cloned) detours from the ms github page.
It appears your "Additional Library Directories" are setup incorrectly or contain invalid entries rather. They look like actual library file entries (i.e. pointing to some specific files) versus being only directories (e.g. "my/lib/path/for/my_project/"). Visual Studio's naming conventions are somewhat cryptic but they should be directory entries only. There should be an entry to whatever directory contains the detours.lib file (e.g. "MyProject/Libs/MSDetour" ... where MSDetour is a folder with the "detours.lib" in it) and then Visual Studio should find the library and link everything correctly.
As a side note, if you are using the Visual Studio developer console for building your project/solution you might want to look into CMake ... it is, in my opinion, significantly easier to work with (less "settings" digging) and maintain in the long-run.

Visual studio unit testing a static library; prevents additional dependencies in main project

I have a project that I would like to unit test in Visual Studio 2013. I have changed the project type to a static library (lib) after getting some linker errors and reading the answer to this question.
I do not understand the underlying mechanisms of compiling and linking in c++. In changing my project to a static library, it seems I have lost the ability to specify additional dependencies, which is a pain because I need three libs for my main project that I would like to test.
What should I do to test this project, is there a way to make my project a static library and still specify additional dependencies?
Static library is a collection of object files. No linker involved. Dependencies are not being resolved. It only requires headers to build.
Now, when you finally create an executable (or a dynamic library), you should link all the necessary dependencies.
However, note that additionally to the "classic" build process described above, Visual Studio IDE provides abstractions (convenient, but sometimes a bit misleading, especially for novices), called "Project references" and "Project dependencies". There are options to make a static library "depend" on other static libraries. Also, executable can depend on that first static library. When "Link dependencies" is enabled, Visual Studio will resolve entire dependency chain recursively and link all the necessary into your executable.
See also:
What does the “Link Library Dependency” linker option actually do in Visual Studio 2010?
MSDN - C/C++ Building Reference
MSDN - Walkthrough: Creating and Using a Static Library (C++)
What is an undefined reference/unresolved external symbol error and how do I fix it?

Libpd configuration in Visual C++ 2010

I'm trying to import and run the C++ sample project coming with the libpd API CppTest within a Visual C++ 2010 project.
I imported the C++ API files of libpd, i.e. PdBase, PdReceiver, PdMidiReceiver and PdTypes, with corresponding source files in the case of PdBase and PdTypes. I put as additional include directories the include folder of Pure Data
("C:\pd_installation_folder\include") and the libpd_wrapper folder containing the C code of the libpd wrapper.
When i run the solution of the sample project it doesn't work due to an error during the linking phase, "error LNK2019" with a huge list of unresolved externals.
What's the problem here?
Should I have any libpd additional static library (or .dll) provided to the linker configuration properties of the VC++ project?

Visual Studio static library compiler hardcodes relative paths

I think my problem is that the Visual Studio static library linker hardcodes relative paths, and this causes problems later when linking against that static library from a different directory.
Here's what I'm trying to do:
I have a library dll, call it base.dll.
I have an application that links against this dll: application.exe. In order to write unit tests for this application, I compile the application as a static library: application.lib.
I have a testing project that compiles testing.dll that links against application.lib.
In the last step, the linking fails with LNK1104 (cannot open file) because it is using the relative path of application.exe, not of testing.dll. Testing.dll has already linked successfully to base.dll earlier in the process.
Any suggestions?
Try checking "Inherit from parent or project defaults" in the "Library Directories" dialog for each project.
I have had similar issues with my own library projects, and there is not much documentation out there on this.
The trick is to not link against base.dll when I build the static library, and only link against it when I build testing.dll.