www.delorie.com/gnu/docs/dld/dld_15.html   search  
 
Buy GNU books!


Dld

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.10 Linking Other Languages

The easiest way to link in functions from other (i.e. not C) languages is to write a C function that makes the necessary calls to the other language. This is simpler than trying to link modules written in the other language directly, since you do not need to know what the actual symbol names are in the other language.

Every C++ compiler, for example, uses a different name mangling system, and it would be almost impossible for dld to know about them all (though a future version of dld should support the mangling systems of popular C++ compilers).

Here is how a C interface function can be used to let dld link in methods from a C++ class, no matter what mangling system is used:

 
// Declaration of class Foo.
class Foo
{
  Foo ();               // constructor
  Foo (Foo other);      // copy constructor
  ~Foo ();              // destructor
  int member_func ();   // member function

 private:
  int my_member;        // member variable
};

// Declare call_foo to be a C (unmangled) symbol.
extern "C" int call_foo (Foo a);

int
call_foo (Foo a)
{
  Foo b (a);            // use copy constructor
  Foo c;                // use constructor

  // Call member function.
  return c.member_func ();

  // Destructor is implicitly called for b and c.
}

Simply use dld to load and link the C function, use_foo, and dld will automatically resolve the references to the C++ member functions that use_foo contains.

If you do know the symbol names of functions in the other language, then you may use them directly as arguments to dld functions. You can investigate what symbol names are by running nm objfile.o where `objfile.o' is the name of the module in which the functions are defined.

As an example, here is how to find out what naming conventions your Fortran-77 compiler uses (this example was run on SunOS 4.1.3):

 
bash$ cat coef.f
**********************************************************
* COEF generates the coefficients and store them in the
* Y array for Newton's interpolation polynomial
**********************************************************

      subroutine coef(n,x,y)
      dimension x(n), y(n)
      do 2 j=1,n-1
      do 2 i=1,n-j
 2       y(n-i+1)=(y(n-i+1)-y(n-i))/(x(n-i+1)-x(n-i-j+1))
      return
      end
bash$ f77 -c coef.f
coef.f:
	coef:
bash$ nm coef.o
000001a8 b VAR_SEG1
00000000 T _coef_
bash$

For the SunOS 4.1.3 `nm', the `T' symbol type means that a global symbol is defined in that file. So, SunOS `f77' uses a leading and trailing underscore for global symbols.

Use the symbol coef_ as the argument to dld_get_func. Note: omit the leading underscore, because C functions, by convention, use a leading underscore and dld is written to automatically add it when dld_get_func is called.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003