title: CMake Undefined reference when creating executables
body: <p>I am new to CMake and I am trying to get my project compiling. The project creates a few static libraries and a few executables.</p>
  
  <p>Below is the example of the file structure that I have. </p>
  
  <p>PROJECT</p>
  
  <ul>
  <li><p>SRC  </p>
  
  <ol>
  <li>subProject_1<br>
  .cpp (all source files) and CMakeLists.txt 1 for this folder (creating a static library)</li>
  <li>subproject_2<br>
  .cpp (all source files) and CMakeLists.txt 2 for this folder (creating a static library)  </li>
  <li>subproject_3<br>
  .cpp (all source files) and CMakeLists.txt 3 for this folder (creating the executable)  </li>
  </ol></li>
  <li><p>Include  </p>
  
  <ol>
  <li>subProject_1<br>
  .h (all the header files)</li>
  <li>subProject_2<br>
  .h (all the header files)</li>
  <li>subProject_3<br>
  .h (all the header files)</li>
  </ol></li>
  <li><p>build/linux  </p>
  
  <ol>
  <li>CMakeLists.txt (Main CMakelist file)</li>
  </ol></li>
  </ul>
  
  <p>Main CMakeLists.txt</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.6)
  SET(CMAKE_CXX_COMPILER "g++")
  Project(ort)
  SET (CMAKE_CXX_FLAGS " -g -Wall -pThread")
  
  #set the source and header directories location
  set(ORT_HEADER_DIRECTORY "../../include") #include folder structure explained above
  set(ORT_SOURCE_DIRECTORY "../../src")
  set(ORT_BINARY_DIRECTORY "../../lib")  # lib folder to contain all the libraries
  
  set (CMAKE_CURRENT_BINARY_DIR ".")
  
  #Include the library packages
  include_directories("/usr/include/wx-2.8")
  include_directories("/usr/local/cuda/include") and so on
  
  
  #set the names of all the projects (for creating the libraries)
  SET(PROJECT_NAMES "log" "data" "cc")
  
  foreach(PROJECT_NAME ${PROJECT_NAMES})
       # Create the cmake related files in the out folder so that the libraries can be
       # copied to the lib folder
       add_subdirectory( "{ORT_SOURCE_DIRECTORY}/${PROJECT_NAME}" "${CMAKE_CURRENT_BINARY_DIR}/out/${PROJECT_NAME}"
  
  endforeach(PROJECT_NAME ${PROJECT_NAMES})
  
  #set the names of all the projects (for creating the libraries)
  SET(EXECUATALE_PROJECTS "metadata" )
  
  foreach(EXECUATALE_PROJECT ${EXECUATALE_PROJECTS})
       # Create the cmake related files in the out folder so that the libraries can be
       # copied to the lib folder
       add_subdirectory( "{ORT_SOURCE_DIRECTORY}/${EXECUATALE_PROJECT}" "${CMAKE_CURRENT_BINARY_DIR}/out/${EXECUATALE_PROJECT}"
  
  endforeach(EXECUATALE_PROJECT ${EXECUATALE_PROJECTS})
  </code></pre>
  
  <p>CMakeLists.txt file for log directory (the same logic I have used for cc and data projects)</p>
  
  <pre><code>include_directories(${ORT_HEADER_DIRECTORY})
  SET(LOG_SOURCE a.cpp b.cpp c.cpp)
  ADD_LIBRARY(log_d ${LOG_SOURCE})
  target_link)libraries(log_d cc_d data_d)
  </code></pre>
  
  <p>metadata CMakeLists.txt file  (creating the executable project)</p>
  
  <pre><code>FIND_PACKAGE(wxWidgets)
  IF(wxWidgets_FOUND)
          INCLUDE(${wxWidgets_USE_FILE})
  ENDIF(wxWidgets_FOUND)
  
  Include_Directories(${wxWidgets_INCLUDE_DIRS})
  include_directories(${ORT_HEADER_DIRECTORY})
  include_directories("/usr/ort/lib/unixODBC/include")
  
  SET(META_SOURCE meta.cpp data.cpp)
  
  ADD_EXECUTABLE(meta_d ${META_SOURCE })
  TARGET_LINK_LIBRARIES(meta_d log_d data_d)
  </code></pre>
  
  <p>When I just make the project, without the creation of the executables, static files are being generated. But, when I make the whole project (i.e. with the inclusion of the subProject_3 directory) I get undefined reference to <code>a::String</code> which is a function in a.cpp.</p>
  
  <p>Note: All the 3 projects are dependent on each other. For example, in a.cpp, I have <code>#include "b.h"</code> and in b.cpp I have <code>#include "a2.h"</code>.</p>
  
  <p>So, I have few questions:</p>
  
  <p>a) How do I resolve the undefined reference issue? Once I have generated the libraries for project 1 and 2, how do I link these to the executable? </p>
  
  <p>b) Should I provide or add any dependencies when I am creating the static libraries? Is this the right way of creating the static libraries (as the projects are inter dependent)?
  i.e. <code>target_link_libraries(project1 project2 ...)</code> in project 1 and <code>target_link_libraries(project2 project1 ...)</code> in project 2.</p>
  
  <p>c) Every project needs to be compiled with its own compilation setting. Can you please let me know how can I specify the same for every individual project?</p>
  
  <p>ERROR DETAILS:</p>
  
  <p>Liking CXX executable metadata_d
  ../log/liblog_d.a: file not recognized: File truncated
  collect2: ld returned 1 exit status</p>
  
  <p>I also get the undefined reference error
  /home...../metadata/data.cpp 172: undefined reference to xmlSerahNs
  and so on.
  collect2: ld returned 1 exit status</p>
  
  <p>Thanks for the help. </p>