Debugging static library function cpp file in Visual Studio - c++

Being new to making/using libraries, I followed a simple tutorial to build a static library in Visual Studio (2022). I made a solution with 2 projects in it. One that builds a library and one that uses the library. It worked. Then I made an independent project / solution to see if I can use this library independently from the original solution/projects. I put the lib file and hpp/cpp file into new separate folders and set these folders in project properties (Additional Include and Library Directories + the .lib file).
Problem: I could use the functions from the library in this new project but when I tried to step into it with the debugger, the cpp file it stepped into was from the original project. (that's what the path showed anyway.) When I renamed the old project and tried again, VS complained that the file directory has changed and asked me to set the path to it. I did set it to the newly made directory with only the hpp/cpp in it and it was fine. I made another new project for using the library and tried the whole thing again and then the debugger simply could not step into the function, it stepped over it.
My questions: what was going on here? how did the debugger find the original cpp file when Im positive I did not set that path anywhere in the new project? and how do I debug a static library then? Is putting the cpp file next to the header in the include directory not enough?

Related

No .lib file generated after building tiny C++ static library project

I decided on adding a tiny extra project to my visual studio solution which includes only a single header file(for now) with a mutex which allows only 1 thread to output to the console at a time. Since this is a functionality which all of my projects in my solution will need so I thought it will be best to add a separate project for it and add references to it on the other projects.
So I created an empty project named Commons, then added an header file logger_mutex.h and wrote the following code inside it.
#pragma once
#ifdef _DEBUG
#include <mutex>
std::mutex loggerMutex;
#endif
I changed the project type in properties from Application(.exe) to Static Library (.lib). Then I added the include path to this file in the other project properties. Also I added this Commons project as a reference to all my other projects. But now when I try to build the solution it gave the error LINK1104 cannot open file ../path/to/my/output/directory/Commons.lib
I investigated on the output directory and there was no file in there named Commons.lib. I tried rebuilding the Commons project separately, and even though visual studio said it built successfully I did not see the Commons.lib file appear on the output directory.
I tried it even without the other projects, in a completely different solution. It still did not generate any .lib file in the output directory. I think this should be verifiable as well.
So what am I missing here, is there some kind of minimum requirement needed to have to get a .lib output file generated? Is my code too small to generate a .lib output? I am using Visual Studio 2022.
Add one empty .cpp file in your lib project and a lib will be generated.
But as far as I am concerned, it will be better to #include the logger_mutex.h to other project's pch.h instead of as a library.

Adding my DLL into a Visual Studio project in C++

I am working on a project that involves making a dynamic link library, so I want to test it in a console app in Visual Studio.
The DLL is also made in Visual Studio, it doesn't have much, just a few functions in it. I'm not sure if I'm just supposed to include the librarys header in the include directories panel in Properties, or do something else
A lot of people say I'm supposed to add its corresponding .lib file in the Library or Reference directory, but I'm not sure that VS generates a .lib file alongside the DLL. I'm using VS 2015.
I don't have VS in front of me this very moment, but these should be the general steps to set it up:
Properties->Linker->Input: your.lib
Properties->Linker->Additional Library Directories: ../your/bin
Properties->General->Compiler->Additional Include Directories: ../your/include
To build your app, the DLL's API headers must be in the include for the compile-time, it's LIB files in the bin for the link-time. Once you have your app EXE, all you need is the DLL to be in the same folder as your EXE when it executes.
You might also want to add the dll project and the app project into a common solution in VS and add (right click) Project Dependency from the app to the dll. This ensures correct order of building, assuming you are going to build the dll at all.
You can also do what I did.
You can create a Libs directory inside of your Solution directory.
You can then place your .DLL files inside of the Libs directory or some sub-directory inside of Libs
In my case, I added the entire SFML-2.3.2 directory in there, which included the source-code, .lib files, and .dll files.
I did link up what I could in the project properties, but I used Visual Studio's macros to fill in the path name to the Solution directory. Just in case I wanted to put this in version control and work on it from multiple machines.
Then I opened up the Project's Property Page.
Within the property page, I went to Build Events -> Post-Build Event -> Command Line
Within the Command Line, you can add a copy command that will copy any needed files into the same directory as the executable that will need them.
In my case I used: copy "$(SolutionDir)Libs\SFML-2.3.2\bin\*" "$(TargetDir)"
I could have written multiple commands to copy just the individual files that I needed, but I had spent a good three hours trying to get SFML to work without actually installing it.

OpenGL Configuration Problems. Unresolved external signals

I watch "OpenGL Essentials" to learn OpenGL programming. The author writes a framework himself which contains lots of .hpp and .cpp files. To use the framework, I have to compile all files to a single Library.lib file. But there are two questions that I cannot figure it out.
When use the original sln provided by author, it create two lib files, one in a new created folder "Lib" and one in "Debug" folder, It also copy all hpp files in "Library" folder to a new created "include" folder. But this do not happen when I create a new VC++ project by myself. So how should i configure the VC++ project properties to make that happen.
When we use Glfw, we have to include glfw3.lib, and we also need to tell the exe file where to find the glfw3.dll. That's hard to understand. Because, we can compile our framework to lib file, And we also can compile it to dll file. So, Why we need both lib and dll file for GLfw? What's the difference between this two files.

NSight Include files from another project

I have been trying to get one Nsight project (project 1) to use some files from another Nsight project(2), and for the life of me can't get the project 1 to build!
I have tried referencing/setting include paths/building project 2 as lib etc but nothing lets me build project 1 without encountering some kind of error. At the moment I have reference project 2 and get "No rule to make target" error.
What is the best way (bar copying and pasting the source files) to include files from one project in another.
Thanks
Sam
This writeup demonstrates
how to include a file from one project into another (the test8lib.h header
file from the library project is being included into the main project)
how to make one project link to a library produced in another project. (the library produced in the library project is linked into the main project).

C++, Visual Studio 2012, LIBs, DLLs, Includes, Source and Linker

I'm trying to understand what exactly all of these are and how they relate to each other (and most importantly, how to install them).
From what I've read, LIBs are libraries linked during the compilation of my project and DLLs are libraries linked during the runtime of my project.
So for me to use a LIB, I have to have the actual .LIB file somewhere in my computer, go to Project -> Properties -> VC++ Directories and add the path to the file in the Library Directories, and after this I have to go to Linker -> Input -> Additional Dependencies add the .lib name in there, and finally I need to type #include in my code, right?
So, some questions:
When I finish and build the release of my program, will the .exe only run if the target platform has the .lib installed in their PC as well? If yes, what steps do I need to do to make sure the .lib goes with the .exe?
When I get the source of a open source project, if I add them (using Add Existing Item...) to my project, can I use them just by using #include as if the files were mine and it would be the same as having the .lib installed? Or do I need to install the .lib file and still use these source files?
I have a project using OpenGL and I linked to glew32.lib, but I don't have the lib or any new directory added in the VC++ Directories, so I think this means I must've installed the .lib in the system folder or somewhere where the Visual Studio won't ask for another directory, should I worry about this when releasing a project?
How the above questions relate to DLLs and is there any reason why should I use DLLs over LIBs or the other way around?
I'm starting to use more and more libraries and I noticed I just dragged, copied and included it everywhere so I could use them but never really understood how they "fit" in the project. Especially those open source libraries where they provide so many files and I don't really know what to do with them...
You don't need to have LIB files along with your EXE file for running in another computer, LIB files are static files and DLL files are dynamic. So when you compile all static codes will be included in your EXE file, but DLL files will be loaded and used dynamically in runtime, so you just need to have your DLL files with your EXE file. This way, your code will work and run properly in other computers.
Just adding another project is not enough, you need to compile them and generate LIB files out of them. Then you add the generated LIB file to your final project and include external projects in your final binary. If you are compiling multiple projects together in a solution, you'll need to set project build order in solution properties in VS.
No, that's OK. It seems you've put LIB files in right folder and you don't need to have LIB file with your EXE file to run it in other computers.
DLLs are dynamic libraries, so you need to have them with your application. Installers usually install EXE files with DLL files in the same folder, so your app will run properly, but no need to include LIB files at all.
Also you can include LIB files like this:
#pragma comment(lib, "glew32.lib")
So you don't need to do it in project settings, but assuming you have your LIB file in "Library Directories" path.
Using DLL files can be done in two ways:
One is linking your application to DLL file and having DLL file's function entry in your EXE file's import table:
like using
#include <windows.h>
then
GetWindowsDirectory(windir, MAX_PATH);
So you'll have GetWindowsDirectory API entry in your EXE file's Import Table.
Also you can do it dynamically:
hinstDLL = LoadLibrary("kernel32.dll");
if (hinstDLL != NULL)
{
func_GetWindir = (DLLPROC) GetProcAddress(hinstDLL, "GetWindowsDirectoryA");
...
There is not much difference, only difference is:
In first method, as it's in your EXE file's Import Table, if there was no kernel32.dll or there was no GetWindowsDirectory entry in kernel32.dll, your EXE will not run at all, it will show a critical error and will not run. But in dynamic way (second way), your app will run, but as soon as your code try to use GetWindowsDirectoryA API, it will fail. You will have 0x00 in func_GetWindir. If you attempt to call it, then program will crash.