2011-12-07 21 views
2

私はCreating Library with backward compatible ABI that uses Boostのスレッドを読んでいますが、安定したABIを維持するために共有ライブラリをどのようにリンクするべきかを理解しようとしています。共有ライブラリとLinux(elf)のリンク

cat <<EOF > a.c 
#define ABI __attribute__((visibility("default"))) 

int common(); 
int ABI a() { return common() + 1; } 
EOF 

cat <<EOF > b.c 
#define ABI __attribute__((visibility("default"))) 

int common(); 
int ABI b() { return common() + 2; } 
EOF 

cat <<EOF > common_v1.c 
int common() { return 1; } 
EOF 

cat <<EOF > common_v2.c 
int common() { return 2; } 
EOF 

cat <<EOF > test.c 
#include <assert.h> 

int a(); 
int b(); 

int main(int argc, const char *argv[]) 
{ 
    assert(a() + b() == 6); 
    return 0; 
} 
EOF 

cat <<EOF > CMakeLists.txt 
cmake_minimum_required(VERSION 2.8) 

project(TEST) 

add_library(common_v1 STATIC common_v1.c) 
add_library(common_v2 STATIC common_v2.c) 

SET_SOURCE_FILES_PROPERTIES(a.c b.c COMPILE_FLAGS -fvisibility=hidden) 
add_library(a SHARED a.c) 
target_link_libraries(a common_v1) 

add_library(b SHARED b.c) 
target_link_libraries(b common_v2) 

add_executable(test test.c) 
target_link_libraries(test a b) 
EOF 

は、ライブラリがcommon_v1とcommon_v2は、ライブラリと(ブーストなど)Bの外部依存関係をエミュレートする必要があります。

私は、次の簡単なテストプロジェクトを作成しました。 common_v1とcommon_v2は外部ライブラリとみなされるので、私はビルドシステムを変更したくないです(コンパイルされたフラグは変更しないでください)。

上記のプロジェクトはうまくコンパイルされますが、機能しません。テストアプリケーションが実行されると、テストアプリケーションはassertステートメントにジャンプします。

私は、共通の定義がlibaとlibbの両方で使用されていると考えています。なぜこれが当てはまり、私は何が間違っているのですか?

答えて

0

あなただけa()b()記号をごabライブラリを作成し、保持するときcommon()シンボルはこれらのライブラリによってエクスポートされません(したがって、1つのライブラリがしようとしないように、ld--retain-symbols-fileオプションを使用してテストプログラムを修正することができます別のcommon()記号)を使用する:

 
    --version-script=version-scriptfile 
     Specify the name of a version script to the linker. 

 
    --retain-symbols-file filename 
     Retain only the symbols listed in the file filename, discarding all 
     others. filename is simply a flat file, with one symbol name per line. 

はまた、あなたは--version-scriptオプションを使用することができます

 
    FOO { 
    global: a; b; # symbols to be exported 
    local: *;  # hide others 
    }; 

関連スレッド:

version-scriptfileは、以下である

関連する問題