{"id":7451,"date":"2021-08-19T12:26:14","date_gmt":"2021-08-19T19:26:14","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=7451"},"modified":"2021-08-19T12:27:05","modified_gmt":"2021-08-19T19:27:05","slug":"troubleshooting-long-cmake-object-paths-cmake_object_path_max","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/documentation\/cmake\/CMAKE_OBJECT_PATH_MAX\/","title":{"rendered":"Troubleshooting long CMake object paths (CMAKE_OBJECT_PATH_MAX)"},"content":{"rendered":"<p>This page explains how to troubleshoot build errors in CMake projects caused by too long object file paths. This error typically results in the following message when trying to build the project:<\/p>\n<pre class=\"\">ninja: error: mkdir(CMakeFiles\/&lt;very long path&gt;): No such file or directory<\/pre>\n<p>You can also confirm it by reloading the project via context menu in Solution Explorer and checking the <strong>VisualGDB Build<\/strong> window:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/08\/reload.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7452\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/08\/reload.png\" alt=\"\" width=\"461\" height=\"304\" \/><\/a>If you see one or more of the following warnings in the configuration log, the problem is caused by long object file paths:<\/p>\n<pre class=\"\">CMake Warning in CMakeLists.txt:\r\nThe object file directory\r\n\r\n&lt;long path here&gt;\r\n\r\nhas ### characters. The maximum full path to an object file is ### characters (see CMAKE_OBJECT_PATH_MAX). Object file\r\n\r\n&lt;object file path&gt;\r\n\r\ncannot be safely placed under this directory. The build may not work correctly.<\/pre>\n<h2>Cause<\/h2>\n<p>In order to build the proejct, each source file (<strong>.c<\/strong> or .<strong>cpp<\/strong>) included in it needs to be compiled into a corresponding object file (<strong>.o<\/strong>). CMake would normally place the object files under following path:<\/p>\n<pre class=\"\">&lt;build directory&gt;\\CMakeFiles\\&lt;target name&gt;\\&lt;relative path to source file&gt;\\&lt;source file name&gt;.o<\/pre>\n<p>If the source file is located on a different logical drive (e.g. <strong>C:\\users\\&#8230;<\/strong> while the project is located in <strong>D:\\projects<\/strong>), CMake will use <strong>C\/users\/&#8230;<\/strong> as the relative path.<\/p>\n<p>If the resulting path is too long (exceeds the value of the CMAKE_OBJECT_PATH_MAX variable), CMake will try to shorten it to the following form:<\/p>\n<pre class=\"\">&lt;build directory&gt;\\CMakeFiles\\&lt;target name&gt;\\&lt;unique hash&gt;\\&lt;source file name&gt;.o<\/pre>\n<p>If you are using the Sysprogs CMake fork with <a href=\"https:\/\/github.com\/sysprogs\/CMake\/commit\/c5d96017281e4de5ad62f92aaa3c3970d13c01d1\">this patch<\/a>, it can shorten the long file paths even further to the following syntax:<\/p>\n<pre class=\"\">&lt;build directory&gt;\\CMakeFiles\\&lt;target name&gt;\\&lt;unique hash without extension&gt;<\/pre>\n<p>If the length of the <strong>build directory<\/strong> and the <strong>target name<\/strong> are still too long to use this form, CMake will show a warning and proceed with the default unshortened path.<\/p>\n<h2>Workarounds<\/h2>\n<p>This section describes various workarounds to the problem.<\/p>\n<h3>Move the project under a shorter path<\/h3>\n<p>The easiest way to work around this limitation is to move the project under a shorter path. If the project is located under source control, consider creating a symbolic link:<\/p>\n<pre class=\"\">mklink \/d c:\\prj\\Name c:\\long\\path\\under\\source\\control<\/pre>\n<p>You can then open the project using the shorter path (<strong>c:\\prj\\Name<\/strong>) while still keeping it under source control.<\/p>\n<h3>Shorten the build directory name<\/h3>\n<p>Another option would be to reduce the length of the build directory:<\/p>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/08\/build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7453\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/08\/build.png\" alt=\"\" width=\"1061\" height=\"337\" \/><\/a>E.g. changing it from <strong>build\/$(PlatformName)\/$(ConfigurationName)<\/strong> to just <strong>build<\/strong> could reduce final path length by 15 characters (<strong>WARNING: this would cause clashes between debug and release builds<\/strong>).<\/p>\n<h3>Increase CMAKE_OBJECT_PATH_MAX<\/h3>\n<p>Another option would be to increase the value of <strong><span id=\"FullSettingPath\">Tools-&gt;Options-&gt;VisualGDB-&gt;Embedded-&gt;Other-&gt;Maximum CMake Object File Path<\/span><\/strong> as close as possible to the actual <strong>MAX_PATH<\/strong> limit (260). Note that you would need to fully rebuild the project after changing it.<\/p>\n<h3>Shorten target names<\/h3>\n<p>You can also reduce the length of the object file paths by using shorter names for targets (individual executables\/libraries), since CMake uses them to derive the paths. If the target causing problems is generated from a VisualGDB BSP and has a long name (e.g. <strong>com.sysprogs.arm.stm32.&lt;&#8230;&gt;<\/strong>), you force VisualGDB to use a shorter name for it by editing the BSP.XML file as follows:<\/p>\n<ol>\n<li>Locate the <strong>&lt;EmbeddedFramework&gt;<\/strong> element matching the target. Note that mutually exclusive frameworks (e.g. HALs for different device families) will have the same <strong>ClassID<\/strong>. Make sure you are editing the correct one.<\/li>\n<li>Add a <strong>&lt;ShortUniqueName&gt;<\/strong> element inside the <strong>&lt;EmbeddedFramework&gt;<\/strong> element containing the desired name of the library.<\/li>\n<li>Delete the <strong>bsp.cmake<\/strong> and <strong>bsp.cmake.ver<\/strong> files in the BSP directory.<\/li>\n<li>Reload and rebuild the project.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This page explains how to troubleshoot build errors in CMake projects caused by too long object file paths. This error<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[77,191],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7451"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=7451"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7451\/revisions"}],"predecessor-version":[{"id":7455,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7451\/revisions\/7455"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=7451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=7451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=7451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}