LIBSSH2和dso_dlfcn.c:...对'dlopen'的未定义引用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LIBSSH2和dso_dlfcn.c:...对'dlopen'的未定义引用相关的知识,希望对你有一定的参考价值。

我正在尝试使用静态git2golibgit2openssl编译libssh2。我的最终目标是能够编译可以部署的go二进制文件,而无需安装这些库。我在SO上找到了类似的question,我用它来创建以下构建库的脚本

OPENSSL:

#!/bin/sh

set -ex
INSTALL_PATH="$PWD/install"
SUBMODULE_PATH="$PWD/submodules/openssl"

cd $SUBMODULE_PATH &&
mkdir -p $INSTALL_PATH/lib &&
mkdir -p build &&

# Switch to a stable version
git checkout OpenSSL_1_0_2-stable &&
./config threads no-shared --prefix=$INSTALL_PATH -fPIC -DOPENSSL_PIC &&
make depend &&
make &&
make install

LIBSSH2:

#!/bin/sh

set -ex

INSTALL_PATH="$PWD/install"
SUBMODULE_PATH="$PWD/submodules/libssh2"

# without this, the system's openssl shared object gets linked in
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$INSTALL_PATH/lib/pkgconfig:$INSTALL_PATH/lib64/pkgconfig"

cd $SUBMODULE_PATH &&
mkdir -p $INSTALL_PATH/lib &&
mkdir build
cd build &&
cmake -DTHREADSAFE=ON 
      -DBUILD_CLAR=OFF 
      -DBUILD_SHARED_LIBS=OFF 
      -DCMAKE_C_FLAGS=-fPIC 
      -DCMAKE_BUILD_TYPE="RelWithDebInfo" 
      -DCMAKE_INSTALL_PREFIX=$INSTALL_PATH 
      .. &&

cmake --build .
make install

我将以下补丁应用于git2go分支next中的脚本,该分支构建libgit2以确保PKG_CONFIG_PATH指向静态openssllibssh2

要中止:

--- build-libgit2-static.sh.orig    2016-04-14 22:49:53.000000000 -0700
+++ build-libgit2-static.sh 2016-04-14 22:52:04.000000000 -0700
@@ -2,10 +2,12 @@

 set -ex

-VENDORED_PATH=vendor/libgit2
+INSTALL_PATH="$PWD/install"
+VENDORED_PATH="$PWD/submodules/git2go/vendor/libgit2"
+export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$INSTALL_PATH/lib/pkgconfig:$INSTALL_PATH/lib64/pkgconfig"

 cd $VENDORED_PATH &&
-mkdir -p install/lib &&
+mkdir -p $INSTALL_PATH/lib &&
 mkdir -p build &&
 cd build &&
 cmake -DTHREADSAFE=ON 
@@ -13,7 +15,8 @@
       -DBUILD_SHARED_LIBS=OFF 
       -DCMAKE_C_FLAGS=-fPIC 
       -DCMAKE_BUILD_TYPE="RelWithDebInfo" 
-      -DCMAKE_INSTALL_PREFIX=../install 
+      -DCMAKE_INSTALL_PREFIX=$INSTALL_PATH 
       .. &&

 cmake --build .
+make install

此设置最终创建一个本地install目录,其中安装了所有库。我使用上面的脚本编译openssl。然后在编译libssh2时得到以下输出:

