代码之家  ›  专栏  ›  技术社区  ›  PhoenixBlue

cmake或g++:包括dll库

  •  -1
  • PhoenixBlue  · 技术社区  · 8 年前

    我一直试图在Windows下编写一个C++代码,使用第三方库(我有.dll文件,.LIB文件和.h文件)。我在CMAKE中尝试了很多配置(我使用的是CLion),但是都没有成功!仍然找不到图书馆。所以我决定用平原 g++ 命令。还是什么都没有!!下图显示了文件树 My project file tree

    这是我最新的cmakelists.txt文件:

    cmake_minimum_required(VERSION 3.12)
    project(tf_cert_mngr)
    set(CMAKE_CXX_STANDARD 11)
    
    set(LIBRARIES_DIR ${PROJECT_SOURCE_DIR}/openssl)
    include_directories(src)
    link_directories(${LIBRARIES_DIR}/lib)
    include_directories(${LIBRARIES_DIR}/include)
    #include_directories(include openssl\\include openssl\\lib2)
    
    set(SOURCE_FILES src/Certificate.cpp src/tf-cert-mngr.cpp)
    set(HEADER_FILES include/Certificate.h include/tf-cert-mngr.h)
    find_package(ssl )
    find_package(crypto)
    add_executable(tf_cert_mngr ${SOURCE_FILES} ${HEADER_FILES})
    target_link_libraries(tf_cert_mngr libssl.lib libcrypto.lib)
    

    其结果是:

    [ 33%] Linking CXX executable tf_cert_mngr.exe
    c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lssl
    c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot 
    find -lcrypto
    collect2.exe: error: ld returned 1 exit status
    CMakeFiles\tf_cert_mngr.dir\build.make:101: recipe for target 'tf_cert_mngr.exe' failed
    mingw32-make.exe[3]: *** [tf_cert_mngr.exe] Error 1
    CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/tf_cert_mngr.dir/all' failed
    mingw32-make.exe[2]: *** [CMakeFiles/tf_cert_mngr.dir/all] Error 2
    mingw32-make.exe[1]: *** [CMakeFiles/tf_cert_mngr.dir/rule] Error 2
    CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/tf_cert_mngr.dir/rule' 
    failed
    Makefile:117: recipe for target 'tf_cert_mngr' failed
    mingw32-make.exe: *** [tf_cert_mngr] Error 2
    

    g++ src\tf-cert-mngr.cpp src\Certificate.cpp -Iinclude -I openssl\include  -L openssl\lib -llibcrypto.lib -l libssl.lib -o tester
    

    其结果是:

    c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -llibcrypto.lib
    c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -llibssl.lib
    collect2.exe: error: ld returned 1 exit status
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Community Mohan Dere    6 年前

    请查看的文档FindOpenSSL.cmake. 这是在调用以下命令时内部调用的脚本:

    find_package(OpenSSL) (请注意,对于可移植性,大小写很重要)。

    请注意,脚本创建了两个目标:

    此模块定义以下导入目标:

    OpenSSL::SSL

    OpenSSL ssl库(如果找到)。

    OpenSSL::Crypto

    所以试着改变你的 CMakeLists.txt 要读取的文件:

    find_package(OpenSSL REQUIRED)
    
    ...
    
    target_link_libraries(tf_cert_mngr OpenSSL::SSL OpenSSL::Crypto)
    

    https://cmake.org/cmake/help/latest/module/FindOpenSSL.html