Shared Object from CPP

Shared Object from CPP

I have been trying to develop a shared object in Fortran and then invoke it from a CPP program.

I started with a simple Hello world example. The fortran code (foo.f90) was:

INTEGER :: index
do index = 1,ctr
write (*,*) "Hello World"
end do

This was then compiled using the

ifc -fPIC -shared -o foo.f90

The excerpts from the cpp file (firstdl.cpp) which invoked the DSO are

void* handle;
typedef void (*foo_t)(int);
char* error;
int i;
foo_t printer;
handle = dlopen ("/home/mohan/code/fortran/foo/", RTLD_LAZY);

printer = (foo_t) dlsym (handle, "foo_");

/* Call the function. */
printer(i) ;

Note that dlsym is passed foo_, because of the name decoration done by IFC. This was found out using
nm | grep goo

The cpp code was compiled using the command

gcc firstdl.cpp -ldl

When a.out was executed, I got the error message

Segmentation fault (core dumped)

The same code worked whe I was not passing any parameters from CPP to Fortran. ie the signature of foo was
void foo(void).

I really dont know what the problem might be. Also I am a newbie to Linux and this is the first code I am running using gcc/ifc.

Anuraag Mohan

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

By default, Fortran expects function parameters to be passed by reference, so you need void foo(&int)
(There are ways to get Fortran functions to accept arguments passed by value, but there's no need to complicate your life). See the section on mixed language programming in the Fortran User's Guide.


Leave a Comment

Please sign in to add a comment. Not a member? Join today