For a general discussion on libraries check the post here.
In this tutorial we want to
1) create a static library .lib
using an object built from an Assembly .asm
file
2) create a .inc
file holding the macros that are going to be used in the main program
3) crate the main assembly program that includes the .inc
file just made
The actual files we are going to deal with are the following:
-
groucho32.asm
-> This will be compiled into an object. Out of the object thegroucho32.lib
is going to be built -
groucho32.inc
-> This includes macros that will be included into our main program. In turn, these macros use the procedures defined intogroucho32.asm
and compiled intogroucho32.lib
-
useGroucho32.asm
-> This is our main program. It includes the macros defined ingroucho32.inc
and will, therefore, call the procedures compiled ingroucho32.lib
The programs
useGroucho32.asm
groucho32.inc
groucho32.asm
All the steps in Visual Studio 2019
Creating the static library
The first step is to create a new static library project.
We clean the initial project of all the unnecessary components that VS has included due the project template we chose. Since we are not working with a library built out of C++ source code we can get rid of some stuff.
Right clicking on the solution, we go to build dependencies -> build customizations
and make sure that masm
is included into the dependencies.
We then add a new file to our solution and name it groucho32.asm
. The object that the compiler will create out of this file is going to be part of the groucho32.lib
library.
We make some adjustments to the project solution properties. We want the .lib
output to be saved into a subfolder of the C:
root and change the target name to groucho32.lib
We are now set for the building of the project. Once built we find that our .lib
static library has been created into the desired destination.
Creating the main program
We now setup a new empty project and clean all the unnecessary components as done in the previous case.
We add useGroucho32.asm
to our solution. We make sure that masm
is included into the build dependencies. We also right click on useGroucho32.asm
file and check that it has been included into the building process.
What are the peculiarities of useGrucho32.asm
?
Let us leave aside for a moment the importing of the Irvine library, we see that the program is including groucho32.inc
and groucho32.lib
.
From groucho32.inc
our program is going to take the mWriteStringToConsole
macro. The text in the macro’s body will be expanded into useGroucho32.asm
before compilation at the place where it has been used.
This means that we need to tell our project where to go to find groucho32.inc
.
In turn mWriteStringToConsole
is calling a procedure, namely GetLenString
, that is just declared but not defined. Where is the body of the procedure been implemented? In groucho32.asm
!
But this file was created in our previous project. Suppose we do not have access to the source code but just to the .lib
library that has been created out of it. We then need to include groucho32.lib
into our useGroucho32.asm
and to tell the building process where to look for such a library.
For explanatory sake lets have both the .lib
and .inc
files in the same folder. We take groucho32.inc
and save it into the same location where the process of library creation produced groucho32.lib
.
We then go to the solution properties and tell all the required information for the building to occur.
Our program also depends on the Irvine library. Therefore together with the information about the Groucho library we tell the build manager where to look for all the dependencies (includes and libraries).
The first thing we do is telling the Assembler where to look for include files. We do it in the MASM
tab of the solution properties
We fill the information in
Recall that includes are a matter of the Assembler due to the fact that everything happens before compilation. On the other hand libraries are food for the linker, which creates an executable file out of compiled objects.
Once done with include files we move on the libraries. We go to the Linker -> General
tab in solution properties and specify the location for additional directories where dependencies could be found.
We are not set yet. We need to specify which additional libraries are going to be imported from the directories just referenced.
We go to the Linker -> Input
tab of the solution properties and add the groucho32.lib
and irvine32.lib
file.
Now everything is ready for the build to run. The following output is telling us that the process was successful.
We can debug the programm and see the following Console output.