AK Linking Libraries
AK Linking Libraries
programming languages
http://sites.google.com/site/akohlmey/
[email protected]
Workshop on Computer Programming and
Advanced Tools for Scientific Research Work
Symbols in Object Files & Visibility
● Compiled object files have multiple sections
and a symbol table describing their entries:
● “Text”: this is executable code
● “Data”: pre-allocated variables storage
● “Constants”: read-only data
● “Undefined”: symbols that are used but not defined
● “Debug”: debugger information (e.g. line numbers)
● Entries in the object files can be inspected with
either the “nm” tool or the “readelf” command
2
Example File: visbility.c
static const int val1 = -5;
const int val2 = 10;
static int val3 = -20;
int val4 = -15;
extern int errno;
12
Fortran 90+ Modules
● When subroutines or variables are defined
inside a module, they have to be hidden
module func
integer :: val5, val6
contains
integer function add_abs(v1,v2)
integer, intent(in) :: v1, v2
add_abs = iabs(v1)+iabs(v2)
end function add_abs
end module func
● gfortran creates the following symbols:
00000000 T __func_MOD_add_abs
00000000 B __func_MOD_val5
00000004 B __func_MOD_val6
13
The Next Level: C++
● In C++ functions with different number or type
of arguments can be defined (overloading)
=> encode prototype into symbol name:
Example : symbol for int add_abs(int,int)
becomes: _ZL7add_absii
● Note: the return type is not encoded
● C++ symbols are no longer compatible with C
=> add 'extern “C”' qualifier for C style symbols
● C++ symbol encoding is compiler specific
14
C++ Namespaces and Classes
vs. Fortran 90 Modules
● Fortran 90 modules share functionality with
classes and namespaces in C++
● C++ namespaces are encoded in symbols
Example: int func::add_abs(int,int)
becomes: _ZN4funcL7add_absEii
● C++ classes are encoded the same way
● Figuring out which symbol to encode into the
object as undefined is the job of the compiler
● When using the gdb debugger use '::' syntax
15
Why We Need Header or Module Files
● The linker is “blind” for any language specific
properties of a symbol => checking of the
validity of the interface of a function is only
possible during compilation
● A header or module file contains the prototype
of the function (not the implementation) and the
compiler can compare it to its use
● Important: header/module has to match library
=> Problem with FFTW-2.x: cannot tell if library
was compiled for single or double precision
16
Calling C from Fortran
● Need to make C function look like Fortran 77
=> provide a wrapper function visible in Fortran
● Append underscore
● Use call by reference conventions
● Best only used for “subroutine”
void add_abs_(int *v1,int *v2,int *res){
*res = abs(*v1)+abs(*v2);}
● Arrays are always passed as flat arrays
● String passing is tricky (no zero-termination)
(length typically appended to list of arguments)
17
Calling C from Fortran Example
void sum_abs_(int *in, int *num, int *out) {
int i,sum;
sum = 0;
for (i=0; i < *num; ++i) { sum += abs(in[i]);}
*out = sum;
return;
}
/* fortran code:
integer, parameter :: n=200
integer :: s, data(n)
call SUM_ABS(data, n, s)
print*, s
*/
18
Calling Fortran from C
● Inverse from above, i.e. need to add
underscore and use lower case
● Difficult for anything but Fortran 77 style calls
since Fortran 90+ features need extra info
● Shaped arrays, optional parameters, modules
● Arrays need to be “flat”,
C-style multi-dimensional arrays are lists of
pointers to individual pieces of storage, which
may not be consecutive
=> use 1d and compute position
19
Calling Fortran From C Example
subroutine sum_abs(in, num, out)
integer, intent(in) :: num, in(num)
integer, intent(out) :: out
Integer :: i, sum
sum = 0
do i=1,num
sum = sum + ABS(in(i))
end do
out = sum
end subroutine sum_abs
!! c code:
! const int n=200;
! int data[n], s;
! sum_abs_(data, &n, &s);
! printf("%d\n", s);
20