Search code examples
copensslcygwinclion

Unable to link openssl libraries to CLion C program on windows with cygwin


I'm new to C programming, and I wanted to start a project for learning purposes. I am trying to open an ssl socket in C with clion on windows using cygwin. For the code I've been using this tutorial: https://aticleworld.com/ssl-server-client-using-openssl-in-c/

I've installed cygwin with those libs: make, gdb, gcc-g++, libssl-dev

I don't see any compliation errors in the editor, but I can't run thr project. When I try to run it I get many errors of undefined during the linking phase:

undefined reference to `OPENSSL_init_crypto'
undefined reference to `OPENSSL_init_ssl'
undefined reference to `SSL_CTX_new'

and many more like it.

So I updated m cmake file to look like this:

cmake_minimum_required(VERSION 3.21)
project(ssl_example C)

set(CMAKE_C_STANDARD 99)
include_directories(/usr/include/openssl/)
link_libraries(openssl)
add_executable(ssl_example main.c)

and now I get this error-

FAILED: ssl_example.exe 
: && /usr/bin/gcc.exe -g -Wl,--enable-auto-import CMakeFiles/ssl_example.dir/main.c.o -o ssl_example.exe -Wl,--out-implib,libssl_example.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lopenssl  -lcrypto && :
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lopenssl
collect2: error: ld returned 1 exit status

EDIT:

this is how my cmake file looks like:
cmake_minimum_required(VERSION 3.21)
project(ssl_example C)

set(CMAKE_C_STANDARD 99)
include_directories(/usr/include/openssl/)
link_libraries(ssl)
link_libraries(crypto)
link_libraries(openssl)
add_executable(ssl_example main.c)

I still get the same error cannot find -lopenssl


Solution

  • OPENSSL_init_crypto belongs to libcrypto

    $ cd /usr/lib
    
    $ grep OPENSSL_init_crypto  *.dll.a
    grep: libcrypto.dll.a: binary file matches
    
    $ nm libcrypto.dll.a |grep " T " |grep OPENSSL_init
    0000000000000000 T OPENSSL_init_crypto
    0000000000000000 T OPENSSL_init
    

    while the other two to libssl

    $ grep OPENSSL_init_ssl  *.dll.a
    grep: libssl.dll.a: binary file matches
    
    $ nm libssl.dll.a |grep " T " |grep OPENSSL_init_ssl
    0000000000000000 T OPENSSL_init_ssl
    
    $ grep SSL_CTX_new *.dll.a
    grep: libQtNetwork.dll.a: binary file matches
    grep: libssl.dll.a: binary file matches
    
    nm libssl.dll.a |grep " T " |grep SSL_CTX_new
    0000000000000000 T SSL_CTX_new
    

    so you need to link versus both libssl and libcrypto