...
Found OpenSSL: (path to project)/install/lib/libssl.a;(path to project)/install/lib/libcrypto.a (found version "1.0.2h-dev")
...
Linking C static library libssh2.a
gmake[3]: Leaving directory `(path to project)/submodules/libssh2/build'
[ 46%] Built target libssh2
gmake[3]: Entering directory `(path to project)/submodules/libssh2/build'
Scanning dependencies of target example-direct_tcpip
gmake[3]: Leaving directory `(path to project)/submodules/libssh2/build'
gmake[3]: Entering directory `(path to project)/submodules/libssh2/build'
[ 48%] Building C object example/CMakeFiles/example-direct_tcpip.dir/direct_tcpip.c.o
Linking C executable example-direct_tcpip
(path to project)/install/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
dso_dlfcn.c:(.text+0x11): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x24): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x2f): undefined reference to `dlclose'
(path to project)/install/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func':
dso_dlfcn.c:(.text+0x354): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x412): undefined reference to `dlerror'
(path to project)/install/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var':
dso_dlfcn.c:(.text+0x484): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x542): undefined reference to `dlerror'
(path to project)/install/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load':
dso_dlfcn.c:(.text+0x5a9): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x60d): undefined reference to `dlclose'
dso_dlfcn.c:(.text+0x645): undefined reference to `dlerror'
(path to project)/install/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr':
dso_dlfcn.c:(.text+0x6d1): undefined reference to `dladdr'
dso_dlfcn.c:(.text+0x731): undefined reference to `dlerror'
(path to project)/install/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload':
dso_dlfcn.c:(.text+0x792): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
gmake[3]: Leaving directory `(path to project)/submodules/libssh2/build'
gmake[2]: Leaving directory `(path to project)/submodules/libssh2/build'
gmake[1]: Leaving directory `(path to project)(path to project)/submodules/libssh2/build'
gmake[3]: *** [example/example-direct_tcpip] Error 1
gmake[2]: *** [example/CMakeFiles/example-direct_tcpip.dir/all] Error 2
gmake[1]: *** [all] Error 2
make: *** [build-libssh2] Error 2

你可以看到正在使用静态openssl。显然libdl没有与libcrypto联系起来。编译openssl时的输出有EX_LIBS=-ldl。这应该包括图书馆吗?我尝试在LDLIBS=-ldl安装脚本中使用openssl,但仍然是同样的错误。我已经尝试了搜索词libcrypto undefined reference to 'dlopen'的几乎所有SO答案都无济于事。任何帮助将非常感激

答案

由于我不熟悉pig-config,我改用Autoconf

#!/bin/sh

set -ex

INSTALL_PATH="$PWD/install"
SUBMODULE_PATH="$PWD/submodules/libssh2"

mkdir -p $INSTALL_PATH/lib &&
cd $SUBMODULE_PATH &&
./buildconf
./configure --prefix=$INSTALL_PATH --libdir=$INSTALL_PATH/lib64 --with-openssl CFLAGS="-fPIC" LDFLAGS="-m64 -L$INSTALL_PATH/lib -L$INSTALL_PATH/lib64" LIBS=-ldl
make
make install

这成功编译。

另一答案

我发现将target_link_libraries(libssh2 ${CMAKE_DL_LIBS})添加到src/CMakeLists.txt(正如here所解释的)解决了问题中提到的问题。请注意,我使用的是libssh2-1.8.0和最新的OpenSSL(5de683d),但我认为这与OpenSSL_1_0_2-stable的故事相同。

所以有效的LIBSSH2构建代码是:

# inside libssh2 root
printf '
target_link_libraries(libssh2 ${CMAKE_DL_LIBS})' >> src/CMakeLists.txt
mkdir build && cd build
cmake -DTHREADSAFE=ON 
      -DBUILD_CLAR=OFF 
      -DBUILD_SHARED_LIBS=OFF 
      -DCMAKE_C_FLAGS=-fPIC 
      -DCMAKE_BUILD_TYPE="RelWithDebInfo" 
      -DCMAKE_INSTALL_PREFIX=$INSTALL_PATH 
      .. &&
cmake --build .
make install

注意:我还必须添加target_link_libraries(libssh2 pthread),因为我收到了对最新版本的pthread的未定义引用。

以上是关于LIBSSH2和dso_dlfcn.c:...对'dlopen'的未定义引用的主要内容,如果未能解决你的问题,请参考以下文章

使用 Libssh2 C++ 的 SSH 公钥认证

用于端口转发的 libssh2 示例代码

RHSA-2019:1880-低危: curl 安全和BUG修复更新 及 RHSA-2019:1884-中危: libssh2

ubuntu系统安装搜狗拼音输入法

php_curl.dll libssh2.dll 始终无法加载的原因 及解决办法

Centos6 下PHP的SSH2扩展安装