链接错误:找不到-l <path to =“”library =“”> </path>

时间:2015-01-28 06:05:42

标签: c++ openssl ubuntu-14.04 kdevelop libssl

我正在C++开发一个Ubuntu项目,其中kDevelop为IDE。该项目使用openssl boost cryptopp库。在构建项目时,我收到的链接错误为/usr/bin/ld: cannot find -l/home/anz/openssl-1.0.1j/libssl.a

据我所知,-l附带了库的名称(例如-lcryptopp),而不是它的路径。但是为什么-l出现在路径之前呢?指定的库确实存在于此路径中。

我该如何克服这个问题?三江源。

编辑:

来自Cmakelists.txt的部分:

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
find_package( Boost COMPONENTS thread system date_time regex filesystem chrono)
include_directories(${Boost_INCLUDE_DIRS})

find_package (OpenSSL)
include_directories(${OPENSSL_INCLUDE_DIR})
message("LINKED :" ${OPENSSL_LIBRARIES}) # ALL THE LIBRARIES ARE LISTED HERE CORECTLY
ADD_EXECUTABLE(Abc ${Abc_SRC})
TARGET_LINK_LIBRARIES(Abc ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Abc ${OPENSSL_LIBRARIES})

target_link_libraries(Abc ${CMAKE_DL_LIBS})  
install(TARGETS Abc RUNTIME DESTINATION ./)

EDIT2:添加了屏幕截图 Screenshot of CMake Configuration Window in kDevelop

3 个答案:

答案 0 :(得分:0)

确保该库存在,并添加-I&#34;路径到您的libcryptopp&#34;如果需要使用-l

链接库,则需要包含在构建中的目录

例如:g ++ -o targetobjects -I&#34;你的libcryptopp的路径&#34; -lcryptopp

或者你可以给出libcryptopp的完整路径 例如:g ++ -o targetobjects&#34;来自makefile位置的libcryptopp的路径&#34;

答案 1 :(得分:0)

在我的实践中,最好的方法是找到系统中存在的库使用locateld工具:

  1. 找到以在文件系统上查找库本身:

    locate libssl.a
    

    注意它可以显示 DONT 存在的文件的文件系统节点。在使用updatedb之前使用locate

  2. ld -lssl --verbose通过使用系统LD_LIBRARY_PATH变量的链接器查找库:

    :~$ ld -lssl --verbose
    attempt to open /usr/x86_64-linux-gnu/lib64/libssl.so failed
    attempt to open /usr/x86_64-linux-gnu/lib64/libssl.a failed
    attempt to open /usr/local/lib/x86_64-linux-gnu/libssl.so failed
    attempt to open /usr/local/lib/x86_64-linux-gnu/libssl.a failed
    attempt to open /usr/local/lib64/libssl.so failed
    attempt to open /usr/local/lib64/libssl.a failed
    attempt to open /lib/x86_64-linux-gnu/libssl.so failed
    attempt to open /lib/x86_64-linux-gnu/libssl.a failed
    attempt to open /lib64/libssl.so failed
    attempt to open /lib64/libssl.a failed
    attempt to open /usr/lib/x86_64-linux-gnu/libssl.so succeeded
    -lssl (/usr/lib/x86_64-linux-gnu/libssl.so)
    libcrypto.so.1.0.0 needed by /usr/lib/x86_64-linux-gnu/libssl.so
    found libcrypto.so.1.0.0 at /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
    libc.so.6 needed by /usr/lib/x86_64-linux-gnu/libssl.so
    found libc.so.6 at /lib/x86_64-linux-gnu/libc.so.6
    libdl.so.2 needed by /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
    found libdl.so.2 at /lib/x86_64-linux-gnu/libdl.so.2
    ld-linux-x86-64.so.2 needed by /lib/x86_64-linux-gnu/libc.so.6
    found ld-linux-x86-64.so.2 at /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
    ld: warning: cannot find entry symbol _start; not setting start address
    

    当您尝试查找时,您会收到此行为。

  3. 那么,当你试图找到libssl.a时会发生什么,ld工具会执行什么?您可以尝试运行ld -l/home/anz/openssl-1.0.1j/libssl.a --verbose

    来调查它
    ld -l/home/anz/openssl-1.0.1j/libssl.a --verbose`
    attempt to open /usr/x86_64-linux-gnu/lib64/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/x86_64-linux-gnu/lib64/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/local/lib/x86_64-linux-gnu/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/local/lib/x86_64-linux-gnu/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/local/lib64/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/local/lib64/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /lib/x86_64-linux-gnu/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /lib/x86_64-linux-gnu/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /lib64/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /lib64/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/lib/x86_64-linux-gnu/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/lib/x86_64-linux-gnu/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/lib64/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/lib64/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/x86_64-linux-gnu/lib/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/x86_64-linux-gnu/lib/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/local/lib/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/local/lib/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /lib/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /lib/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    attempt to open /usr/lib/lib/home/anz/openssl-1.0.1j/libssl.a.so failed
    attempt to open /usr/lib/lib/home/anz/openssl-1.0.1j/libssl.a.a failed
    ld: cannot find -l/home/anz/openssl-1.0.1j/libssl.a
    

    如果您知道-l-L ld-options意味着您会知道它的确切作用:-l之后的所有内容都是您搜索并希望链接的库名称; -L设置路径(在链接过程中附加到链接器自己的列表)搜索库的位置。

    正确编辑CMakeLists.txt以解决此问题。

答案 2 :(得分:0)

  

在构建项目时,我收到链接错误/ usr / bin / ld:找不到-l / home / anz / openssl-1.0.1j / libssl.a

归档(libssl.a)是目标文件的集合。因此,您在此实例中使用它就像任何其他对象文件一样。不需要-l

gcc foo.o bar.o -o baz.exe

在您的情况下,例如:

gcc foo.o bar.o /home/anz/openssl-1.0.1j/libssl.a -o baz.exe

我经常使用相同的技术来确保我链接到静态版本的库,因为它可以在Mac OS X,Linux和BSD之间移植。 -Bstatic-L-l 可移植,因为OS X始终链接到动态库(如果可用)。

KDevelop中,您只需要找到指定其他目标文件的位置(因为存档只是目标文件的集合)。

相关问题