Using external functions in a VA Smalltalk application : Accessing external functions : Accessing C and COBOL : Building a Dynamic Link Library

Building a Dynamic Link Library
Also, before you begin this example, you need to compile and link a C or COBOL function into a Dynamic Link Library (DLL).
If you already have a function that you would like to practice with, you might prefer to substitute yours in place of our sample. Otherwise, build a new DLL, using the source files provided below. See your compiler and toolkit documentation for guidance on building DLLs. You should also refer to VA Smalltalk Base Programmer's Reference for guidance on the compiler options required by VA Smalltalk. If you are running on AIX you should also see Building an AIX DLL.
Windows icon
These sample files work with VA Smalltalk for OS/2. If you are using VA Smalltalk for Windows, you can make minor modifications to the source code and create your own .def and .mak files, or you might prefer to use your own function in your own DLL. For an example of .def and .mak files for Windows, see Preparing images.
If you build these source files in a directory other than the directory where VA Smalltalk is installed, be certain that the include files or copybooks are in a directory that is in your INCLUDE or COBCPY path, respectively.
For your convenience, we have provided all the source files in the online version of this book, so you can copy them and do not have to retype them. When you paste the text into your editor, ensure that you remove any extra spaces, if necessary, especially in the make file and the command file. For the atm.cbl file, ensure it holds to COBOL column conventions.
After you have built the source files, you can proceed to Adding the parts.
Building an AIX DLL
UNIX icon
This section applies only to VA Smalltalk for AIX.
Some Smalltalk environments require you to relink the virtual machine to access external functions. However, VA Smalltalk gives you the ability to link to libraries dynamically, provided the following steps are taken when the library is built.
1.
| temp |
temp := AbtCLangParser parseFile: 'catm.h'.
temp generateEntryTableForSource: 'catm.h' asTarget: 'catmtab.c'
The output is a C source file that inserts a table into your library when the file is compiled and linked into your library. VA Smalltalk uses this table to access the functions in the library. For this example, the output will look like the following:
#include <esuser.h>
#include <catm.h>
EsDefinePrimitiveTable(Catm)
EsPrimitiveTableEntry ("Atm", atm)
EsEndPrimitiveTable
The entry point that VA Smalltalk will be able to call directly is Atm.
2.
MODULE_NAME = catm.w # the 'DLL'
TABLE_NAME = Catm # declared as the 'entry point' to "ld"
USER_OBJS = catm.o catmtab.o # my dispatch table and functions
SYSTEM_INCLUDE = -I/build/make.rs6000.vm/include
USER_INCLUDE = -I.
LDFLAGS = -H512 -T512 # Don't forget these!
CFLAGS = -O -s $(SYSTEM_INCLUDE) $(USER_INCLUDE)
LIBS = -lc
$(MODULE_NAME): $(USER_OBJS)
ld -o $@ $(USER_OBJS) $(LDFLAGS) $(LIBS) -e$(TABLE_NAME)
 
Note:
You will need to remove the cdecl from the function declarations in the sample files that follow if your compiler does not support this keyword.
After you have successfully relinked your library you can proceed to Adding the parts.