Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Building shared libraries with Insure++ under AIX

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in Insure++
Building shared libraries with Insure++
Building shared libraries with Insure++ under AIX:


The Makefile below has been modified to build a shared library with Insure++. The foo.c and Foo.C source files are first compiled into an object (using insure), then linked into a shared object , and finally archived into the shared library. The shared library can then be linked with the main program.

If you build C++ shared libs using /usr/ibmcxx/bin/makeC++SharedLib. Copy the makeC++SharedLib file, give the file you copied a name you are comfortable with, for example, makeC++SharedLib_ins. Now you have two files the makeC++SharedLib_ins file will work exclusively with Insure, and the makeC++SharedLib file will work without Insure. Modify the new makeC++SharedLib_ins file, changing the LD line

From:
LD=ld
To:
LD=ins_ld. .

After modifying the makeC++SharedLib_ins file, you need to change a few lines in your Makefile.

CC = insure
LD= ins_ld
CXX = insure xlC
LXX =/usr/ibmcxx/bin/makeC++SharedLib_ins


Makefile
--------------------------------------------
CC = insure
LD = ins_ld
CXX = insure xlC
LXX=/usr/ibmcxx/bin/makeC++SharedLib_ins
PRIO = -p 5


all : testfoo testFoo


testfoo: testfoo.c libfoo.a
$(CC) -g -o testfoo testfoo.c -L. -lfoo

libfoo.a: foo.c
$(CC) -c -g foo.c
$(LD) -o foo-shr.o -bM:SRE -bE:foo.exp foo.o -lc
$(AR) ruv libfoo.a foo-shr.o

# For C++ Files

testFoo: libFoo.a
$(CXX) -g -o testFoo testfoo.c -L. -lFoo

libFoo.a: Foo.C
$(CXX) -c -g Foo.C
$(LXX) $(PRIO) -o Foo-shr.o -bM:SRE -bE:foo.exp Foo.o -lc
$(AR) ruv libFoo.a Foo-shr.o

--------------------------------------end-------------------------------------------------


foo.exp (Export file)
------------cut------------
#!
foo
----------end------------

foo.c (C example)
------------cut---------------------------------
/* foo.c */

#include <stdio.h>
char *foo(char *s)
{
#ifdef __INSIGHT__
printf("foo(%s) (INSIGHT)\n", s);
#else
printf("foo(%s)\n", s);
#endif
return strdup(s);
}
------------------end--------------------

Foo.C (C++ example)
--------------------cut-------------------------
/* Foo.C */

#include <stdio.h>
#include <string.h>

struct Foo {
Foo(char *p) {
printf("Foo::Foo(%s)\n", p);
}
~Foo() { printf("Foo::~Foo()\n"); }
};

Foo f("hello, there");

extern "C"
char *foo(char *s)
{
#ifdef __INSIGHT__
printf("foo(%s) (INSIGHT)\n", s);
#else
printf("foo(%s)\n", s);
#endif
return strdup(s);
}
-----------------end--------------------

testfoo.c (Main example program)
-------------------cut--------------------
/* testfoo.c */
#include <stdio.h>

extern char *foo(char *);
extern char *bar(char *);

main(int argc, char *argv[])
{
int i;

for (i = 0; i < argc; i++) {
char *s = foo(argv[i]);
if (i > 2)
free(s);
}
return 0;
}
------------------end---------------------