我一直试图在Windows下用C ++编写代码,该代码使用第三方库(我有.dll文件,.lib文件和.h文件)。我在CMAKE中尝试了许多配置(我正在使用CLion),但是它们都不起作用!仍然找不到图书馆。因此,我决定使用普通的g++
命令。依然没有!!下图显示了文件树
这是我最新的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 ++命令:
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
答案 0 :(得分:0)
查看FindOpenSSL.cmake的文档。这是调用时在内部调用的脚本:
find_package(OpenSSL)
(请注意,对于可移植性,大小写很重要)。
请注意,脚本创建了两个目标:
此模块定义了以下导入目标:
OpenSSL::SSL
OpenSSL ssl库(如果找到)。
OpenSSL::Crypto
OpenSSL加密库(如果找到)。
因此,请尝试将您的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