C/C++ and Fortran

 View Only

Invoking Open XL C/C++ with GNU Libtool

By Aaron Liu posted 13 days ago

  

GNU Libtool is a tool that allows package developers to provide generic shared library support. It was created to address the software portability problem when compiling shared libraries from source code. This blog demonstrates how to invoke Open XL C/C++ with GNU Libtool.

The GNU Libtool used in this blog was built using the latest stable release libtool-2.4.6 source code on AIX 7.2 TL5, and we name it as libtool.openXL. We pass `M4=/path/to/bin/m4`, `CC=/path/to/bin/ibm-clang`, `CXX=/path/to/bin/ibm-clang++_r` and option `--with-aix-soname=aix` to the config when we built libtool.openXL.


The source code we are using are the following foo.c and hello.c:

$ cat foo.h

#define HELLO_RET 0xe110
#define FOO_RET 0xf00

extern int foo();
extern int hello();

$ cat foo.c

#include <stdio.h>
#include <math.h> 
#include "foo.h"

int foo() {
printf ("cos (0.0) = %g\n", cos (0.0));
  return FOO_RET;
}

int hello() {
  printf ("** This is libfoo **\n");
  return HELLO_RET;
}

$ cat hello.c

#include <stdio.h>
#include "foo.h"

int main () {
  int value;
  printf ("Invoking Open XL C/C++ with GNU Libtool!\n");
  value = hello();
  printf ("hello returned: %i\n", value);

  if (value == HELLO_RET)
    printf("hello is ok!\n");

  if (foo () == FOO_RET)
    printf("foo is ok!\n");

  return 0;
}

For the above foo.c and hello.c, we will demonstrate how to invoke Open XL C/C++ with GNU Libtool by creating object files, linking libraries, linking executables, debugging executables and installing libraries.

 

------------------------------------------------------------------------------------------

 

I Create object files

 

In the following we pass options `--tag=CC --mode=compile /path/to/bin/ibm-clang -g -O -c` to Libtool as follows.

 

./libtool.openXL --tag=CC --mode=compile /path/to/bin/ibm-clang -g -O -c foo.c

Output:

libtool.openXL: ibm-clang -g -O -c foo.c  -fPIC -DPIC -o .libs/foo.o

 

On AIX, Libtool by default builds shared library object files with position-independent code (PIC), and stores them into the subdirectory `.libs`.


If you also want to build static libraries, you can change `build_old_libs=no` into `build_old_libs=yes` in the Libtool config which is inside libtool.openXL itself. The result should look like:

./libtool.openXL --tag=CC --mode=compile /path/to/bin/ibm-clang -g -O -c foo.c

Output:

libtool.openXL: compile:  /path/to/bin/ibm-clang -g -O -c foo.c  --DPIC -o .libs/foo.o

libtool.openXL: compile:  /path/to/bin/ibm-clang -g -O -c foo.c -o foo.o >/dev/null 2>&1

So the shared library object files built with PIC are in the .libs subdirectory, and the static ones are in the current directory.

In the above we invoked Open XL C using Libtool, if you want to invoke Open XL C++, you can pass `--tag=CXX --mode=compile /path/to/bin/ibm-clang++_r` options to Libtool as follows.

 

./libtool.openXL --tag=CXX --mode=compile /path/to/bin/ibm-clang++_r -g -O -c foo.cpp

As invoking Open XL C and Open XL C++ are similar, we only show the result of invoking Open XL C using GNU Libtool.

Similarly, we create shared library object files and static library object files for hello.c using the following commands:

./libtool.openXL --tag=CC --mode=compile /path/to/bin/ibm-clang -g -O -c hello.c

Output:

libtool.openXL: compile:  /path/to/bin/ibm-clang -g -O -c hello.c  -fPIC -DPIC -o .libs/hello.o

libtool.openXL: compile:  /path/to/bin/ibm-clang -g -O -c hello.c -o hello.o >/dev/null 2>&1

 

------------------------------------------------------------------------------------------

II Link libraries

 

./libtool.openXL --tag=CC --mode=link /path/to/bin/ibm-clang -g -O -o libhello.la foo.lo hello.lo -rpath /usr/lib -lm

Output:

libtool.openXL: link: `func_echo_all /usr/bin/nm -B | /usr/bin/sed -e 's/B\([^B]*\)$/P\1/'` -PCpgl  .libs/foo.o .libs/hello.o   | awk '{ if ((($ 2 == "T") || ($ 2 == "D") || ($ 2 == "B") || ($ 2 == "W") || ($ 2 == "V") || ($ 2 == "Z")) && (substr($ 1,1,1) != ".")) { if (($ 2 == "W") || ($ 2 == "V") || ($ 2 == "Z")) { print $ 1 " weak" } else { print $ 1 } } }' | sort -u > .libs/libhello.exp

libtool.openXL: link: rm -f -r .libs/libhello.a.d

libtool.openXL: link: mkdir .libs/libhello.a.d


libtool.openXL: link: /path/to/bin/ibm-clang -shared -o .libs/libhello.a.d/libhello.so.0  .libs/foo.o .libs/hello.o   -lm -lc -Wl,-bnoentry `func_echo_all " -g -O   " | /usr/bin/sed -e "s%-brtl\\([, ]\\)%-berok\\1%g"`-Wl,-bE:.libs/libhello.exp -Wl,-berok

libtool.openXL: link: ar cru .libs/libhello.a .libs/libhello.a.d/libhello.so.0

