{"id":5146,"date":"2019-08-12T10:41:50","date_gmt":"2019-08-12T17:41:50","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=5146"},"modified":"2019-08-12T10:41:50","modified_gmt":"2019-08-12T17:41:50","slug":"cross-compiling-linux-c-c-projects-for-stm32mp1-devices","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/stm32mp1\/cross\/","title":{"rendered":"Cross-compiling Linux C\/C++ Projects for STM32MP1 Devices"},"content":{"rendered":"<p>This tutorial shows how to use a Windows-based cross-toolchain to build and debug Linux projects on the STM32MP1 devices. We will create a basic &#8220;Hello, World&#8221; application using the GTK+ toolkit and will show how to build it on Windows, debug it and view the source code of the GTK functions on the call stack.<\/p>\n<p>Before you begin, install VisualGDB 5.4R11 or later and ensure you have an STM32MP1 board ready.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Linux Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/01-wizard.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5129\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/01-wizard.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Specify the name and location for your project and click &#8220;Create&#8221; to start the VisualGDB&#8217;s part of the wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/01-prjname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5147\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/01-prjname.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>On the first page of the wizard select &#8220;Create a new project -&gt; Application -&gt; MSBuild&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/02-msbuild.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5148\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/02-msbuild.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select &#8220;Build the project locally with a cross-compiler&#8221; and pick the STM32MP1 cross-toolchain. If you haven&#8217;t installed it yet, VisualGDB will automatically download and install one for you:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/03-stm32mp1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5149\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/03-stm32mp1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Ensure you have uploaded a compatible SD card image to your STM32MP1 board. You can download pre-built images compatible with our toolchains from our <a href=\"https:\/\/github.com\/sysprogs\/oe-manifest\/releases\">Github page<\/a>. Then plug in both power and Ethernet connectors on the board (optionally use the ST-Link&#8217;s virtual COM port to find out the board&#8217;s IP address and double-check its network status):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/netconn.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5143\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/netconn.jpg\" alt=\"\" width=\"1280\" height=\"610\" \/><\/a><\/li>\n<li>Select the board&#8217;s connection in the &#8220;Deployment computer&#8221; field. By default, the STM32MP1 board will have a host name of <strong>stm32mp1<\/strong> and have a root user with an empty password:\u00a0 <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/04-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5150\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/04-target.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. VisualGDB will automatically test the toolchain and ensure it produces binaries compatible with the board:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/05-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5151\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/05-test.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Once the project has been created, set a breakpoint at the end of main() and press F5 to build it and start debugging:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/06-debug-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5152\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/06-debug-1.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a><\/li>\n<li>Now we will show how to use GTK+ library with VisualGDB. Replace the contents of the main source file with the basic <a href=\"https:\/\/developer.gnome.org\/gtk3\/stable\/gtk-getting-started.html\">GTK example<\/a>:\n<pre class=\"\">#include &lt;gtk\/gtk.h&gt;\r\n\r\nstatic void\r\nprint_hello (GtkWidget *widget,\r\n             gpointer   data)\r\n{\r\n  g_print (\"Hello World\\n\");\r\n}\r\n\r\nstatic void\r\nactivate (GtkApplication *app,\r\n          gpointer        user_data)\r\n{\r\n  GtkWidget *window;\r\n  GtkWidget *button;\r\n  GtkWidget *button_box;\r\n\r\n  window = gtk_application_window_new (app);\r\n  gtk_window_set_title (GTK_WINDOW (window), \"Window\");\r\n  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);\r\n\r\n  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);\r\n  gtk_container_add (GTK_CONTAINER (window), button_box);\r\n\r\n  button = gtk_button_new_with_label (\"Hello World\");\r\n  g_signal_connect (button, \"clicked\", G_CALLBACK (print_hello), NULL);\r\n  g_signal_connect_swapped (button, \"clicked\", G_CALLBACK (gtk_widget_destroy), window);\r\n  gtk_container_add (GTK_CONTAINER (button_box), button);\r\n\r\n  gtk_widget_show_all (window);\r\n}\r\n\r\nint\r\nmain (int    argc,\r\n      char **argv)\r\n{\r\n  GtkApplication *app;\r\n  int status;\r\n\r\n  app = gtk_application_new (\"org.gtk.example\", G_APPLICATION_FLAGS_NONE);\r\n  g_signal_connect (app, \"activate\", G_CALLBACK (activate), NULL);\r\n  status = g_application_run (G_APPLICATION (app), argc, argv);\r\n  g_object_unref (app);\r\n\r\n  return status;\r\n}<\/pre>\n<p>Note that initially VisualGDB will will have to guess the include directories containing GTK+ files: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/07-files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5153\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/07-files.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a><\/li>\n<li>You could proceed with using the guessed directories (although you would then need to specify library names), however in this tutorial we will use the <strong>pkg-config<\/strong> tool to find the include directories and libraries declared by the gtk+-3.0 package. Open the <strong>&lt;toolchain&gt;\\usr\\bin<\/strong> folder in a Command Prompt and run the following commands:\n<pre class=\"\">pkg-config.bat --cflags gtk+-3.0 &gt; cflags.txt\r\npkg-config.bat --libs gtk+-3.0 &gt; libs.txt<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/08-pkg-config.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5154\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/08-pkg-config.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a>Normally, you should get the following cflags file:<\/p>\n<pre class=\"\">-pthread\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/gtk-3.0\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/atk-1.0\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/at-spi2-atk\/2.0\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/pango-1.0\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/gio-unix-2.0\/\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/cairo\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/gdk-pixbuf-2.0\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/glib-2.0\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/lib\/glib-2.0\/include\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/at-spi-2.0\r\n-I\/usr\/include\/dbus-1.0\r\n-I\/usr\/lib\/dbus-1.0\/include\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/libdrm\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/harfbuzz\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/freetype2\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/fribidi\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/pixman-1\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include\/libpng16\r\n-IC:\/SysGCC\/stm32mp1\/usr\/bin\/\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/include<\/pre>\n<p>The libs.txt should contain the following:<\/p>\n<pre class=\"\">-LC:\/SysGCC\/stm32mp1\/usr\/bin\/..\/..\/cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\/sysroot\/usr\/lib \r\n-lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0\r\n-lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0<\/pre>\n<\/li>\n<li>As the cflags.txt mostly contains the include directories (the -pthread flag can be ignored), we recommend replacing the -I&lt;path to sysroot&gt; part with just &#8216;=&#8217;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/09-replace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5155\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/09-replace.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a>Also replace spaces with semicolons:\n<pre class=\"\">=\/usr\/include\/gtk-3.0;\r\n=\/usr\/include\/atk-1.0;\r\n=\/usr\/include\/at-spi2-atk\/2.0;\r\n=\/usr\/include;\r\n=\/usr\/include\/pango-1.0;\r\n=\/usr\/include\/gio-unix-2.0\/;\r\n=\/usr\/include\/cairo;\r\n=\/usr\/include\/gdk-pixbuf-2.0;\r\n=\/usr\/include\/glib-2.0;\r\n=\/usr\/lib\/glib-2.0\/include;\r\n=\/usr\/include\/at-spi-2.0;\r\n=\/usr\/include\/dbus-1.0;\r\n=\/usr\/lib\/dbus-1.0\/include;\r\n=\/usr\/include\/libdrm;\r\n=\/usr\/include\/harfbuzz;\r\n=\/usr\/include\/freetype2;\r\n=\/usr\/include\/fribidi;\r\n=\/usr\/include\/pixman-1;\r\n=\/usr\/include\/libpng16;\r\n=\/usr\/include;<\/pre>\n<p>In the libs.txt file remove the &#8220;-l&#8221; prefix, the library search path (with the -L option) and also replaces spaces with semicolons:<\/p>\n<pre class=\"\">gtk-3;gdk-3;atk-1.0;gio-2.0;pangocairo-1.0;gdk_pixbuf-2.0;cairo-gobject;pango-1.0;cairo;gobject-2.0;glib-2.0<\/pre>\n<\/li>\n<li>Now copy the include directories and library names to VisualGDB Project Properties as shown below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/10-parameters.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5156\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/10-parameters.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a><\/li>\n<li>Once you apply the new properties, you will be able to build the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/11-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5157\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/11-built.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a><\/li>\n<li>Set a breakpoint inside print_hello() and press F5 to start debugging. Observe the Hello, World window on the board&#8217;s LCD screen:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/hello.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5162\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/hello.jpg\" alt=\"\" width=\"1200\" height=\"598\" \/><\/a><\/li>\n<li>Tap the &#8220;Hello World&#8221; button and observe the breakpoint being triggered:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/12-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5158\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/12-bkpt.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a><\/li>\n<li>You can now debug your application using the regular Visual Studio functionality, however you will only see the source code of your own application. In order to display the sources of the Linux components shipped with the STM32MP1 toolchain, add a path mapping between <strong>\/usr\/src\/debug<\/strong> and <strong>$(ToolchainDir)\\cortexa7t2hf-neon-vfpv4-openstlinux_weston-linux-gnueabi\\sysroot\\usr\\src\\debug<\/strong> to the Path Mapping page of VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/13-path.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5159\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/13-path.png\" alt=\"\" width=\"1078\" height=\"532\" \/><\/a><\/li>\n<li>Now you will be able to see the GTK+ sources and step through them in the debugger, although due to optimization, the detail level will be lower, compared to debugging unoptimized code:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/14-source.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5160\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/07\/14-source.png\" alt=\"\" width=\"1135\" height=\"808\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use a Windows-based cross-toolchain to build and debug Linux projects on the STM32MP1 devices. We<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[72,33,61,187],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5146"}],"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=5146"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5146\/revisions"}],"predecessor-version":[{"id":5163,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5146\/revisions\/5163"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=5146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=5146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=5146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}