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

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.

Related

Debugging static library function cpp file in Visual Studio

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?

#include <winsqlite/winsqlite3.h> works in one project, not another

From Visual Studio 2017 I created a console application to test Sqlite. The first line I put in my main file was
#include <winsqlite/winsqlite3.h>
This caused no problem (the location was found). But I tried adding this include to a source file I had obtained by migrating a legacy project to VC++ 2017. This time the include line causes an error.
I suspect I should look at
$(VC_IncludePath);$(WindowsSDK_IncludePath);
however, I do not know how I can find out what's in the macros VC_IncludePath and WindowsSDK_IncludePath, let alone change them.
Could there be any other explanation?
>>however, I do not know how I can find out what's in the macros VC_IncludePath and WindowsSDK_IncludePath, let alone change them.
In Visual Studio, open Project->Properties->VC++ Directories->Include Directories->edit->Macros, Then you can check the content of these Macros.
If you need to use a three-party library in Visual Studio, you generally need to add three places in the property page. I will show in the picture below.
include is the path of .h file and library is the path of .lib file, and you should also fill the name of .lib file in the third edit box(additional dependencies). In your case, this third-party library seems has only .h file, so just add the path of .h file to the first edit box. Or simply put the file directly in your project's root directory and add it to your project in Visual Studio, then include it.

Creating a C++ visual studio project based on existing files

I've never worked with C++ or C. I'm trying to create a Visual studio project based on existing files which can be found here: example1.cpp together with the resources. As you can see this is example code of a book for OpenGl. I have opengl and glut present on my computer and they work ( tested it).
Based on the files mentioned above a created an empty C++ project in visual studio 2012 (i also have other versions installed if you can provide a solution in 2010 or so). I included the header files & the source file. Though I still get the following in my IDE:
with errors such as:
cannot open source file "Angle.h"
( Though the file is present in the project)
Can anyone tell me how I get these files to compile and run ?
Make sure that the file angel.h it's in the same path that the .cpp file.
Header files need to be in same directory with source files in order to use #include with quotes.
#include "header.h"
In other words Angel.h must be in same directory with example1.cpp.
However,you can add spesicific paths to your project from Project Settings>VC++ Directories and include header files which exists in those paths using
#include <header.h>

CMake and Visual Studio resource files

I am converting a C++ project created using Visual Studio 2005 to CMake and have stumbled upon a bit of a problem with resource files that are included in the project.
The project includes a .rc file, a bunch of .ico files and a .rc2 file.
The regular .rc file works fine in the generated project and uses the resource compiler. The .ico and .rc2 files however are causing problems when they are just being included, because in the generated project Visual Studio attempts to compile them using the C/C++ compiler.
I assume that these files are included by the .rc file, so it would probably work to just not include them in the CMakeLists.txt file, but since it is obviously possible to list them in the project (they are visible in the original project) I would like to do so, so that the user of the generated project can see that these files are being used.
What is the correct way to handle these extra VS resource files in CMake?
Try to set_source_files_properties(your.ico your.rc2 PROPERTIES LANGUAGE RC).
By default it shouldn't do anything with those files. The source file property LANGUAGE should be empty and thus the action for the file should be checked by the file type. Which shouldn't be anything since it's not something it should compile.
Check your CMakeLists.txt that is doesn't contain a set_source_files_properties command that would mess with that property.
If you want to do something with the files, here are two ways to do things:
With add_custom_target you can add them and run custom commands for them when you build the project. Granted that the files have changed.
With configure_file you can easily copy them to a build directory if needed. With the COPYONLY flag.

Simple Way to Use a DLL

I followed the MSDN walkthrough on creating and using a DLL in Visual C++ Studio, but it requires the user to add the DLL project to the same solution as the project they're working on.
Is there a simple way to include a DLL? Ideally, I'd like to just distribute my .dll (and the .lib, I suppose) to my friends so they can use it in their own projects.
I realize there are other walkthroughs out there (some of them on SO), but they all require editing the PATH environment variable, etc. Is that really the simplest way?
At a minimum, you need to do the following:
Include the .lib file in the project
Tell the linker where you put the .lib file (library search path)
Make the .dll file available at runtime (easiest is to put it in the same directory as the .exe)
To distribute the compiled .dll to your friends, you will need to include:
the .h file(s) for the compiler
the .lib file for the linker
the .dll file for runtime