{"id":57,"date":"2013-04-29T23:20:08","date_gmt":"2013-04-29T21:20:08","guid":{"rendered":"http:\/\/jv-coder.de\/wordpress\/?p=57"},"modified":"2015-04-26T23:04:48","modified_gmt":"2015-04-26T21:04:48","slug":"cmake-bits-and-pieces","status":"publish","type":"post","link":"https:\/\/jv-coder.de\/wordpress\/cmake-bits-and-pieces\/","title":{"rendered":"CMake Bits and Pieces"},"content":{"rendered":"<p>When working with CMake and Visual Studio some things are a little hard to achieve. This post will show you three things I stumbled across:<\/p>\n<p><!--more--><\/p>\n<p>1. Activating precompiled headers:<br \/>\nThis is quiet easy to do:<\/p>\n<pre class=\"lang:default decode:true\">function(use_precompiled_header TARGET HEADER_FILE SRC_FILE)\r\n  get_filename_component(HEADER ${HEADER_FILE} NAME)\r\n\r\n  if (MSVC)\r\n\tadd_definitions(\/Yu\"${HEADER}\")\r\n    set_source_files_properties(${SRC_FILE}\r\n      PROPERTIES COMPILE_FLAGS \/Yc\"${HEADER}\"\r\n\t)\r\n  endif ()\r\nendfunction(use_precompiled_header)<\/pre>\n<p>This function can be called after an executable or library is defined with\u00a0<span class=\"lang:default decode:true  crayon-inline\">use_precompiled_header(&lt;name&gt; \"stdafx.h\" \"stdafx.cpp\")<\/span>. This works by simply setting the\u00a0<span class=\"lang:default decode:true  crayon-inline \">\/Yc<\/span> option for the <span class=\"lang:default decode:true  crayon-inline \">stdafx.cpp<\/span> and the option <span class=\"lang:default decode:true  crayon-inline \">\/Yu<\/span> for all other files. This is exactly what you would do if you configure it on your own.<\/p>\n<p>2. Sadly there is a subtle bug in recent VC versions, triggering the error\u00a0<span class=\"lang:c++ highlight:0 decode:true  crayon-inline\">fatal error C1027: Inconsistent values for \/Ym between creation and use of precompiled header<\/span>.<br \/>\nThe switch\u00a0<span class=\"lang:c++ decode:true  crayon-inline \">\/Ym<\/span> is used internally by the compiler to pass the start address of the precompiled header memory<sup>[<a href=\"#ref_1\">1<\/a>]<\/sup>. Precompiled headers are essentially a memory snapshot of the parser that can be loaded into memory the next time the PCH are required. Because it is just a snapshot, there are pointers in it that require loading the PCH to the same memory location where it was when the snapshot was taken. That is the reason for the switch. The compiler tries to allocate the memory as soon as possible, because dlls that are loaded may be loaded into the memory required for the PCH. (The reason for this is ASLR - \u00a0Address Space Layout Randomization - that is used to load dlls in different locations, every time they are loaded to make it harder to exploit bugs.)<br \/>\nIt turns out, that the switch\u00a0<span class=\"lang:c++ highlight:0 decode:true  crayon-inline\">\/Zm1000<\/span>\u00a0is set by CMake for whatever reason. That flag is used to specify the maximum size of precompiled headers (since CMake doesn't seem to know about precompiled headers I don't know, why it uses this switch...). A value of 1000 means 1000 times 0.5 MB = 500 MB. The default value is 100 = 50 MB. A user on Microsoft Connect<sup>[<a href=\"#ref_1\">1<\/a>]<\/sup> reported that removing this flag resolved his build issues. I think the reason this works is the following: If we need 500 MB of contiguous memory starting at a specific location, the probability that\u00a0the memory\u00a0is used by other allocations\u00a0is 10 times as high as for 50 MB of contiguous memory.<br \/>\nNevertheless, the MSDN says you shouldn't set\u00a0<span class=\"lang:c++ highlight:0 decode:true  crayon-inline \">\/Zm<\/span> unless the compiler tells you to\u00a0with error C1076\u00a0<span class=\"lang:c++ highlight:0 decode:true  crayon-inline\">compiler limit : internal heap limit reached; use \/Zm to specify a higher limit<\/span><sup>[<a href=\"#ref_2\">2<\/a>]<\/sup> Removing this switch makes the build more stable. The following CMake code removes the switch:<\/p>\n<pre class=\"lang:default decode:true\">string(REGEX REPLACE \"\/Zm[0-9]+ *\" \"\" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})\r\nset(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS}\" CACHE STRING \"\" FORCE)<\/pre>\n<p>The first line removes the option from the CXX_FLAGS and the second line ensures the new flags are inserted into the CMake cache.<\/p>\n<p>3. If you want to get rid of the MinSizeRel or any other configuration you can use the following snippet:<\/p>\n<pre class=\"lang:default decode:true\">set(CMAKE_CONFIGURATION_TYPES Debug Release RelWithDebInfo)\r\nset(CMAKE_CONFIGURATION_TYPES \"${CMAKE_CONFIGURATION_TYPES}\" CACHE STRING \"\" FORCE)<\/pre>\n<p>This snippet resets the configuration types variable and inserts it into the cache again. You can also add more configurations using this variable and setting CMAKE_CXX_FLAGS_&lt;TYPE&gt; and other variables suffixed with the new type. (I had to delete the generated project files in order to get this to work correctly.)<\/p>\n<p>&nbsp;<\/p>\n<p>[<a name=\"ref_1\"><\/a>1] <a href=\"http:\/\/connect.microsoft.com\/VisualStudio\/feedback\/details\/731299\/error-c1027-inconsistent-values-for-ym-between-creation-and-use-of-precompiled-header-c\">http:\/\/connect.microsoft.com\/VisualStudio\/feedback\/details\/731299\/error-c1027-inconsistent-values-for-ym-between-creation-and-use-of-precompiled-header-c<\/a><br \/>\n[<a name=\"ref_2\"><\/a>2]\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bdscwf1c(v=vs.71).aspx\">http:\/\/msdn.microsoft.com\/en-us\/library\/bdscwf1c(v=vs.71).aspx<br \/>\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with CMake and Visual Studio some things are a little hard to achieve. This post will show you three things I stumbled across:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/posts\/57"}],"collection":[{"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":16,"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/posts\/57\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jv-coder.de\/wordpress\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}