3

ビルド システムから吐き出される (C++ からの) オブジェクト ファイルがいくつかあります。それらextern "C"には、プログラムで使用したいいくつかのリンケージシンボルがあり、dlopen/を介しdlsymて他の場所からアクセスできます。

gcc を使用して実行可能ファイルにコンパイルすると、これらのシンボルは使用リストに表示されませんnm -D <executable-here>(つまり、これらは動的シンボルではありません)。

コンパイルされた実行可能ファイル内で動的シンボルとして公開されるようにするにはどうすればよいですか?

オブジェクト ファイルと実行可能ファイルのビルド フラグを変更することはできますが、C++ ファイルを最終的な実行可能ファイルにする方法を変更する (つまり、最初にオブジェクト ファイルにしない) のは困難です。

(GCC 4.8、ld 2.24)

編集:私はこの質問に出くわしました. 静的バイナリで dlsym を使用する

4

1 に答える 1

3

--export-dynamicld オプションを見てみたいと思うかもしれません:

   -E
   --export-dynamic
   --no-export-dynamic
       When creating a dynamically linked executable, using the -E option
       or the --export-dynamic option causes the linker to add all symbols
       to the dynamic symbol table.  The dynamic symbol table is the set
       of symbols which are visible from dynamic objects at run time.

       If you do not use either of these options (or use the
       --no-export-dynamic option to restore the default behavior), the
       dynamic symbol table will normally contain only those symbols which
       are referenced by some dynamic object mentioned in the link.

       If you use "dlopen" to load a dynamic object which needs to refer
       back to the symbols defined by the program, rather than some other
       dynamic object, then you will probably need to use this option when
       linking the program itself.

       You can also use the dynamic list to control what symbols should be
       added to the dynamic symbol table if the output format supports it.
       See the description of --dynamic-list.

       Note that this option is specific to ELF targeted ports.  PE
       targets support a similar function to export all symbols from a DLL
       or EXE; see the description of --export-all-symbols below.

また、リンク中にどのオブジェクトも extern シンボルを参照し--dynamic-listない場合は、エクスポートされることを確認するためにそれらを に配置することをお勧めします。


例:

$ cat test.cc
#include <stdio.h>

int main() {
    printf("Hello, world\n");
}

extern "C" void export_this() {
    printf("Hello, world from export_this\n");
}

$ g++ -o test -W{all,extra} -Wl,--export-dynamic test.cc

$ ./test
Hello, world

$ nm --dynamic test | grep export_this
00000000004007f5 T export_this # <---- here you go
于 2014-10-10T08:32:51.450 に答える