{"id":1011,"date":"2016-06-30T22:56:04","date_gmt":"2016-06-30T20:56:04","guid":{"rendered":"https:\/\/iww.inria.fr\/sed-sophia\/?p=1011"},"modified":"2016-07-01T10:50:43","modified_gmt":"2016-07-01T08:50:43","slug":"enforce-c11-flags-using-cmake","status":"publish","type":"post","link":"https:\/\/iww.inria.fr\/sed-sophia\/enforce-c11-flags-using-cmake\/","title":{"rendered":"Enforce c++11 flags using cmake"},"content":{"rendered":"<p><\/p>\n<p>Since CMake 3.2, it is possible to enforce in very simple way the use of c++11 features (see <a href=\"https:\/\/crascit.com\/2015\/03\/28\/enabling-cxx11-in-cmake\/\">Craig Scott&#8217;s blog post<\/a> for more details). Until now in dtk, we had to test the architecture (Apple, Unix or Windows), then check whether the compiler provides c++11 support or not and eventually set dedicated flags manually.<\/p>\n<p><!--more--><\/p>\n<h2>Former way<\/h2>\n<p>Here follows the code used in dtk:<\/p>\n<pre><code class=\"language-cmake\">if(APPLE)\n  set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++11 -I\/usr\/lib\/c++\/v1\")\nendif(APPLE)\n\nif(NOT APPLE AND NOT MSVC)\n\n  include(CheckCXXCompilerFlag)\n  CHECK_CXX_COMPILER_FLAG(\"-std=c++11\" COMPILER_SUPPORTS_CXX11)\n  CHECK_CXX_COMPILER_FLAG(\"-std=c++0x\" COMPILER_SUPPORTS_CXX0X)\n\n  if(COMPILER_SUPPORTS_CXX11)\n    set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++11\")\n\n  elseif(COMPILER_SUPPORTS_CXX0X)\n    set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++0x\")\n\n  else()\n    message(STATUS \"The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.\")\n  endif()\n\nelse(NOT APPLE AND NOT MSVC)\n\n  set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS}\")\n\nendif(NOT APPLE AND NOT MSVC)<\/code><\/pre>\n<p>One can see how painful and not really readable it was.<\/p>\n<h2>New method<\/h2>\n<p>We now just have to add two variable definitions to enforce the use of c++11 features:<\/p>\n<pre><code class=\"language-cmake\">set(CMAKE_CXX_STANDARD 11)\nset(CMAKE_CXX_STANDARD_REQUIRED ON)<\/code><\/pre>\n<p>Obviously, this way is easier.<\/p>\n<p>CMake provides finer tuning as far as the compiler features are concerned. Craig Scott details them very clearly in his <a href=\"https:\/\/crascit.com\/2015\/03\/28\/enabling-cxx11-in-cmake\/\">post<\/a>. <\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Since CMake 3.2, it is possible to enforce in very simple way the use of c++11 features (see Craig Scott&#8217;s blog post for more details). Until now in dtk, we had to test the architecture (Apple, Unix or Windows), then check whether the compiler provides c++11 support or not and\u2026<\/p>\n<p> <a class=\"continue-reading-link\" href=\"https:\/\/iww.inria.fr\/sed-sophia\/enforce-c11-flags-using-cmake\/\"><span>Continue reading<\/span><i class=\"crycon-right-dir\"><\/i><\/a> <\/p>\n","protected":false},"author":848,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[28],"tags":[59,93,43,45],"class_list":["post-1011","post","type-post","status-publish","format-standard","hentry","category-goodpractices","tag-c","tag-cmake","tag-dtk","tag-trick"],"_links":{"self":[{"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/posts\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/users\/848"}],"replies":[{"embeddable":true,"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/comments?post=1011"}],"version-history":[{"count":13,"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/posts\/1011\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/posts\/1011\/revisions\/1026"}],"wp:attachment":[{"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/media?parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/categories?post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iww.inria.fr\/sed-sophia\/wp-json\/wp\/v2\/tags?post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}