Static linking against the indigo cheminformatics package under Linux x64 complains undefined reference to __dso_handle when Free Pascal is used

StackOverflow https://stackoverflow.com/questions/23647522

Pregunta

Update 1

According to this SO post, Free Pascal may not link against C++ object file, which means static C++ libraries, when libstdc++.so.6.X is used as the C++ lib.

It should be noted that for that specific hello-world program in that SO post, Free Pascal can link against C++ object file using libstdc++.so.5 as the C++ lib. Note libstdc++.so.5 is with GCC 3.3.X.

Update 2

According to the Free Pascal future plans, Linking with C++ code is not until next major version is finished.

Update 3

According to "How to use C code in Free Pascal projects", Free Pascal cannot link in C++ objects directly. They should be placed into shared library to be used from Free Pasacal.


The source code of the indigo cheminformatics package can be compiled into static libraries and provides a plain-C API interface to its C++ core.

The procedure is shown:

cd indigo
module add gcc/4.7.2 # or gcc/4.3.2
module add cmake/2.8.8
python ./build_scripts/indigo-release-libs.py --preset=linux64

Part of the tree structure is shown:

indigo
./dist/indigo-libs-1.1.12-linux64-static.zip
./dist/LICENSE.GPL
./dist/indigo-inchi.h
./dist/indigo-renderer.h
./dist/indigo.h
./dist/static
./dist/static/Linux
./dist/static/Linux/x64
./dist/static/Linux/x64/libcairo.a
./dist/static/Linux/x64/libcommon.a
./dist/static/Linux/x64/libgraph.a
./dist/static/Linux/x64/libinchi.a
./dist/static/Linux/x64/libindigo-inchi-static.a
./dist/static/Linux/x64/libindigo-renderer-static.a
./dist/static/Linux/x64/libindigo-static.a
./dist/static/Linux/x64/liblayout.a
./dist/static/Linux/x64/libmolecule.a
./dist/static/Linux/x64/libpixman.a
./dist/static/Linux/x64/libpng.a
./dist/static/Linux/x64/libreaction.a
./dist/static/Linux/x64/librender2d.a
./dist/static/Linux/x64/libtinyxml.a
./dist/static/Linux/x64/libz.a

To compile .c and static link against the static indigo libraries:

Source code of a sample cTest.c file is shown:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    
#include "indigo.h"

int main (void) { printf("%s\n", indigoVersion()); }    

, the procedure is shown:

cd indigo/dist/static/Linux/x64
module add gcc/4.7.2 # or gcc/4.3.2
g++ -I../../../ cTest.c -o cTest libindigo-static.a libreaction.a liblayout.a libmolecule.a libgraph.a libcommon.a libz.a libtinyxml.a -lpthread

To compile .pp and static link against the static indigo libraries:

Source code of a sample fpcTest.pp file is shown:

program fpcTest;

{$MODE DELPHI}
{$APPTYPE CONSOLE}

uses
  SysUtils;

 // {$linklib stdc++}
 {$link /usr/lib64/libstdc++.so.6}
 {$linklib pthread}
 {$linklib libindigo-static}
 {$linklib libreaction}
 {$linklib liblayout}
 {$linklib libmolecule}
 {$linklib libgraph}
 {$linklib libcommon}
 {$linklib libz}
 {$linklib libtinyxml}

 function indigoVersion (): PChar; cdecl; external 'libindigo-static';

begin
  WriteLn(indigoVersion);
end.

, the procedure is shown:

cd indigo/dist/static/Linux/x64
~/fpc-2.6.4/bin/fpc  fpc_static.pp

However, it complains "undefined reference to __dso_handle". The Error msg is shown below:

Free Pascal Compiler version 2.6.4 [2014/03/03] for x86_64
Copyright (c) 1993-2014 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling fpc_static.pp
fpc_static.pp(4,2) Note: APPTYPE is not supported by the target OS
Linking fpc_static
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
./libindigo-static.a(option_manager.cpp.o): In function `_GLOBAL__sub_I_option_manager.cpp':
option_manager.cpp:(.text.startup+0x3): undefined reference to `__dso_handle'
./libindigo-static.a(indigo.cpp.o): In function `Indigo::init()':
indigo.cpp:(.text+0x3bf): undefined reference to `__dso_handle'
./libindigo-static.a(indigo.cpp.o): In function `_GLOBAL__sub_I_indigo.cpp':
indigo.cpp:(.text.startup+0x2e): undefined reference to `__dso_handle'
indigo.cpp:(.text.startup+0x5b): undefined reference to `__dso_handle'
./libcommon.a(profiling.cpp.o): In function `_GLOBAL__sub_I_profiling.cpp':
profiling.cpp:(.text.startup+0x22): undefined reference to `__dso_handle'
./libcommon.a(profiling.cpp.o):profiling.cpp:(.text.startup+0x40): more undefined references to `__dso_handle' follow
fpc_static.pp(25,1) Error: Error while linking
fpc_static.pp(25,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: ~/fpc-2.6.4/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)

Could you help to comment about the reason and the workaround ?

¿Fue útil?

Solución

I think in your references you are majorly confusing "pascal calling C++ classes" and "pascal and C++ libraries linked into the same project". update 1 is about the latter, update 2 is about the former, update 3 is old and not really related I guess.

My guess is that the modified FPC startup code that initializes libraries via CTOR and DTOR, ONLY initializes shared libraries, and not static libraries. IOW any C/C++ state linked into the main binary might not be initialized.

It will probably require an exact understanding of CTOR and DTOR internal working, and specially in combination with static linking to figure this out.

To make it clear: the "C++" support in trunk (on which the next FPC version will be based) is about calling (gcc) C++ classes directly. IOW making FPC understand gcc mangling (in some gcc version, since C++ mangling is usually version dependent long term). And while it is in the branch, it is not known yet if it will be considered finished and stable in the next fixes branch.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top