title: CMake unable to link libxml2 in CLion
body: <p><strong>tl;dr add <code>set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )</code> to <code>CMakeLists.txt</code> (more infos below).</strong></p>
  
  <hr>
  
  <p>I want to run a very simple application within CLion. This application uses libxml2.</p>
  
  <p>I use CLion 2018.1.2 on Ubuntu 18.04 LTS, <code>libxml2-dev</code> is properly installed.</p>
  
  <p>My project does compile with:</p>
  
  <pre><code>g++ -I /usr/include/libxml2 xmlTest.cpp -o xmlTest.exe -lxml2
  </code></pre>
  
  <p>it does not compile in CLion though, using this <code>CMakeLists.txt</code>:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.10)
  project(xmlTest)
  
  set(CMAKE_CXX_STANDARD 11)
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2 -lxml2 -lz -lm")
  
  include_directories( /usr/local/include/libxml2 )
  
  add_executable (xmlTest.exe xmlTest.cpp)
  </code></pre>
  
  <p>the compiler throws me:</p>
  
  <pre><code>/home/xxxx/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/181.4668.70/bin/cmake/bin/cmake --build /home/xxxx/src/cmake-build-debug --target xmlTest.exe -- -I /usr/include/libxml2 -lxml2
  Scanning dependencies of target xmlTest.exe
  [ 50%] Building CXX object CMakeFiles/xmlTest.exe.dir/xmlTest.cpp.o
  [100%] Linking CXX executable xmlTest.exe
  CMakeFiles/xmlTest.exe.dir/xmlTest.cpp.o: In function `main':
  /home/xxxx/src/xmlTest.cpp:10: undefined reference to `xmlReadFile'
  /home/xxxx/src/xmlTest.cpp:17: undefined reference to `xmlDocGetRootElement'
  /home/xxxx/src/xmlTest.cpp:21: undefined reference to `xmlFreeDoc'
  /home/xxxx/src/xmlTest.cpp:25: undefined reference to `xmlStrcmp'
  /home/xxxx/src/xmlTest.cpp:27: undefined reference to `xmlFreeDoc'
  </code></pre>
  
  <p>my application (<code>xmlTest.cpp</code>):</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;libxml/parser.h&gt;
  #include &lt;libxml/tree.h&gt;
  
  int main(int argc, char const *argv[]) {
    xmlDocPtr doc;
    xmlNodePtr cur;
  
    doc = xmlReadFile(argv[1], NULL, 0);
  
    if (doc == NULL ) {
      fprintf(stderr,"Document not parsed successfully. \n");
      return 1;
    }
  
    cur = xmlDocGetRootElement(doc);
  
    if (cur == NULL) {
      fprintf(stderr,"empty document\n");
      xmlFreeDoc(doc);
      return 1;
    }
  
    if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "users")) {
      fprintf(stderr,"document of the wrong type, root node != story");
      xmlFreeDoc(doc);
      return 1;
    }
  
    return 0;
  }
  </code></pre>
  
  <p>after fiddling with libxml2 and CMake for hours now without success, I am open to any idea what to do to tell CMake <code>libxml2</code> actually exists...</p>
  
  <hr>
  
  <p><strong>Update #1:</strong></p>
  
  <p>thanks to @vre's suggestion I updated my <code>CMakeLists.txt</code> file:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.10)
  project(xmlTest)
  
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2 -lxml2 -lz -lm")
  set(CMAKE_CXX "/usr/local/include/libxml2")
  find_library(libxml2 libxml2)
  link_directories(/usr/local/include/libxml2)
  add_compile_options("-I/usr/include/libxml2 -lxml2 -lz -lm")
  find_package(LibXml2 REQUIRED)
  add_executable (xmlTest.exe xmlTest.cpp)
  target_compile_definitions(xmlTest.exe PUBLIC ${LibXml2_DEFINITIONS})
  target_include_directories(xmlTest.exe PUBLIC ${LibXml2_INCLUDE_DIRS})
  target_link_libraries(xmlTest.exe ${LibXml2_LIBRARIES})
  </code></pre>
  
  <p>This did not change or resolve the error though.</p>
  
  <p><strong>Update #2 (solution):</strong></p>
  
  <p>this is how to build projects with <code>libxml2</code> in CLion:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.10)
  project(xmlTest)
  
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2")
  set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )
  
  add_executable (xmlTest.exe xmlTest.cpp)
  </code></pre>
  
  <p>My issue was that I had the wrong order of arguments in my <code>CMAKE_CXX_FLAGS</code>. <a href="https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc">This article</a> points this out. Using <code>set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )</code> was the key (<a href="https://stackoverflow.com/questions/34131414/link-a-library-as-last-against-all-targets">see this</a>)</p>
  
  <p>I hope this helps some future timetravelers:</p>