libtool.openXL: link: mv -f .libs/libhello.a.d/libhello.so.0 .libs

libtool.openXL: link: rm -f -r .libs/libhello.a.d

libtool.openXL: link: ar cru .libs/libhello.a  foo.o hello.o

libtool.openXL: link: ranlib .libs/libhello.a

libtool.openXL: link: ( cd ".libs" && rm -f "libhello.la" && ln -s "../libhello.la" "libhello.la" )

Notice Libtool invoke Open XL C using option `-shared` to get shared libraries, and use .la file to remember the destination directory for the library (the argument to -rpath) as well as the dependency on the math library (‘-lm’).  Note also that libtool creates extra files in the .libs subdirectory, rather than the current directory, which make it easier to clean up the build directory.

By the way, if you want to pass a linker-specific flag directly to the linker, you can use the option `-Wl,flag`. For example, the user can add `-Wl,-G` to the command line to allow runtime linking.

------------------------------------------------------------------------------------------

III Link executables

./libtool.openXL --tag=CC --mode=link /path/to/bin/ibm-clang -g -O -o hello hello.o libhello.la

Output:

libtool.openXL: link: /path/to/bin/ibm-clang -g -O -o .libs/hello hello.o  -L./.libs -lhello -lm

Notice that Libtool remembered that libhello.la depends on -lm, so even though we didn’t specify -lm on the Libtool command line, Libtool has added it to the Open XL C link line for us. After the above command, Libtool created executables `hello` in the shared library subdirectory `.libs` as well as in the current directory.

The following is the output by running command `./hello` or `./.libs/hello`:

Invoking Open XL C/C++ with GNU Libtool!

** This is libfoo **

hello returned: 57616

hello is ok!

cos (0.0) = 1

foo is ok!

Invoking `ldd ./.libs/hello`, shows that the created executable has a dependency on the shared library `libhello.a` we just created:

./.libs/hello needs:

         /usr/lib/libc.a(shr.o)

         ./.libs/libhello.a(libhello.so.0)

         /unix

         /usr/lib/libcrypt.a(shr.o)

------------------------------------------------------------------------------------------

 

IV Debug executables

It doesn’t work if we directly debug the executable `hello` in the current directory. Because the current directory `hello` itself is actually a temporary wrapper script for .libs/hello, not the real executable, GDB doesn’t know where the executable lives, and will complain `not in executable format: file format not recognized` as follows.

 

$ gdb hello

GNU gdb (GDB) 10.2

Copyright (C) 2021 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Type "show copying" and "show warranty" for details.

This GDB was configured as "powerpc64-ibm-aix6.1.0.0".

Type "show configuration" for configuration details.

For bug reporting instructions, please see:

<https://www.gnu.org/software/gdb/bugs/>.

Find the GDB manual and other documentation resources online at:

    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".

Type "apropos word" to search for commands related to "word"...

"fffffffffffef08s": not in executable format: file format not recognized

(gdb) quit

 

But if you debug through Libtool using `./libtool.openXL --mode=execute gdb hello` or debug the executable `hello` in `.libs/hello`, it will give you correct result as follows.

$ ./libtool.openXL --mode=execute gdb hello

GNU gdb (GDB) 10.2

Copyright (C) 2021 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Type "show copying" and "show warranty" for details.

This GDB was configured as "powerpc64-ibm-aix6.1.0.0".

Type "show configuration" for configuration details.

For bug reporting instructions, please see:

<https://www.gnu.org/software/gdb/bugs/>.

Find the GDB manual and other documentation resources online at:

    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".

Type "apropos word" to search for commands related to "word"...

Reading symbols from /home/aaron/work/try_libtool/openXL/.libs/lt-hello...

(gdb) break main

Breakpoint 1 at 0x100006a8: file hello.c, line 8.

(gdb) run

Starting program: /home/aaron/work/try_libtool/openXL/.libs/lt-hello

Breakpoint 1, main () at hello.c:8

8         printf ("Welcome to GNU libtool cdemo!\n");

(gdb) quit

A debugging session is active.

        Inferior 1 [process 46662372] will be killed.

Quit anyway? (y or n) y

------------------------------------------------------------------------------------------

 

V Installing libraries

The following Libtool installation command shows how we install Libtool library libhello.la into the sub directory `/usr/lib`.

$ ./libtool.openXL --mode=install cp libhello.la /usr/lib/libhello.la

Output:

libtool.openXL: install: cp .libs/libhello.a /usr/lib/libhello.a

libtool.openXL: install: cp .libs/libhello.lai /usr/lib/libhello.la

libtool.openXL: install: cp .libs/libhello.a /usr/lib/libhello.a

libtool.openXL: install: chmod 644 /usr/lib/libhello.a

libtool.openXL: install: ranlib /usr/lib/libhello.a

libtool.openXL: warning: remember to run 'libtool.openXL --finish /usr/lib'

Notice that the command is simple, and Libtool hides the obscure steps behind the scene and make our life easier.


------------------------------------------------------------------------------------------

 

References

GNU: https://www.gnu.org/

GNU Libtool: https://www.gnu.org/software/libtool/

Open XL C/C++ Product Page: https://www.ibm.com/products/xl-cpp-aix-compiler-power

Open XL C/C++ Documentation: https://www.ibm.com/docs/openxl-c-and-cpp-aix

0 comments
213 views

Permalink