【问题标题】:Compile with linking against static and dynamic library for OpenSSL通过链接到 OpenSSL 的静态和动态库进行编译
【发布时间】:2015-07-14 03:39:53
【问题描述】:

我想编译我的代码,包括 OpenSSL 库。出于我的目的,有必要静态地链接库。

如果我动态链接脚本以进行编译,看起来像g++ test.cpp -lcrypto -o test

我尝试使用-static 选项进行编译,但如果这样做,我会收到以下错误:

/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x15): Nicht definierter Verweis auf `dlopen'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x2b): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x35): Nicht definierter Verweis auf `dlclose'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_func':
(.text+0x381): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_func':
(.text+0x460): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_var':
(.text+0x4e1): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_var':
(.text+0x5c0): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x630): Nicht definierter Verweis auf `dlopen'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x6a0): Nicht definierter Verweis auf `dlclose'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x6df): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_pathbyaddr':
(.text+0x788): Nicht definierter Verweis auf `dladdr'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_pathbyaddr':
(.text+0x7d9): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_unload':
(.text+0x834): Nicht definierter Verweis auf `dlclose'
collect2: error: ld returned 1 exit status

我该如何解决这个问题?

【问题讨论】:

  • @Sacx 实际上,这些答案非常不令人满意。如果我遵循第一个答案,我不会得到静态链接的二进制文件(与动态链接的二进制文件具有相同的大小)。如果我尝试使用 -static 选项而不是 -static-libc,并将 -ldl 附加到 -lcrypto,我会收到以下警告:/usr/lib/gcc/i686-linux-gnu/4.8/../ ../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o):“在 Funktion `dlfcn_globallookup': (.text+0x15): 警告:在静态链接的应用程序中使用 'dlopen' 需要在运行时共享用于链接的 glibc 版本的库"
  • 为什么要链接-ldl,好像是动态链接库
  • 大约 8 个月前我做了等效的(静态编译 tcpdump)。我花了一天的时间在不同的谷歌点击中敲打,直到我找到了一个有效的,抱歉,不,我不记得我是怎么做到的。综上所述,我投票将其移至 Stack Overflow,因为这是一个编程问题而不是安全问题。
  • 第一条评论的链接相当于 Stack Overflow,@gowenfawr 你能把你的解决方案放在那里吗?
  • 再说一次,我不记得我是怎么做到的了……我给你的建议是反复用头撞墙,尝试不同的方法,直到有效果为止。哎呀,这就是我小时候编译包的工作方式(早在configure 之前)。

标签: ubuntu openssl


【解决方案1】:

我不确定我是否完全理解您想要做什么,但您是否尝试过将 -lcrypto -ldl 替换为 /path/to/libcrypto.a /path/to/libdl.a

【讨论】:

    【解决方案2】:
    g++ test.cpp -lcrypto -o test
    

    试试:

    g++ test.cpp -o test -lcrypto -ldl
    

    图书馆的顺序和位置。


    出于我的目的,有必要静态链接库。

    我的错。我之前错过了这个。为此,它最容易执行:

    g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl
    

    有些人想使用-BstaticL-l。但这不是可移植的(BSD、OS X 和 iOS 不支持它们)。

    存档只是目标文件的集合 (*.o)。因此,您可以在需要目标文件的任何地方使用存档。

    您可以通过以下方式找到库:

    $ lsb_release -r
    Release:    14.04
    $ find /usr/lib -iname libcrypto.a
    /usr/lib/x86_64-linux-gnu/libcrypto.a
    

    另外,test 是一个真正的程序。请务必使用./test 运行它。我通常将其命名为test.exe 以避免冲突。


    如果我将库放在输出文件的前面或后面有什么区别?

    设想这个的方法是转换它(有些手放弃):

    g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl
    

    进入:

    g++ test.o libcrypto.o libssl.o -o test -ldl
    

    输出文件的位置 (-o test) 不相关。我用它来分隔对象(左侧)和库(右侧)。

    【讨论】:

    • 如果我将库放在输出文件的前面或后面有什么区别? @jww
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多