如何使用静态链接使用OpenSSL在C / C ++

问题描述 投票:12回答:4

我在C和C ++写的简单应用程序使用OpenSSL。我编译他们是这样的:

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

和所有的好,但只,你已经安装了OpenSSL的时候。

我想编译它,我可以在操作系统上运行他们那里是没有安装Openssl的(类似Linux OS)。我尝试这样做:

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

和同为C ++:

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

但是有这些错误:

/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

我怎样才能让这个?我记得前一阵子,我这样做,但现在,嗯,忘了该怎么办呢。林在Ubuntu 13.04 64 - 它可以是一个问题?

c++ c static linker static-linking
4个回答
4
投票

我有一个类似的问题,试图以静态编译使用OpenSSL的LIB一个简单的DES程序。我用-lcrypto -lz -ldl -static-libgcc,它为我工作。没有警告或错误。


1
投票

你需要知道,图书馆需要availiable作为静态链接* .a文件。如果不是,那么编译将失败或你最终有一个动态链接的可执行文件。

如果这得到太多的PITA(所有库的依赖需要静态编译,和他们DEPS太等)使用这样的buildroot


0
投票

修补程序:尝试在libdl静态链接了。

如果这不是作品,恕我直言,你的libcrypto.a是坏的编译。


-3
投票

我有同样的问题,因为你。下面是解决这对我的命令:

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

它生成此警告:

/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

但可执行文件仍然有效。

© www.soinside.com 2019 - 2024. All rights reserved.