2013-08-12 9 views
7

Napisałem proste aplikacje z Openssl w językach C i C++. Kompiluję je w ten sposób:Aplikacja do łączenia statycznego z Openssl C/C++ [x86_x64]

gcc openssltest.c -o openssltest -lcrypto 
g++ openssltest.cpp -o openssltest -lcrypto 

I wszystko w porządku, ale tylko, gdy masz zainstalowany Openssl.

Chciałbym skompilować, że mogę uruchomić je w systemie operacyjnym, gdzie nie ma openssl installend (system operacyjny podobny do linuksa). Próbowałem to:

gcc -c openssltest.c -lcrypto -static 
gcc openssltest.o -o openssltest -lcrypto -static 

i takie same dla C++:

g++ -c openssltest.cpp -lcrypto -static 
g++ openssltest.o -o openssltest -lcrypto -static 

Ale mają te błędy:

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': 
(.text+0x19): undefined reference to `dlopen' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': 
(.text+0x2c): undefined reference to `dlsym' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': 
(.text+0x37): undefined reference to `dlclose' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func': 
(.text+0x354): undefined reference to `dlsym' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func': 
(.text+0x3fb): undefined reference to `dlerror' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var': 
(.text+0x474): undefined reference to `dlsym' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var': 
(.text+0x52e): undefined reference to `dlerror' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': 
(.text+0x5a2): undefined reference to `dlopen' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': 
(.text+0x60b): undefined reference to `dlclose' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': 
(.text+0x638): undefined reference to `dlerror' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr': 
(.text+0x6cd): undefined reference to `dladdr' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr': 
(.text+0x731): undefined reference to `dlerror' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload': 
(.text+0x78a): undefined reference to `dlclose' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free': 
(.text+0x4d): undefined reference to `inflateEnd' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free': 
(.text+0x6b): undefined reference to `deflateEnd' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_ctrl': 
(.text+0x284): undefined reference to `deflate' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_ctrl': 
(.text+0x342): undefined reference to `zError' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_expand_block': 
(.text+0x411): undefined reference to `inflate' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_compress_block': 
(.text+0x4ca): undefined reference to `deflate' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_finish': 
(.text+0x51f): undefined reference to `inflateEnd' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_finish': 
(.text+0x528): undefined reference to `deflateEnd' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_init': 
(.text+0x5d7): undefined reference to `inflateInit_' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_init': 
(.text+0x659): undefined reference to `deflateInit_' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': 
(.text+0x893): undefined reference to `inflate' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': 
(.text+0x90d): undefined reference to `zError' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': 
(.text+0x97c): undefined reference to `inflateInit_' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': 
(.text+0xa6f): undefined reference to `deflate' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': 
(.text+0xaec): undefined reference to `zError' 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': 
(.text+0xb7e): undefined reference to `deflateInit_' 
collect2: error: ld returned 1 exit status 

Jak mogę zrobić to? Pamiętam, że jakiś czas temu zrobiłem to, ale teraz, erm, zapomniałem, jak to zrobić. Im na Ubuntu 13.04 x64 - czy to może być problem?

+0

spróbować dodać '' -ldl -lZ – billz

+0

@billz: 'gcc -static -o openssltest openssltest.c -ldl -lZ -lcrypto' daje same błędy Zamieściłem – yak

+1

-ldl -lZ powinny być po -lcrypto – billz

Odpowiedz

0

Poprawka: spróbuj również statycznie połączyć w libdl.

Jeśli to nie zadziała, IMHO Twoje libcrypto.a jest źle skompilowane.

-2

Miałem ten sam problem co ty. Oto polecenie, które rozwiązać go dla mnie:

gcc yourfile.c -o yourfile -static -lcrypto -lz -ldl 

Generuje to ostrzeżenie:

/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x1b): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking 

wykonywalny ale nadal działa.

+0

Tak długo, jak masz libgcc/libc libs łatwo dostępne na twoim urządzeniu docelowym, na pewno zadziała - ale jeśli nie, jesteś skazany na zagładę. W rzeczywistości oznacza to, że plik binarny nie jest całkowicie statyczny - również nie jest to rozwiązanie. – drahnr

+0

Mówisz "pewnie, że to zadziała", a następnie skończysz z "to nie jest rozwiązanie". W mojej książce, jeśli coś działa, jest to rozwiązanie. – schulwitz

+0

Ponieważ wykluczasz coś, co nie jest oczywiste. Nie każdy system (zwłaszcza wbudowany) niekoniecznie zawiera libgcc. Zauważ, że "na pewno to zadziała" jest częścią instrukcji "jeśli". – drahnr

1

Musisz mieć świadomość, że biblioteki muszą być dostępne jako statycznie połączone pliki * .a. Jeśli nie, kompilacja albo zawiedzie, albo skończy się dynamicznie połączonym plikiem wykonywalnym.

Jeśli jest zbyt dużo Pita (wszystkich bibliotek zależności muszą być statycznie skompilowany, a ich deps zbyt i tak dalej) użyć czegoś jak buildroot

2

Miałem podobny problem, starając się statycznie skompilować prosty DES program korzystający z biblioteki openssl. Użyłem "-lcrypto -lz -ldl -static-libgcc" i zadziałało to dla mnie. Brak ostrzeżeń i błędów.

+0

it 'works, Thanks! czy możesz po prostu zaktualizować swoją odpowiedź, aby wspomnieć, że zamówienie jest ważne. – Zskdan

Powiązane problemy