So verwenden Sie statisches Linken mit OpenSSL in C/C++

Lesezeit: 7 Minuten

Benutzeravatar von Yak
Yak

Ich habe einfache Apps mit Openssl in C und C++ geschrieben. Ich stelle sie so zusammen:

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

Und es ist alles in Ordnung, aber nur, wenn Sie Openssl installiert haben.

Ich möchte es so kompilieren, dass ich sie auf einem Betriebssystem ausführen kann, auf dem kein Openssl installiert ist (Linux-ähnliches Betriebssystem). Ich habe das versucht:

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

und dasselbe für C++:

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

Habe aber diese Fehler:

/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

Wie kann ich das machen? Ich erinnere mich, dass ich das vor einer Weile gemacht habe, aber jetzt, ähm, vergessen habe, wie es geht. Bin auf Ubuntu 13.04 x64 – Kann es ein Problem sein?

  • @billz: gcc -static -o openssltest openssltest.c -ldl -lz -lcrypto gibt dieselben Fehler, die ich gepostet habe

    – Jak

    12. August 2013 um 11:26 Uhr

  • -ldl -lz sollte nach -lcrypto stehen

    – billz

    12. August 2013 um 11:27 Uhr

  • @billz: gcc -o openssltest openssltest.c -lcrypto -ldl -lz -static gibt: /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function dlfcn_globallookup: (.text+0x19): warning: Using dlopen in statically linked applications requires at runtime the shared libraries from the glibc version used for linking aber ich verwende keine betriebssystemspezifischen Bibliotheken in meinem Code

    – Jak

    12. August 2013 um 11:30 Uhr


  • können Sie -static-libgcc anstelle von -static versuchen?

    – rakib_

    12. August 2013 um 11:46 Uhr

  • @rakib: sicher kann ich 😉 Habe das gemacht: gcc -o openssltest openssltest.c -lcrypto -static-libgcc ohne Fehler, aber ich bin mir nicht sicher, ob es sich um eine echte statische App handelt, da ihre Größe für eine statische Anwendung zu klein ist 🙁 Die Größe ist die gleiche, wie ich sie ohne verlinke -static Flagge

    – Jak

    12. August 2013 um 11:51 Uhr


Benutzeravatar von user4035357
Benutzer4035357

Ich hatte ein ähnliches Problem beim Versuch, ein einfaches DES-Programm mit der Openssl-Bibliothek statisch zu kompilieren. ich benutzte -lcrypto -lz -ldl -static-libgcc und es hat bei mir funktioniert. Keine Warnungen oder Fehler.

  • es funktioniert, danke! können Sie Ihre Antwort einfach aktualisieren, um zu erwähnen, dass die Bestellung relevant ist.

    – Zskdan

    5. Februar 2016 um 14:47 Uhr

  • Auf diese Weise wird es mit einer Erhöhung der ausführbaren Datei um 2 MB kompiliert und ausgeführt, aber es gibt auch diese Warnung: “Die Verwendung von ‘dlopen’ in statisch gelinkten Anwendungen erfordert zur Laufzeit die gemeinsam genutzten Bibliotheken aus der glibc-Version, die zum Linken verwendet wird”.

    – Hack06

    11. Juni 2018 um 21:35 Uhr

  • Es scheint bei mir nicht zu funktionieren. Ich verwende g++, falls das einen Unterschied macht. Macht der C++-Compiler die Dinge anders?

    – AjB

    12. November 2019 um 7:43 Uhr

  • Diese Antwort ist wahrscheinlich falsch. file <output> Auf der Ausgabebinärdatei des Kompilierungsprozesses steht eindeutig, dass die Binärdatei auf meinem Computer “dynamisch” verknüpft ist.

    – C–

    11. Dezember 2021 um 18:42 Uhr

Das muss man sich bewusst sein Bibliotheken müssen als statisch gelinkte *.a-Dateien vorliegen. Wenn dies nicht der Fall ist, schlägt die Kompilierung entweder fehl oder Sie erhalten eine dynamisch verknüpfte ausführbare Datei.

Wenn dies zu viel PITA wird (alle Bibliotheksabhängigkeiten müssen statisch kompiliert werden, und ihre Deps auch und so weiter), verwenden Sie so etwas wie buildroot

Ein Hotfix: Versuchen Sie, libdl auch statisch einzubinden.

Wenn dies nicht funktioniert, ist IMHO Ihre libcrypto.a schlecht kompiliert.

Benutzeravatar von schulwitz
Schulwitz

Ich hatte das gleiche Problem wie du. Hier ist der Befehl, der es für mich gelöst hat:

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

Es generiert diese Warnung:

/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

aber die ausführbare Datei funktioniert immer noch.

1447420cookie-checkSo verwenden Sie statisches Linken mit OpenSSL in C/C++

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy