title: How to link my project library to my pybind module?
body: <p>I'm am new to pybind11 and I'm struggling with this :</p>
  <p>I have a cpp program called my_exec.</p>
  <p>I want to create a python-binding for it. (with pybind11)</p>
  <p>In my exec I have 1 simple function :</p>
  <pre><code>int add(int i, int j)
  {
      return i+j;
  }
  </code></pre>
  <p>And this is my pybind .cpp file :</p>
  <pre><code>#include &lt;pybind11/pybind11.h&gt;
  #include &quot;lib.h&quot; // my add function is defined here and implement in lib.cpp
  
  PYBIND11_MODULE(cpp, m)
  {
      m.doc() = &quot;first python binding.&quot;;
      m.def(&quot;add&quot;, &amp;add, &quot;A function that adds 2 numbers&quot;);
      m.def(&quot;mul&quot;, &amp;mul, &quot;A function that multiply 2 numbers&quot;);
  }
  </code></pre>
  <p>So here is my problem :
  I can't build my pybind module with CMake because I can't (I don't know how) link my program my_exec (containing the add function implementation) to it.</p>
  <p>This is my CMakeLists.txt file :</p>
  <pre><code># only for cmake --version &gt;= 3.5.1
  cmake_minimum_required(VERSION 3.5.1)
  
  if (CMAKE_BUILD_TYPE MATCHES Debug)
      message(&quot;Cmake in debug mode.&quot;)
  else ()
      message(&quot;Cmake in release mode.&quot;)
  endif (CMAKE_BUILD_TYPE MATCHES Debug)
  
  # project name
  project(my_exec)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  
  #lsp file
  set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
  
  # I../includes
  include_directories(
  src 
  /media/romain/Donnees/Programmation/C++/frameworks
  /media/romain/Donnees/Programmation/C++/frameworks/files
  /media/romain/Donnees/Programmation/C++/libs/json
  /media/romain/Donnees/Programmation/C++/libs/boost-install/include
  )
  
  #Link directories
  link_directories(build/libs)
  
  # puts all .cpp files inside src to the SOURCES variable
  file(GLOB SOURCES src/*.cpp 
  /media/romain/Donnees/Programmation/C++/frameworks/debug.cpp
  /media/romain/Donnees/Programmation/C++/frameworks/str.cpp
  )
  
  if (CMAKE_BUILD_TYPE MATCHES Debug)
      add_compile_definitions(mydebug)
      add_compile_options(-Og)
  else()
      add_compile_options(-O3)
  endif(CMAKE_BUILD_TYPE MATCHES Debug)
  
  # compiles the files defined by SOURCES to generate the executable defined by EXEC
  add_executable(${PROJECT_NAME} ${SOURCES})
  
  add_subdirectory(pybind) #where is the pybind CMakeLists file
  
  #make the executable linkable by other libs (runtime ones - here for my module pybind)
  set_target_properties(${PROJECT_NAME} PROPERTIES ENABLE_EXPORTS on)
  
  # linkers
  target_link_libraries(${PROJECT_NAME} 
  pthread
  stdc++fs
  )
  </code></pre>
  <p>And this is my pybind CMakeLists.txt (locate in the subdir pybind) :</p>
  <pre><code>cmake_minimum_required(VERSION 3.5)
  project(pybind)
  include_directories(/media/romain/Donnees/Programmation/C++/libs/pybind11/include)
  add_subdirectory(/media/romain/Donnees/Programmation/C++/libs/pybind11
      ./pybind11_build)
  
  
  pybind11_add_module(cpp
      ../src/pybind/cpp.cpp)
  
  target_link_libraries(${PROJECT_NAME} PRIVATE 
      my_exec)
  </code></pre>
  <p>I can't compile because CMake give a error when trying to link my_exec :</p>
  <pre><code>Cannot specify link libraries for target &quot;pybind&quot; which is not built by
    this project.
  </code></pre>
  <p>So, how to link an external lib/exec to a pybind Cmake project ?</p>
  <p>Thank you in advance ! :)</p>
  <p>EDIT :</p>
  <hr />
  <p>Thanks to @David Fong anwser, I managed to compiled.
  But unfortunatly, I think python does not support module linked to an executable.
  It has to be a static or shared library.</p>
  <p>So I had to compile my program my_exec as a library like that :</p>
  <pre><code>add_library(my_exec SHARED ${SOURCES})
  </code></pre>
  <p>I hope it will help some of you if you have the same issue. :)</p>