Should I include stdafx.h within my static library? What format should my library be? - static-libraries

I am creating a static library. I have gone through the win32 application, static library, no precompiled headers. The file is a .cpp I am assuming that I can export this to the actual .lib file, or is it suppose to be .lib from the start?
Also, I keep getting the cannot open source file stdafx.h, which I have included within my main static library .cpp file. Tell me, am I suppose to do this?
Thanks

Related

Why does VS not generate a .lib file if there is no .cpp file?

I am trying to create a simple 2D library, consisting entirely of class templates. I created an empty project, changed configuration type to static library, and then added my header files. It currently looks like this:
When I try to build this project, the build succeeds but no .lib file is actually created.
When googling for a way to fix this, I found this question and this answer, which refers to the same problem of not being able to generate a .lib file because the project contains no .cpp file.
The answer says that they solved it by adding a .cpp file that just includes all the header files:
And this does indeed solve the issue:
But surely there's a more elegant way of solving this? Why doesn't VS want to generate a .lib file if you don't do this?

Is it possilble to link .so file into .a file

Is it possilble to link .so file into .a file
A 3rd party only offer a so
Our .a lib depend on it.
And we don't want to give customer too much dependency
No, you can't convert a shared lib back to a static lib. There is information in the .o files that gets discarded when they are welded together into a .so file. If you really want static linking, you'll have to persuade the third party to also ship you an archive.

Class library lib and dll files

To use class library I must have header file and lib file that I suppose contains compiled library code. So, why and when I need dll file? I have breath understanding that in case of dynamic linking I must use dll and in case of static linking there is no need to use dll.
You can have a library project or a DLL project. A DLL is good if it will be used by multiple exes. A lib is good if you want it to become part of an exe.
DLL projects generate both a DLL file and a lib file. The import lib file is very small and just contains a jump table so the exe can be compiled.
When your library is dynamic library i.e, .dll, .lib file have exports table. .h header file have function prototype.
Export table is table of all exported functions from dll.

Two different static libs with the same .obj file

I'm working with visual studio,
and I have two different static libs which share the same .obj file, but that .obj file is newer in one of them.
I need both of these libs so i can't exclude one of them,
How can I "make" the linker to take this specific .obj file from the static lib I want him to?
It's either that or i will have to add the .obj file every time he has been changed, cause it's location is based on the version number while the lib file is in a global location.
Unless there's other solution.

What do you need in a static library?

I want to try making a simple game engine. Just something that handles states, assets, characters/actors and their stats and an inventory. Most of the code I can take from other games I've wrote, but I'm confused on how I then turn it into a static library. Do I need a main.cpp? If so what has to go in it? Under Linux I'm guessing I compile it to .so and add the headers to my include directory and then just link to the .so but what do I do on Windows and Mac?
A .so is not a static library, it's a dynamic one. A static library is, in its most basic, a .o file compiled from a single C file, or a .a file which is simply a collection of .o files.
A static library is different from a shared one in that the object code is linked directly in to the final executable, requiring no dependencies at run time.
Under Unix, the ar(1) command is used to bundle .o files in to a composite .a file. I do not know the comparable utility for Windows.
Once you have the .a file, you will simply need the combination of the .a file and the .h files to build your code. You use the .h files for compiling, and then link against the .a file.
Shared libraries have a specific advantage over static libraries in that if you have multiple, yet different, programs relying on the same libraries, the code from the shared libraries can be shared among all of the programs at the same time, so in that sense they lower the overall impact on the system. Their downside is slower start up times (though that's pretty marginal nowadays). Statically linked libraries can not be shared across independent programs, but if you run the same executable several times, its code will be shared.

Resources