title: cmake find openssl libraries in non-standard paths
body: <p>I am having trouble telling cmake to find openssl on my mac.  Let me say, I am an amateur at using cmake, as my acquaintance with it, is very recent and occasional.  That said, I can do simple things in cmake such as create small projects build them and run them.  Some commands (find_library, find_package, ...) always perplex me.</p>
  
  <p>I downloaded and installed openssl (./configure, make and make install) and it has put the files in /opt/openssl</p>
  
  <p>I was googling around to see help for a simple inclusion openssl in my program.</p>
  
  <p>One search result points to a command FindOpenSSL here (<a href="https://cmake.org/cmake/help/v3.6/module/FindOpenSSL.html" rel="nofollow noreferrer">https://cmake.org/cmake/help/v3.6/module/FindOpenSSL.html</a>). When i used FindOpenSSL, cmake emitted error saying FindOpenSSL is not a command.</p>
  
  <p>I also tried find_library and find_package commands.
  But if I check the variable OPENSSL_FOUND, it is still undefined.</p>
  
  <p>It looks like I have to hardcode (or pass through -D options to cmake) the variables for openssl.  What is the best practice here?  The reference manual of cmake seems to be like piece of puzzle, as I don't understand the true difference between find_library and find_package, or when to use in preference of the other.</p>
  
  <p>Am I the only one facing this, or more souls around here that are confused and struggling?</p>
  
  <p>Thanks to anyone pointing me the right direction and helping me out of this confusion.</p>
  
  <p>My current CMakeLists.txt looks like below:</p>
  
  <pre><code>ravindranaths-MacBook-Pro:crypt_handlers ravindranath$ cat CMakeLists.txt 
  cmake_minimum_required (VERSION 3.5.1)
  
  # search for CPP_HOME. If found use this as the install_root dir.
  # else, use /usr/local
  message (STATUS "Searching for environment var CPP_HOME ...")
  if (DEFINED ENV{CPP_HOME})
  message (STATUS "Found CPP_HOME: " $ENV{CPP_HOME})
  set (CPP_HOME $ENV{CPP_HOME})
  else()
  message (STATUS "Could not find.  Treating /usr/local as CPP_HOME...")
  set (CPP_HOME /usr/local)
  endif()
  set(Boost_USE_STATIC_LIBS ON)
  ###########################################################
  set(CMAKE_INCLUDE_PATH /usr/local/ssl/include/openssl)
  
  set(OPENSSL_USE_STATIC_LIBS ON)
  ############################################################
  find_package(Boost 1.45.0 COMPONENTS  system ) 
  find_package (Threads)
  
  set (CMAKE_CXX_STANDARD 11)
  include_directories(../../include)
  include_directories(../../include/ext)
  include_directories(../../include/ext/spdlog)
  include_directories(${CPP_HOME}/externals/plog/include)
  
  add_executable(crypt app.cpp)
  
  link_directories(../../build/src)
  target_link_libraries (crypt ${CMAKE_THREAD_LIBS_INIT})
  
  find_library(OpenSSL_LIB  libcrypto.a libssl.a)
  #find_package(OpenSSL)
  
  if (OPENSSL_FOUND)
  message (STATUS "OPENSSL found")
  message (STATUS "OpenSSL INclude directories:" OPENSSL_INCLUDE_DIR)
  else() 
  message (FATAL_ERROR "OpenSSL Not found.")
  endif()
  
  include_directories(${OPENSSL_INCLUDE_DIR})
  target_link_libraries(crypt ${CMAKE_DL_LIBS})
  TARGET_LINK_LIBRARIES(crypt ${OPENSSL_LIBRARIES} ${LIBYAML_LIBRARIES} pthread -ldl)
  #target_link_libraries(crypt /usr/local/ssl/lib/libcrypto.a /usr/local/lib/libssl.a)
  target_link_libraries(crypt /usr/local/lib/libcrypto.a /usr/local/lib/libssl.a)
  
  if(Boost_FOUND)
  include_directories(${include_directories} ${Boost_INCLUDE_DIRS}) 
  target_link_libraries(crypt nettu ${Boost_LIBRARIES})
  endif()
  
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/dist/bin)
  </code></pre>
  
  <p><strong>UPDATE</strong> 
  now, I just use find_package(OpenSSL), instead of find_libraries(...).  And, I am running cmake as below:</p>
  
  <pre><code>cmake -DCMAKE_BUILD_TYPE=Debug -DOPENSSL_ROOT_DIR=/opt/openssl/ -DOPENSSL_LIBRARIES=/opt/openssl/lib/ ..
  </code></pre>
  
  <p>cmake succeeds by writing the makefiles. However, I get a linker error when running make.  The error points to missing libssl.a like below:</p>
  
  <pre><code>make[2]: *** No rule to make target `/usr/local/lib/libssl.a', needed by `examples/crypt_handlers/crypt'.  Stop.
  </code></pre>
  
  <p>What I don't understand is that the make is still looking for libssl.a in "/usr/local/lib/.." instead of /opt/openssl/lib.  </p>
  
  <p>How to fix this?</p>