[comp.os.vms] Dynamic Linking possible???

carl@CITHEX.CALTECH.EDU (Carl J Lydick) (07/12/88)

 >   Is dynamic linking possible on VMS and BSD Unix, in  other  words  can  I
 > load in routines on the fly and call them?  I want my C program to generate
 > some more C code, and then link it in with the already running program.

Yes, it is.  With VMS v4.something, DEC introduced a library routine called
LIB$FIND_IMAGE_SYMBOL.  What it does is to load a shareable image into memory
and map a specified symbol from that image.  So what your C program has
to do is:
	1)  Create a file with the new code;
	2)  Create a command procedure to compile and link the new image;
	3)  Define a logical name to point to the image (this can be done
	    from the command procedure by using the job logical name table);
	4)  Use LIB$FIND_IMAGE_SYMBOL to load and map the new routine.

The following program does just that.  Hope this helps.

/* Program to demonstrate LIB$FIND_IMAGE_SYMBOL and creation of shareable
 *	images from C code
 */
#include descrip
#include stdio
main()
{	FILE *fp;
	$DESCRIPTOR(file, "NEW_PROGRAM");
	$DESCRIPTOR(symb, "test");
	int (*subr)();
	fp = fopen("new_program.c", "w");
	fprintf(fp,"test()\n");
	fprintf(fp,"{	puts(\"This is a test\");\n");
	fprintf(fp,"}\n");
	fclose(fp);
	fp = fopen("link.com","w");
	fprintf(fp,"$ SET VERIFY\n");
	fprintf(fp,"$ cc new_program\n");
	fprintf(fp,"$ LINK/SHARE new_program,SYS$INPUT:/OPT\n");
	fprintf(fp,"SYS$SHARE:VAXCRTL/SHAR\n");
	fprintf(fp,"UNIVERSAL=test\n");
	fprintf(fp,"$ DELETE new_program.OBJ;\n");
	fprintf(fp,"$ DEF/JOB NEW_PROGRAM 'f$ENV(\"DEFAULT\")'NEW_PROGRAM\n");
	fprintf(fp,"$ DELETE LINK.COM;\n");
	fclose(fp);
	system("@LINK.COM");
	LIB$FIND_IMAGE_SYMBOL(&file, &symb, &subr);
	(*subr)();
}