Listing 6: ex2lnx.cpp, C++ file on Linux.

// ex2lnx.cpp  —  Test file for passing a single complex argument to an 
// F77 function in a dll, and getting the returned complex value
#include <complex>
#include <iostream>
#include <dlfcn.h>
int main()
{
    void *hDll = dlopen("./ex2.so", RTLD_NOW);
    typedef std::complex<float> (* LPFNDLLFUNC) (std::complex<float>&);
    LPFNDLLFUNC lpFunc = NULL;
    lpFunc = (LPFNDLLFUNC)dlsym(hDll, "foo_");
    std::complex<float> x, y;
    x = std::complex<float>(1.0, 2.0);
    y = lpFunc(x);
    std::cout << "y = foo(" << x << ") = " 
              << y << std::endl;
    dlclose(hDll);
    return 0;
}