-1

trying to run this simple program

#include <openssl/ssl.h>
int main() {
    printf("version: %s\n", OpenSSL_version(0));
    return 0;
}

result In this error

Scanning dependencies of target main
[ 50%] Building C object CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
/usr/bin/ld: CMakeFiles/main.dir/main.c.o: in function `main':
/home/khaled/CLionProjects/untitled2/main.c:4: undefined reference to `OpenSSL_version'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/main.dir/build.make:84: main] Error 1
make[2]: *** [CMakeFiles/Makefile2:105: CMakeFiles/main.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:112: CMakeFiles/main.dir/rule] Error 2
make: *** [Makefile:131: main] Error 2

I am runinning ubuntu 20.04

cmakelist.txt

cmake_minimum_required(VERSION 3.16)
project(untitled2 C)

set(CMAKE_C_STANDARD 99)
add_executable(main main.c)

I guess there is no errors in linking since openssl is already in usr/include which is in turn in the preprocesser search tree. I am new with cmake so any expalnation would be really appreciated.

KMG
  • 1,433
  • 1
  • 8
  • 19
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – fdk1342 Jul 25 '20 at 16:45
  • `ld returned 1 exit status` and the surrounding lines are a linking error. You need to link against the libraries you intend to use. – fdk1342 Jul 25 '20 at 16:47
  • @fdk1342 so how to do this in cmake if i just used command line everything is fine the problem comes that I cant deal with cmake actually? – KMG Jul 25 '20 at 17:03
  • In general you used the command `target_link_libraries()` to tell `CMake` which libraries a target needs during linking. I wouldn't know the specifics for your particular environment. – fdk1342 Jul 25 '20 at 17:19
  • @fdk123 already tried it using ```target_link_libraries(main /usr/include/openssl/ssl.h)``` same issue still – KMG Jul 25 '20 at 17:22

1 Answers1

2
target_link_libraries(main /usr/include/openssl/ssl.h)

Is incorrect because you are passing is a header, not a library.

See documentation:

https://cmake.org/cmake/help/latest/command/target_link_libraries.html

target_link_libraries(<target> ... <item>... ...)

Where <item> in your case shall be either a library name or a full path to the library.

You are probably missing s/t like:

find_package(OpenSSL REQUIRED)
target_link_libraries(main PUBLIC OpenSSL::SSL OpenSSL::Crypto)

msune
  • 231
  • 1
  • 3
  • Thank's it worked but why is it needed to use all uppercase i mean OpenSSL::SSH and not OpenSSL::ssh.since the header file is actually ssl.h not SSL.h And what is the benefit of using find_package when i can just use target_link_libraries( ) – KMG Jul 25 '20 at 20:29
  • First, I am not an expert on `cmake`, I am more versed in autotools. It's not strictly needed, as you point out, but it seems to be something desirable (if the library provides it) for a number of reasons: https://cmake.org/cmake/help/v3.8/command/find_package.html; you can check for specific versions of the pacakge, make it mandatory as done above(early fail, instead at linking stage), and in config mode, import the necessary required cflags/cxxflags dependencies etc. Perhaps someone more familiar with cmake can better explain this. – msune Jul 26 '20 at 18:53