title: Unable to link an executable to shared library
body: <p>I have a <code>cmake</code> project where I am trying to link the executable to the shared library. But it is not getting linked. After searching enough and not finding any useful solutions, I'm posting my question here, please let me know if there are any obvious mistakes as I am not familiar with <code>cmake</code></p>
  
  <p>Here is my <code>CMakeLists.txt</code> </p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8)
  # Set Project Name
  set(PROJECT_NAME myproj)
  project(${PROJECT_NAME})
  # Tell Cmake to invoke gcc with specific flags. Use c++11 standard.
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  
  # Include all headers 
  include_directories(${PROJECT_SOURCE_DIR}/../public/include)
  
  # Driver Program
  file(GLOB_RECURSE SOURCE_FILES
    "${PROJECT_SOURCE_DIR}/src/*.cpp"
  )
  # Dont know what to do about this
  #set( CMAKE_SKIP_BUILD_RPATH true)
  
  link_directories(${CMAKE_SOURCE_DIR}/../relative/path/to/sharedlib) #contains libshared.so
  
  # Generate Executable, trying to generate shared library to see if it generates # correct dependencies
  # add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
  add_executable(${PROJECT_NAME} ${SOURCE_FILES})
  
  #Link the libraries
  
  target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/../relative/path/to/sharedlib/libshared.so)
  </code></pre>
  
  <p>Here is the steps I have tried and some observations. </p>
  
  <ol>
  <li><p>If I generate a shared library instead of an executable, it is able to find the dependent shared library(i.e libshared.so)</p>
  
  <p><code>ldd myproj.so</code></p>
  
  <p><code>libshared.so =&gt; full/path/to/sharedlib/libshared.so (0x00007fa26c263000)</code></p></li>
  </ol>
  
  <p>I did not fully understand what it meant, but it is able to find the shared library is what I guessed. </p>
  
  <ol start="2">
  <li><p>If I try to generate an executable instead of shared library, I see undefined reference errors to the functions within the library.</p></li>
  <li><p>I tried just <code>target_link_libraries(${PROJECT_NAME} shared)</code> as well and it gave same error. </p></li>
  </ol>
  
  <p>Any clues as to how can I get the shared library linked to my executable? Thanks</p>