{"id":6295,"date":"2020-06-08T09:46:29","date_gmt":"2020-06-08T16:46:29","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6295"},"modified":"2020-10-26T17:20:29","modified_gmt":"2020-10-27T00:20:29","slug":"visualgdb-makefiles","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/documentation\/makefiles\/","title":{"rendered":"VisualGDB Makefiles"},"content":{"rendered":"<p>When you create a Makefile-based project using VisualGDB, it generates a <strong>Makefile<\/strong> (and a separate <strong>.mak<\/strong> file for each configuration). That will be used to build the project.<\/p>\n<p><strong>Note that we do not recommend using Makefiles for new projects. For greater reliability, build speed, and a much better integration with Visual Studio GUI, please use <a href=\"https:\/\/visualgdb.com\/documentation\/projects\/msbuild\/\">MSBuild projects<\/a> instead. If you would like to build the projects outside Visual Studio, please use the <a href=\"https:\/\/visualgdb.com\/documentation\/projects\/cmake\/\">Advanced CMake Project Subsystem<\/a> together with Ninja.<\/strong><\/p>\n<h1>Typical Structure<\/h1>\n<p>A VisualGDB-generated Makefile normally has the following structure:<\/p>\n<pre class=\"\">#VisualGDB: AutoSourceFiles\r\n\r\nTARGETNAME := ProjectName.elf\r\nTARGETTYPE := APP\r\nCONFIG ?= DEBUG\r\n\r\n&lt;...&gt;\r\n\r\ninclude $(CONFIGURATION_FLAGS_FILE)\r\ninclude $(ADDITIONAL_MAKE_FILES)\r\n\r\nSOURCEFILES := &lt;...&gt;\r\n\r\n#VisualGDB: FileSpecificTemplates\r\n$(BINARYDIR)\/%.o : %.cpp $(all_make_files) |$(BINARYDIR)\r\n\t$(CXX) $(CXXFLAGS) -c $&lt; -o $@ $(CC_DEPENDENCY_FILE_SPECIFIER) $(@:.o=.dep)\r\n\r\n&lt;...&gt;\r\n\r\n#VisualGDB: GeneratedRules\r\n\r\n$(BINARYDIR)\/startup_stm32f407xx.o : $(BSP_ROOT)\/STM32F4xxxx\/StartupFiles\/startup_stm32f407xx.c $(all_make_files) |$(BINARYDIR)\r\n\t$(CC) $(CFLAGS) -c $&lt; -o $@ $(CC_DEPENDENCY_FILE_SPECIFIER) $(@:.o=.dep)\r\n&lt;...&gt;<\/pre>\n<p>The SOURCEFILES line lists all source files used in the project. VisualGDB will automatically update this line when you add new files to the project. The &#8220;%.cpp&#8221; and &#8220;%.c&#8221; lines define generic rules to build .o files from the C\/C++ source files.<\/p>\n<p>The configuration-specific .mak file (e.g. <strong>debug.mak<\/strong>) contains various configuration-specific parameters:<\/p>\n<pre class=\"\">PREPROCESSOR_MACROS := DEBUG=1\r\nINCLUDE_DIRS := \r\n\r\n&lt;...&gt;\r\n\r\nCFLAGS := -ggdb -ffunction-sections -O0\r\nCXXFLAGS := -ggdb -ffunction-sections -fno-exceptions -fno-rtti -O0\r\n\r\n&lt;...&gt;\r\n\r\nADDITIONAL_MAKE_FILES := stm32.mak\r\nGENERATE_BIN_FILE := 1\r\nGENERATE_IHEX_FILE := 0<\/pre>\n<p>The following table summarizes all variables defined in flags.mak:<\/p>\n<table class=\"neat_table\">\n<tbody>\n<tr>\n<th>Variable<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>CC<\/td>\n<td>Location of the GCC compiler<\/td>\n<td>c:\/gcc\/gcc.exe<\/td>\n<\/tr>\n<tr>\n<td>CXX<\/td>\n<td>Location of the C++ compiler<\/td>\n<td>c:\/gcc\/g++.exe<\/td>\n<\/tr>\n<tr>\n<td>LD<\/td>\n<td>Location of the linker (typically GCC\/G++ is used for linking)<\/td>\n<td>c:\/gcc\/g++.exe<\/td>\n<\/tr>\n<tr>\n<td>COMMON_MACROS<\/td>\n<td>Preprocessor definitions to be defined in all configurations.<\/td>\n<td>_WIN32 _CONSOLE<\/td>\n<\/tr>\n<tr>\n<td>DEBUG_MACROS<\/td>\n<td>Preprocessor definitions to be defined in DEBUG configuration only<\/td>\n<td>_DEBUG DEBUG<\/td>\n<\/tr>\n<tr>\n<td>RELEASE_MACROS<\/td>\n<td>Preprocessor definitions to be defined in RELEASE configuration only<\/td>\n<td>_NDEBUG _RELEASE<\/td>\n<\/tr>\n<tr>\n<td>MCUFLAGS<\/td>\n<td>Additional flags specifying microcontroller type<\/td>\n<td>-mmcu=msp430f2013<\/td>\n<\/tr>\n<tr>\n<td>INCLUDE_DIRS<\/td>\n<td>Additional include directories relative to the project location<\/td>\n<td>..\/lib1 ..\/lib2<\/td>\n<\/tr>\n<tr>\n<td>LIBRARY_DIRS<\/td>\n<td>Additional library directories<\/td>\n<td>..\/lib1\/bin ..\/lib2\/bin<\/td>\n<\/tr>\n<tr>\n<td>LIBRARY_NAMES<\/td>\n<td>Additional libraries to link with (no &#8220;lib&#8221; prefix)<\/td>\n<td>1 2<\/td>\n<\/tr>\n<tr>\n<td>CFLAGS<\/td>\n<td>Additional GCC flags for all configurations<\/td>\n<td>-ggdb -ffunction-sections<\/td>\n<\/tr>\n<tr>\n<td>DEBUG_CFLAGS<\/td>\n<td>Additional GCC flags for DEBUG configuration<\/td>\n<td>-O0<\/td>\n<\/tr>\n<tr>\n<td>RELEASE_CFLAGS<\/td>\n<td>Additional GCC flags for RELEASE configuration<\/td>\n<td>-O3<\/td>\n<\/tr>\n<tr>\n<td>CXXFLAGS<\/td>\n<td>Additional C++ flags for all configurations<\/td>\n<td>-fno-exceptions<\/td>\n<\/tr>\n<tr>\n<td>DEBUG_CXXFLAGS<\/td>\n<td>Additional C++ flags for DEBUG configuration<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>RELEASE_CXXFLAGS<\/td>\n<td>Additional C++ flags for RELEASE configuration<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>MACOS_FRAMEWORKS<\/td>\n<td>Additional framework names (MacOS only)<\/td>\n<td>iokit<\/td>\n<\/tr>\n<tr>\n<td>LDFLAGS<\/td>\n<td>Additional flags to the linker (with -Wl, prefix when GCC is used for linking)<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>DEBUG_LDFLAGS<\/td>\n<td>Additional linker flags for DEBUG configuration<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>RELEASE_LDFLAGS<\/td>\n<td>Additional linker flags for RELEASE configuration<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>START_GROUP<\/td>\n<td>Either an empty string, or &#8220;-Wl,&#8211;start-group&#8221;<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>END_GROUP<\/td>\n<td>Either an empty string, or &#8220;-Wl,&#8211;end-group&#8221;<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The START_GROUP\/END_GROUP flags surround the library\/object file list and allow specifying them in an arbitrary order (otherwise the object file list has to be sorted manually). You can set those variables to empty strings if your linker (e.g. MacOS linker) does not require those flags.<\/p>\n<h1><a id=\"lifecycle\"><\/a>Makefile Lifecycle<\/h1>\n<p>Once the Makefile is created, VisualGDB will not regenerate the Makefile anymore. Instead, when you build the project, it will edit the following statements each time you build the project:<\/p>\n<ul>\n<li><strong>SOURCEFILES<\/strong> will be set to the list of all source files in the .vcxproj file.<\/li>\n<li><strong>EXTERNAL_LIBS<\/strong> will be set to libraries produced by referenced projects.<\/li>\n<li>All rules below the <strong>#VisualGDB: GeneratedRules<\/strong> line will be regenerated based on the <strong>#VisualGDB: GeneratedRules<\/strong> templates.<\/li>\n<\/ul>\n<p>The synchronization process is very straight-forward, e.g. the following contents in the<strong> .vcxproj<\/strong> file:<\/p>\n<pre class=\"\">&lt;ItemGroup&gt;\r\n  &lt;ClCompile Include=\"EmbeddedProject5.cpp\" \/&gt;\r\n  &lt;ClCompile Include=\"system_stm32f4xx.c\" \/&gt;\r\n  &lt;ClCompile Include=\"$(BSP_ROOT)\\STM32F4xxxx\\StartupFiles\\startup_stm32f407xx.c\" \/&gt;\r\n&lt;\/ItemGroup&gt;<\/pre>\n<p>will get translated into this:<\/p>\n<pre class=\"\">SOURCEFILES := EmbeddedProject5.cpp system_stm32f4xx.c $(BSP_ROOT)\/STM32F4xxxx\/StartupFiles\/startup_stm32f407xx.c<\/pre>\n<p>This allows preserving any custom Make targets or rules added to the Makefile and generally allows using VisualGDB with any Makefile structure, as long as it has has the <strong>#VisualGDB: AutoSourceFiles<\/strong> tag and uses <strong>SOURCEFILES<\/strong> and <strong>EXTERNAL_LIBS <\/strong>variables. Note that this is extremely limited compared to <a href=\"https:\/\/visualgdb.com\/documentation\/projects\/cmake\/\">Advanced CMake Project Subsystem<\/a> that supports multiple targets per project and can render the exact CMake project layout in Solution Explorer.<\/p>\n<p>You can also disable the automatic updating of Makefiles by completely removing the <strong>#VisualGDB: AutoSourceFiles<\/strong> line from the Makefile. This will keep your manual edits to <strong>SOURCEFILES<\/strong> and <strong>EXTERNAL_LIBS <\/strong>regardless of the files referenced in the .vxcproj file.<\/p>\n<p>You can edit various common properties (e.g. preprocessor macros) via <strong>VisualGDB Project Properties -&gt; Makefile Settings<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/make.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6296\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/make.png\" alt=\"\" width=\"1018\" height=\"745\" \/><\/a>Any changes made via the VisualGDB GUI will be automatically saved to both Makefile and the VC++-level IntelliSense settings.<\/p>\n<h1><a id=\"embedded\"><\/a>Embedded Makefile Projects<\/h1>\n<p>For Embedded Makefile projects, VisualGDB generates an extra .mak file (e.g. <strong>stm32.mak<\/strong>) containing device-specific flags:<\/p>\n<pre class=\"\">CC := $(TOOLCHAIN_ROOT)\/bin\/arm-none-eabi-gcc.exe\r\nCXX := $(TOOLCHAIN_ROOT)\/bin\/arm-none-eabi-g++.exe\r\n\r\n&lt;...&gt;\r\n\r\nPREPROCESSOR_MACROS += ARM_MATH_CM4 flash_layout STM32F407VG STM32F407xx USE_HAL_DRIVER \r\nINCLUDE_DIRS += . $(BSP_ROOT)\/STM32F4xxxx\/STM32F4xx_HAL_Driver\/Inc &lt;...&gt;<\/pre>\n<p>This file will get included for all configurations of the project and will get regenerated each time you change the MCU-specific settings, or click the &#8220;Regenerate MCU files&#8221; button in VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/regenerate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6297\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/regenerate.png\" alt=\"\" width=\"1018\" height=\"745\" \/><\/a><\/p>\n<h1><a id=\"building\"><\/a>Building Outside Visual Studio<\/h1>\n<p>The Makefile projects can be built independently from Visual Studio (e.g. on a continuous integration server). You can dump the build command used by VisualGDB into a batch file via the VisualGDB Build window and then run it manually:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6298\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/build.png\" alt=\"\" width=\"844\" height=\"323\" \/><\/a><\/p>\n<h1><a id=\"intellisense\"><\/a>IntelliSense<\/h1>\n<p>Internally, Makefile-based projects are implemented as VC++ NMake-based projects. Their IntelliSense settings (e.g. include directories and preprocessor macros) are stored in the NMake settings:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/make-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6380\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/make-1.png\" alt=\"\" width=\"875\" height=\"544\" \/><\/a>Note that the preprocessor macros are stored in the <strong>gcc_&lt;Configuration&gt;.h<\/strong> files that are referenced as forced includes.<br \/>\nWhen you use the VisualGDB GUI to edit the Makefile settings, it updates the NMake settings based on them, however modifying the Makefiles manually will not have the same effect. For better IntelliSense integration, please consider using Advanced CMake projects, or MSBuild projects.<\/p>\n<h1><a id=\"troubleshooting\"><\/a>Troubleshooting<\/h1>\n<p>GNU Make projects will always display the full GCC\/linker command lines during build:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/makecmd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6355\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/06\/makecmd.png\" alt=\"\" width=\"840\" height=\"391\" \/><\/a>You can use them to see the exact parameters passed to the compiler, and troubleshoot the build problems.<\/p>\n<h1><a id=\"limitations\"><\/a>Limitations<\/h1>\n<p>Makefile-based projects are relatively limited compared to other project types. The list below shows their main limitations:<\/p>\n<ul>\n<li>Makefile projects do not automatically create response files (.rsp). Hence some embedded BSPs (e.g. Nordic nRF5x) could exceed the Windows command line length limit and fail the build.<\/li>\n<li>Makefile projects store the list of source files redundantly. The list shown in Solution Explorer is stored in the <strong>.vcxproj<\/strong> file, and the list used for the actual build is shown stored in the Makefile itself (<strong>SOURCEFILES<\/strong>). Although VisualGDB synchronizes them during build, it may not work in some advanced cases (e.g. MSBuild property sheets or dynamic MSBuild rules).<\/li>\n<li>Makefile projects store IntelliSense settings (include directories and preprocessor macros) separately from the actual build settings. VisualGDB automatically updates IntelliSense settings when using VisualGDB Project Properties, however editing the Makefile manually will keep the old IntelliSense settings.<\/li>\n<li>Editing the main Makefile, or any of the .mak files will invalidate all object files, and not only affected ones (i.e. next build will be equivalent to a full rebuild).<\/li>\n<li>Makefile-based projects do not support precompiled headers and cannot have different build settings (e.g. preprocessor macros) for different source files.<\/li>\n<\/ul>\n<p>If you are planning to use Makefiles for a new project, please consider using <a href=\"https:\/\/visualgdb.com\/documentation\/projects\/msbuild\/\">MSBuild<\/a> or <a href=\"https:\/\/visualgdb.com\/documentation\/projects\/cmake\/\">Advanced CMake<\/a> instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you create a Makefile-based project using VisualGDB, it generates a Makefile (and a separate .mak file for each configuration).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[201],"tags":[214],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6295"}],"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=6295"}],"version-history":[{"count":7,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6295\/revisions"}],"predecessor-version":[{"id":7056,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6295\/revisions\/7056"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}