title: Adding Boost to CMake project?
body: <h2>Background</h2>
  
  <p>I'm a complete newb with C++ and I've been running into one headache after another, so forgive me if this is incredibly simple and I'm <strong><em>just that dumb</em></strong>...</p>
  
  <p>I have a project that should ultimately compile and run in Linux. Unfortunately after <a href="https://stackoverflow.com/questions/54521402/locating-iostream-in-clang">lots of issues with my C++ development environemnt</a> (still unresolved), I gave up on trying to develop in Linux and moved to Windows Visual Studio 2017. My hope was to get my code working in Windows and then, since C++ is supposedly a portable language, it should just work in Linux with minimal changes.</p>
  
  <p>For a day or so Visual Studio seemed to be working. I could write code, hit "compile", and like magic it would run. I threw together a few classes to construct a directed acyclic graph, used the standard library for a hash table, and then I tried to create a socket...</p>
  
  <p>Windows and Linux use different libraries for sockets (<code>&lt;sys/socket.h&gt;</code> vs <code>&lt;winsock.h&gt;</code>) so I needed some way to abstract the differences, and I preferred a well-established standard. Googling around I found <a href="https://www.boost.org/" rel="noreferrer">the Boost library</a> that seemed to fit my needs... That's when everything went to hell.</p>
  
  <h2>My project setup</h2>
  
  <p>Because this project will be developed across a variety of platforms and IDEs (some people use Windows + Visual Studio, some people use Mac + Eclipse, and others use Linux + VIM) I opted to make it a CMake project. After several hours of reading and learning and research it seems like CMake should give me what I want (convenient and reproducible cross-platform builds with little or no dependency issues)</p>
  
  <p><a href="https://i.stack.imgur.com/3pNJU.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/3pNJU.jpg" alt="Creating a new project, pt 1"></a>
  <a href="https://i.stack.imgur.com/LIpE1.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/LIpE1.jpg" alt="Creating a new project, pt 2"></a></p>
  
  <p>My source code (directly from the <a href="https://www.boost.org/doc/libs/1_69_0/more/getting_started/windows.html" rel="noreferrer">Boost Getting Started on Windows</a> guide) is as follows:</p>
  
  <p><strong>CMakeProject2.cpp</strong></p>
  
  <pre><code>#include &lt;boost/lambda/lambda.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;iterator&gt;
  #include &lt;algorithm&gt;
  
  int main()
  {
      using namespace boost::lambda;
      typedef std::istream_iterator&lt;int&gt; in;
  
      std::for_each(
          in(std::cin), in(), std::cout &lt;&lt; (_1 * 3) &lt;&lt; " ");
  }
  </code></pre>
  
  <p>Per the <a href="https://www.boost.org/doc/libs/1_69_0/more/getting_started/windows.html" rel="noreferrer">Boost Getting Started on Windows</a> guide, I downloaded Boost from here:</p>
  
  <p><a href="https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.zip" rel="noreferrer">https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.zip</a></p>
  
  <p>(interestingly, the Getting Started guide is titled "Boost Getting Started on Windows - 1.69.0", yet it linked to Boost versions 1.67.0)</p>
  
  <p>After downloading and extracting the ZIP file, I had a whole mess of files - but no idea where to put them:</p>
  
  <p><a href="https://i.stack.imgur.com/FeDz9.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/FeDz9.jpg" alt="Boost download directory"></a></p>
  
  <h2>Attempts to Get It Working So Far</h2>
  
  <p>I tried to <a href="https://stackoverflow.com/questions/45974108/how-to-add-boost-library-1-65-or-1-64-to-visual-studio-2017-project">add the Boost library to my project</a>, but none of the expected menu options were available:</p>
  
  <p><a href="https://i.stack.imgur.com/4vpsE.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/4vpsE.jpg" alt="Right-clicking on project in Solution Explorer"></a></p>
  
  <p>Although I <a href="https://stackoverflow.com/questions/10588734/add-folders-in-a-visual-c-project">couldn't</a> <a href="https://docs.microsoft.com/en-us/cpp/ide/vcpp-directories-property-page?view=vs-2017" rel="noreferrer">find</a> <a href="https://stackoverflow.com/questions/13162656/is-there-anyway-to-set-the-vc-directories-at-project-level-in-vs2008-as-in-vs2">a</a> <a href="https://stackoverflow.com/questions/212492/how-do-you-add-external-libraries-for-compilation-in-vc">single</a> <a href="https://stackoverflow.com/questions/45815860/specify-external-library-for-linux-from-visual-studio-2017">page</a> that warns you of this gotcha, apparently CMake projects don't have the elusive "Properties" window - and instead third party libraries must somehow be included via the CMakeLists.txt file</p>
  
  <p>For starters, I copied the <strong><em>entire</em></strong> 540 MB contents of the Boost ZIP file to within my project under the folder name "Boost":</p>
  
  <p><a href="https://i.stack.imgur.com/aNTU1.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/aNTU1.jpg" alt="Boost in project"></a></p>
  
  <p>I then tried a series of different CMakeList.txt commands:</p>
  
  <p>Per <a href="https://stackoverflow.com/questions/6646405/how-do-you-add-boost-libraries-in-cmakelists-txt">How do you add Boost libraries in CMakeLists.txt?</a>:</p>
  
  <pre><code>set(Boost_USE_STATIC_LIBS OFF) 
  set(Boost_USE_MULTITHREADED ON)  
  set(Boost_USE_STATIC_RUNTIME OFF) 
  find_package(Boost REQUIRED COMPONENTS lambda) 
  
  if(Boost_FOUND)
      include_directories(${Boost_INCLUDE_DIRS}) 
      add_executable(CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h") 
      target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})
  endif()
  </code></pre>
  
  <p>Per <a href="https://www.selectiveintellect.net/blog/2016/7/29/using-cmake-to-add-third-party-libraries-to-your-project-1" rel="noreferrer">https://www.selectiveintellect.net/blog/2016/7/29/using-cmake-to-add-third-party-libraries-to-your-project-1</a>:</p>
  
  <pre><code>include("Boost")
  add_subdirectory("Boost")
  add_subdirectory("boost")
  add_subdirectory("Boost/boost")
  add_subdirectory("Boost/boost/lambda")
  
  target_link_libraries(boost)
  target_link_libraries(Boost)
  </code></pre>
  
  <p>Per <a href="https://cmake.org/pipermail/cmake/2009-November/033249.html" rel="noreferrer">https://cmake.org/pipermail/cmake/2009-November/033249.html</a>:</p>
  
  <pre><code>SET (Boost_FIND_REQUIRED TRUE)
  SET (Boost_FIND_QUIETLY TRUE)
  SET (Boost_DEBUG FALSE)
  set (Boost_USE_MULTITHREADED TRUE)
  set (Boost_USE_STATIC_LIBS TRUE)
  SET (Boost_ADDITIONAL_VERSIONS "1.67" "1.67.0")
  
  FIND_PACKAGE(Boost COMPONENTS  lambda)
  INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
  LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
  </code></pre>
  
  <p>I tried several other incantations (not being familiar with C++ <strong><em>or</em></strong> CMake as a tool) and either received errors from CMakeLists.txt, or from CMakeProject2.cpp about <code>cannot open source file "boost/lambda/lambda.hpp"</code>. In fact, with regards to those "CMakeLists.txt" errors, after adding <strong><em>enough</em></strong> lines to my file I started to crash Visual Studio regularly. Note that I have an 8th generation i7, 32 gigabytes of RAM, and an M.2 NVMe hard drive -- so I was rather impressed that a few lines in a text file pissed off Microsoft enough to lock up my computer for 10 minutes at a time.</p>
  
  <p>Failing all of that, I tried copying the files I needed directly into my project:</p>
  
  <p><a href="https://i.stack.imgur.com/oAwjc.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/oAwjc.jpg" alt="New project setup"></a></p>
  
  <p>Now, again, I'm new to C/C++ development and everything that can go wrong <em>has</em> gone wrong. So far I've spent almost two weeks and barely managed to compile a single "Hello, World" app across two computers, three IDEs, and four compilers. I've yet to have any success including a third party library, from anywhere, of any popularity level or simplicity level, and actually compile a functioning program that references the library. So believe me when I say: I don't know the difference between a "header-only library" and... whatever the alternative is. I just know that, according to the <a href="https://www.boost.org/doc/libs/1_69_0/more/getting_started/windows.html" rel="noreferrer">Boost Getting Started on Windows</a> guide, <strong><em>most</em></strong> of Boost is "headers only" and therefore I shouldn't have any build step -- it should be simple to use it. Furthermore, this example (using <code>boost::lambda</code>) is - per their instructions - a header-only library, and should therefore be extremely easy to use.</p>
  
  <p>I now updated the source code slightly to look in the current directory, instead of looking in the system include directory (which, as far as I'm aware at this point, doesn't exist in Windows):</p>
  
  <pre><code>#include "boost/lambda/lambda.hpp"
  #include &lt;iostream&gt;
  #include &lt;iterator&gt;
  #include &lt;algorithm&gt;
  
  int main()
  {
      using namespace boost::lambda;
      typedef std::istream_iterator&lt;int&gt; in;
  
      std::for_each(
          in(std::cin), in(), std::cout &lt;&lt; (_1 * 3) &lt;&lt; " ");
  }
  </code></pre>
  
  <p>Now I can <strong><em>manually verify</em></strong> that this file exists (the file <code>CMakeProject2\CMakeProject2\boost\lambda\lambda.hpp</code> can be found in File Explorer) - yet I'm <strong><em>still</em></strong> getting errors:</p>
  
  <p><code>cannot open source file "boost/lambda/lambda.hpp"</code></p>
  
  <p>Some further Googling led me to update my CMakeLists.txt file once more, putting it in its current form:</p>
  
  <pre><code># CMakeList.txt : CMake project for CMakeProject2, include source and define
  # project specific logic here.
  #
  cmake_minimum_required (VERSION 3.8)
  
  # Add source to this project's executable.
  file(GLOB CMakeProject2_SRC
      "*.h"
      "*.cpp"
      "**/*.h"
      "**/*.cpp"
      "**/*.hpp"
      "boost/lambda/lambda.hpp"
  )
  add_executable (CMakeProject2 ${CMakeProject2_SRC})
  #add_executable (CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h")
  
  # TODO: Add tests and install targets if needed.
  </code></pre>
  
  <p>Despite this I'm <strong><em>still</em></strong> getting the error:</p>
  
  <p><code>cannot open source file "boost/lambda/lambda.hpp"</code></p>
  
  <p>At this point I'm ripping my hair out. What am I doing wrong? What do I not know? How is something as simple as the Boost-equivalent of "Hello, World" not working for me?</p>