title: Undefined Python references in C++ using CMake
body: <p>I am trying to compile a c++ project referencing Python using CMake.  I am using Cygwin and I have Python2.7 source files in Cygwin.</p>
  
  <p>For example:</p>
  
  <pre><code>PyObject *l = PyList_New(0);
  </code></pre>
  
  <p>Online help suggested I add the -lpython2.7 linker flag.  Am I not adding this correctly in CMake?  Otherwise why can I still not use the Python library and how might I fix this?</p>
  
  <p>The compile line:</p>
  
  <pre><code>C:\cygwin64\bin\cmake.exe --build "C:\Users\...\.clion10\system\cmake\generated\3e6845d6\3e6845d6\Release" --target projectname -- -j 4
  </code></pre>
  
  <p>The CMakeList.txt file:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8.4)
  project(projectname)
  
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpython2.7")
  
  set(SOURCE_FILES
      src/cpp/...
      src/cpp/...
      src/cpp/..
      src/cpp/...
      src/cpp/...)
  
  add_executable(projectname ${SOURCE_FILES})
  </code></pre>
  
  <p>The errors...</p>
  
  <pre><code>CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaeb4): undefined reference to `PyDict_New'
  CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaeb4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyDict_New'
  CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaec4): undefined reference to `PyList_New'
  CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaec4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyList_New'
  CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaf0d): undefined reference to `PyDict_New'
  CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaf0d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `PyDict_New'
  CMakeFiles/spot.dir/src/cpp/OBwrapper.cpp.o:OBwrapper.cpp:(.text+0xaf25): undefined reference to `PyString_FromString'
  
  ...and so on....
  </code></pre>