0

I am using clion for my c++ project. The command I need to run is: g++ server.cpp -L/usr/lib -lssl -lcrypto -o server

I have tried:

SET(GCC_COVERAGE_LINK_FLAGS "-L/usr/lib -lssl -lcrypto") add_definitions(${GCC_COVERAGE_COMPILE_FLAGS}) as mentioned here

Also tried:

include_directories(/usr/inc) link_directories(/usr/lib) SET(GCC_COVERAGE_LINK_FLAGS "-lssl -lcrypto") mentioned here.

Nothing seems to be working. I am new to cmake. Can someone explain a solution and some good tutorial/resource to follow.

Edit: Error that I get is undefined reference to `PKCS5_PBKDF2_HMAC_SHA1' which is a funciton in openssl/evp.h

Following is run by clion: /"path to clion"/clion-2018.3.1/bin/cmake/linux/bin/cmake -S"Path to project" -B"Path to project/cmake-build-debug" --check-build-system CMakeFiles/Makefile.cmake 0 /usr/bin/make -f CMakeFiles/server.o.dir/build.make CMakeFiles/server.o.dir/server.cpp.o

user415612
  • 457
  • 1
  • 3
  • 13
  • First of all, can you show us the error messages? Secondly, CMake will search all standard locations by default, including `/usr/lib`, so you don't have to add that directory to the search paths. Thirdly, are you sure all of those libraries are properly installed? – thomas_f Mar 21 '19 at 14:59
  • The g++ command works correctly so the libraries should be correctly installed. I have edited with the error. – user415612 Mar 21 '19 at 15:04
  • where are your target_link_libraries() and target_include_directories() calls? What arguments are you providing to cmake? – Tzalumen Mar 21 '19 at 15:06
  • @Tzalumen I added them in the CMakeLists.txt but that didn't work. – user415612 Mar 21 '19 at 15:08
  • Try `link_libraries(-lssl -lcrypto)`. Also, you might want to post your `CMakeLists.txt` file (at least the relevant parts): https://stackoverflow.com/help/mcve – thomas_f Mar 21 '19 at 15:11
  • @thomas_f That worked!!. Thank you. If you'd like to add it as an answer, I would gladly mark as accepted. Any explanation as to why it works while linked answers don't would be appreciated too. – user415612 Mar 21 '19 at 15:15
  • I'll leave that to @Tzalumen since he/she was the first to suggest it.Also, `target_link_libraries()` should be used over `link_libraries()`. Glad it worked out! – thomas_f Mar 21 '19 at 15:17
  • Okay, I'll put up an answer. – Tzalumen Mar 21 '19 at 15:18
  • [target_link_directories](https://cmake.org/cmake/help/latest/command/target_link_directories.html#command:target_link_directories) – Jesper Juhl Mar 21 '19 at 16:51

1 Answers1

3

As discussed in comments:

In your target CMakeLists.txt, the one with your

add_executable (${PROJECT_NAME} ...)

add a

target_link_libraries(${PROJECT_NAME} PUBLIC ssl crypto ...)

call to attach those library dependencies to your target

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Tzalumen
  • 652
  • 3
  • 16
  • From OPs description I got the impression that [target_link_directories](https://cmake.org/cmake/help/latest/command/target_link_directories.html#command:target_link_directories) was what they were after.. – Jesper Juhl Mar 21 '19 at 16:54
  • @JesperJuhl If the `target_link_libraries()` call succeeds, you don't need `target_link_directories()`, that just adds directories to cmake for `target_link_libraries()` to check. – Tzalumen Mar 21 '19 at 20:51