PhoenixBlue
PhoenixBlue

Reputation: 1037

cmake or g++: include dll libraries

I have been trying to write a code in C++ under Windows, that uses a third party library (I have .dll files, .lib files and .h files). I tried so many configurations in CMAKE (I am using CLion), but none of them worked! still cannot find the library. So I decided to use the plain g++ command. Still nothing!! The image below shows file tree My project file tree

This is my latest 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)

its result:

[ 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

and this is the latest g++ command I used:

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

its result:

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

Upvotes: 1

Views: 855

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69884

Have a look at the documentation for FindOpenSSL.cmake. This is the script that is called internally when you call:

find_package(OpenSSL) (note that for portability, case is important).

Notice that the script creates two targets:

This module defines the following IMPORTED targets:

OpenSSL::SSL

    The OpenSSL ssl library, if found.

OpenSSL::Crypto

    The OpenSSL crypto library, if found.

So try changing your CMakeLists.txt file to read:

find_package(OpenSSL REQUIRED)

...

target_link_libraries(tf_cert_mngr OpenSSL::SSL OpenSSL::Crypto)

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

Upvotes: 2

Related Questions