Yocto Programming with CodeBlocks: Difference between revisions

From Variscite Wiki
No edit summary
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<!-- Set release according to "release" parameter in URL and use RELEASE_DUNFELL_V1.7_DART-MX8M-MINI as default
<!-- Set release according to "release" parameter in URL and use RELEASE_DUNFELL_V1.7_DART-MX8M-MINI as default
--> {{#vardefine:RELEASE_PARAM|{{#urlget:release}}}} <!--
--> {{INIT_RELEASE_PARAM|RELEASE_DUNFELL_V1.7_DART-MX8M-MINI}} <!--
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM|RELEASE_DUNFELL_V1.7_DART-MX8M-MINI}}}} <!--
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{ #varexists:TOOLCHAIN_TARGET_CROSS_PREFIX | | {{ #vardefine:TOOLCHAIN_TARGET_CROSS_PREFIX | {{#var:TOOLCHAIN_TYPE}} }} }} <!--
--> {{#lst:Debian_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{PageHeader|{{#var:HARDWARE_NAME}} - Yocto Programming with Code::Blocks}} {{DocImage|category1={{#var:HARDWARE_NAME}}|category2=Yocto}}
--> {{#vardefine:PLATFORM_OS|Yocto}}
    {{#varexists:TOOLCHAIN_TARGET_CROSS_PREFIX | |{{#vardefine:TOOLCHAIN_TARGET_CROSS_PREFIX|{{#var:TOOLCHAIN_TYPE}} }} }}
{{#switch:{{#var:SOC_FAMILY}}
|am6={{#vardefine:TOOLCHAIN_TARGET_CROSS_PATH|{{#var:TOOLCHAIN_HOST_SYSROOT_LOCATION}}/usr/bin}}
|#default={{#vardefine:TOOLCHAIN_TARGET_CROSS_PATH|{{#var:TOOLCHAIN_HOST_SYSROOT_LOCATION}}/usr/bin/{{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}} }}
}}
    {{#vardefine:TOOLCHAIN_TARGET_SYSROOT_PATH|{{#var:TOOLCHAIN_TARGET_SYSROOT_LOCATION}} }} <!-- Default to Yocto
--> {{#varexists:DEBIAN_NAME |
    {{#vardefine:PLATFORM_OS|Debian}}
    {{#vardefine:TOOLCHAIN_TARGET_CROSS_PREFIX | {{#sub:{{#var:CROSS_COMPILE}}|0|{{#expr:{{#Len:{{#var:CROSS_COMPILE}}}}-1}}}} }}
    {{#vardefine:TOOLCHAIN_TARGET_CROSS_PATH|/home/user/{{#var:BUILD_FOLDER_NAME}}/toolchain/{{#var:TOOLCHAIN}}/bin }}
    {{#vardefine:TOOLCHAIN_TARGET_SYSROOT_PATH|/home/user/{{#var:BUILD_FOLDER_NAME}}/toolchain/sysroot }} }} <!-- Debian
--> {{PageHeader|{{#var:HARDWARE_NAME}} - {{#var:PLATFORM_OS}} Programming with Code::Blocks}} {{DocImage|category1={{#var:HARDWARE_NAME}}|category2={{#var:PLATFORM_OS}}}}


Code::Blocks is a free C/C++ IDE built around a plugin framework, designed to be very extensible and fully configurable.
Code::Blocks is a free C/C++ IDE built around a plugin framework, designed to be very extensible and fully configurable.
Line 15: Line 27:


Debugging with Code::Blocks requires your preferred SSH server (openssh, dropbear, etc.), gdb, and gdbserver installed on the target device.<br>
Debugging with Code::Blocks requires your preferred SSH server (openssh, dropbear, etc.), gdb, and gdbserver installed on the target device.<br>
{{#switch: {{#var:PLATFORM_OS}} | Yocto=
Append the following to the conf/local.conf file in your Yocto build directory:
Append the following to the conf/local.conf file in your Yocto build directory:


Line 23: Line 36:


Now bitbake your image.
Now bitbake your image.
| Debian=
The packages can be installed by running the following commands on the target:
# apt-get update && apt-get -y upgrade
# apt-get -y install gdbserver openssh-server <!--


-->{{Note|'''Note:''' The package '''openssh-server''' may possibly already be installed.|info}}
If you want to learn how to add the packages at compile time, please refer to the {{Varlink2|Adding Debian packages|{{#var:RELEASE_LINK}}}} guide.
}}
= Setup Host Computer Environment =
= Setup Host Computer Environment =


Line 30: Line 51:
== Install Dependencies ==
== Install Dependencies ==


  $ sudo apt-get -y update
  $ sudo apt-get update
  $ sudo apt-get -y install build-essential gdb gdb-multiarch git
  $ sudo apt-get -y install build-essential gdb gdb-multiarch git


== Install Code::Blocks ==
== Install Code::Blocks ==


  $ sudo apt install codeblocks codeblocks-contrib
  $ sudo apt-get -y install codeblocks codeblocks-contrib


== Install Yocto Toolchain ==
== Install {{#var:PLATFORM_OS}} Toolchain ==


A toolchain is necessary for cross compiling applications. To install the toolchain, follow Variscite's {{Varlink2|Yocto Toolchain installation|{{#var:RELEASE_LINK}}}} guide.
A toolchain is necessary for cross compiling applications. To install the toolchain, follow Variscite's {{Varlink2|{{#var:PLATFORM_OS}} Toolchain installation|{{#var:RELEASE_LINK}}}} guide.


= Configure Code::Blocks =
= Configure Code::Blocks =
This section uses the default toolchain path, assuming you followed the default instruction in Variscite's {{Varlink2|Yocto Toolchain installation|{{#var:RELEASE_LINK}}}} guide.
This section uses the default toolchain path, assuming you followed the default instruction in Variscite's {{Varlink2|{{#var:PLATFORM_OS}} Toolchain installation|{{#var:RELEASE_LINK}}}} guide.


Changes may be required if you installed the toolchain in a different path or used different distro settings.
Changes may be required if you installed the toolchain in a different path or used different distro settings.
 
{{#switch: {{#var:PLATFORM_OS}} | Debian=
{{Note|'''Note:''' The configuration values shown in the pictures rely on a Yocto configuration and may differ for Debian, please use the values given in the text.|info}}
|#default=
}}
== Configure the debugger ==
== Configure the debugger ==


From the menu, click on "Settings" and then on "Debugger..."
From the menu bar, click on "Settings" and then on "Debugger..."


[[File:CodeBlock-MenuSettingsDebugger.jpg]]
[[File:CodeBlock-MenuSettingsDebugger.jpg]]
Line 66: Line 90:


The default path is:
The default path is:
 
  {{#var:TOOLCHAIN_TARGET_CROSS_PATH}}/{{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}-gdb
  {{#var:TOOLCHAIN_HOST_SYSROOT_LOCATION}}/usr/bin/{{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}/{{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}-gdb
 
Then, add the set sysroot command in the "Debugger initialization commands" section, to speedup the gdb startup.  
Then, add the set sysroot command in the "Debugger initialization commands" section, to speedup the gdb startup.  


The default path is:
The default path is:
 
  set sysroot {{#var:TOOLCHAIN_TARGET_SYSROOT_PATH}}
  set sysroot {{#var:TOOLCHAIN_TARGET_SYSROOT_LOCATION}}
 
Finally, click on "OK".
Finally, click on "OK".


Line 83: Line 103:
The next steps depend on the previous section: here we assume the debugger is already configured.
The next steps depend on the previous section: here we assume the debugger is already configured.


From the menu, click on "Settings" and then on "Compiler...".
From the menu bar, click on "Settings" and then on "Compiler...".


[[File:CodeBlock-MenuSettingsCompiler.jpg]]
[[File:CodeBlock-MenuSettingsCompiler.jpg]]
Line 105: Line 125:


The default settings are
The default settings are
* Compiler's installation directory: {{#var:TOOLCHAIN_HOST_SYSROOT_LOCATION}}/usr/bin/{{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}
* Compiler's installation directory: {{#var:TOOLCHAIN_TARGET_CROSS_PATH}}
* C compiler: {{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}-gcc
* C compiler: {{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}-gcc
* C++ compiler: {{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}-g++
* C++ compiler: {{#var:TOOLCHAIN_TARGET_CROSS_PREFIX}}-g++
Line 120: Line 140:


E.g. add the sysroot option:
E.g. add the sysroot option:
  --sysroot={{#var:TOOLCHAIN_TARGET_SYSROOT_LOCATION}}
  --sysroot={{#var:TOOLCHAIN_TARGET_SYSROOT_PATH}}


[[File:CodeBlock-OtherCompilerOptions.jpg]]
[[File:CodeBlock-OtherCompilerOptions.jpg]]
Line 128: Line 148:


E.g. add the sysroot option:
E.g. add the sysroot option:
  --sysroot={{#var:TOOLCHAIN_TARGET_SYSROOT_LOCATION}}
  --sysroot={{#var:TOOLCHAIN_TARGET_SYSROOT_PATH}}


[[File:CodeBlock-OtherLinkerOptions.jpg]]
[[File:CodeBlock-OtherLinkerOptions.jpg]]
Line 146: Line 166:


Create the file '''/home/user/bin/var-sdk.conf''' using the right IP address of the target board:
Create the file '''/home/user/bin/var-sdk.conf''' using the right IP address of the target board:
<syntaxhighlight lang="bash">
{{#lst:{{#var:PLATFORM_OS}}_Platform_Customization|SCRIPT_CODEBLOCKS_VAR_SDK}}
TARGET_DIR="/home/root"
TARGET_IP="192.168.73.165"
TARGET_PORT="3000"
SSH_OPTIONS="-oStrictHostKeyChecking=no"
</syntaxhighlight>


Create the file '''/home/user/bin/var-sdk-deploy.sh''':
Create the file '''/home/user/bin/var-sdk-deploy.sh''':
<syntaxhighlight lang="bash">
{{#lst:{{#var:PLATFORM_OS}}_Platform_Customization|SCRIPT_CODEBLOCKS_VAR_SDK_DEPLOY}}
#!/bin/bash
 
readonly PROGRAM="$1"
PROGRAMNAME="$(basename $PROGRAM)"
 
. /home/user/bin/var-sdk.conf
 
echo "Deploying to target"
 
# prevent "Host key verification failed"
ssh-keygen -f "${HOME}/.ssh/known_hosts" -R "${TARGET_IP}"
 
# delete old binary
ssh ${SSH_OPTIONS} root@${TARGET_IP} "sh -c 'rm -rf ${TARGET_DIR}/${PROGRAMNAME}'"
 
# send the program to the target
scp ${PROGRAM} root@${TARGET_IP}:${TARGET_DIR}/${PROGRAMNAME}
</syntaxhighlight>


Create the file '''/home/user/bin/var-sdk-debug.sh''':
Create the file '''/home/user/bin/var-sdk-debug.sh''':
<syntaxhighlight lang="bash">
{{#lst:{{#var:PLATFORM_OS}}_Platform_Customization|SCRIPT_CODEBLOCKS_VAR_SDK_DEBUG}}
#!/bin/bash
 
readonly PROGRAM="$1"
PROGRAMNAME="$(basename $PROGRAM)"
 
. /home/user/bin/var-sdk.conf
 
echo "Starting GDB Server on Target"
 
# kill gdbserver on target
ssh ${SSH_OPTIONS} root@${TARGET_IP} "sh -c '/usr/bin/killall -q gdbserver'"
 
# start gdbserver on target and fork
ssh ${SSH_OPTIONS} -t root@${TARGET_IP} "sh -c 'XDG_RUNTIME_DIR=/run/user/0 gdbserver localhost:${TARGET_PORT} ${TARGET_DIR}/${PROGRAMNAME}'" &
</syntaxhighlight>


Finally, make the above .sh files executable:
Finally, make the above .sh files executable:
Line 197: Line 179:
= Create a sample "Console application" project with Code::Blocks =
= Create a sample "Console application" project with Code::Blocks =


From the menu, click on "File", then on "New" and then on "Project...".
From the menu bar, click on "File", then on "New" and then on "Project...".


[[File:CodeBlocks-NewProject.jpg]]
[[File:CodeBlocks-NewProject.jpg]]
Line 209: Line 191:
Unless you prefer changing something, click "Next" in the "Welcome" and "Language" windows.
Unless you prefer changing something, click "Next" in the "Welcome" and "Language" windows.


[[File:CodeBlocks-ConsoleApplicationWelcome.jpg]][[File:CodeBlocks-ConsoleApplicationLanguage.jpg]]
[[File:CodeBlocks-ConsoleApplicationWelcome.jpg]]
 
[[File:CodeBlocks-ConsoleApplicationLanguage.jpg]]




Line 222: Line 206:




A sample "Hello World" project will show up in the IDE.
A sample "Hello world" project will show up in the IDE.


[[File:CodeBlocks-ConsoleApplicationHelloWorld.jpg]]
[[File:CodeBlocks-ConsoleApplicationHelloWorld.jpg]]
Line 263: Line 247:




In the "Project/targets option" window, scroll the tabs and click on the "Debugger" tab, then ensure "<Project>" is selected in the left pane.<br>
In the "Project/targets options" window, scroll the tabs and click on the "Debugger" tab, then ensure "<Project>" is selected in the bottom left pane.<br>
In the "Remote connection" area, populate the TARGET_IP and the TARGET_PORT that must match the value stored in file '''/home/user/bin/var-sdk.conf'''
In the "Remote connection" tab, fill in the "IP address" and "Port" fields, with the same values of TARGET_IP and TARGET_PORT from the '''/home/user/bin/var-sdk.conf''' file.


[[File:CodeBlocks-RemoteConnection.jpg]]
[[File:CodeBlocks-RemoteConnection.jpg]]




Still in the same window, click on the "Additional shell commands" area and add the following under "Before connection":
Still in the same window, click on the "Additional shell commands" tab and add the following under "Before connection":
  /home/user/bin/var-sdk-debug.sh $(TARGET_OUTPUT_FILE)
  /home/user/bin/var-sdk-debug.sh $(TARGET_OUTPUT_FILE)
Then click on "OK".
Then click on "OK".
Line 277: Line 261:
== Debug the project ==
== Debug the project ==


As the last step of our example, enable a breakpoint by clicking on the right of the line number you want stop at and start the debugger.
As the last step of our example, set a breakpoint by clicking to the right of the line number you want stop at, then start the debugger by clicking on the "Debug/Continue" icon.


[[File:CodeBlocks-StartDebugging.jpg]]
[[File:CodeBlocks-StartDebugging.jpg]]

Latest revision as of 18:27, 28 April 2023

Warning: This page is designed to be used with a 'release' URL parameter.

This page is using the default release RELEASE_DUNFELL_V1.7_DART-MX8M-MINI.
To view this page for a specific Variscite SoM and software release, please follow these steps:

  1. Visit variwiki.com
  2. Select your SoM
  3. Select the software release


DART-MX8M-MINI - Yocto Programming with Code::Blocks

Code::Blocks is a free C/C++ IDE built around a plugin framework, designed to be very extensible and fully configurable.

This guide demonstrates how to create and debug a C++ application using Code::Blocks on the DART-MX8M-MINI.


Create your rootfs with Code::Blocks debug support

Debugging with Code::Blocks requires your preferred SSH server (openssh, dropbear, etc.), gdb, and gdbserver installed on the target device.
Append the following to the conf/local.conf file in your Yocto build directory:

EXTRA_IMAGE_FEATURES = " \
    tools-debug \
    ssh-server-dropbear \
    "

Now bitbake your image.

Setup Host Computer Environment

Please follow the steps below to prepare a fresh Ubuntu 20.04 installation for Code::Blocks debugging:

Install Dependencies

$ sudo apt-get update
$ sudo apt-get -y install build-essential gdb gdb-multiarch git

Install Code::Blocks

$ sudo apt-get -y install codeblocks codeblocks-contrib

Install Yocto Toolchain

A toolchain is necessary for cross compiling applications. To install the toolchain, follow Variscite's Yocto Toolchain installation guide.

Configure Code::Blocks

This section uses the default toolchain path, assuming you followed the default instruction in Variscite's Yocto Toolchain installation guide.

Changes may be required if you installed the toolchain in a different path or used different distro settings.

Configure the debugger

From the menu bar, click on "Settings" and then on "Debugger..."

CodeBlock-MenuSettingsDebugger.jpg


In the "Debugger settings" window, select "GDB/CDB debugger" and click on "Create Config".

CodeBlock-DebuggerSettings.jpg


In the "Create config" window, enter the new debugger's name and click on "OK".

CodeBlock-CreateDebuggerConfig.jpg


Select the new configuration in the left pane, and fill in the "Executables path" field.

The default path is:

/opt/fslc-xwayland/3.1/sysroots/x86_64-fslcsdk-linux/usr/bin/aarch64-fslc-linux/aarch64-fslc-linux-gdb

Then, add the set sysroot command in the "Debugger initialization commands" section, to speedup the gdb startup.

The default path is:

set sysroot /opt/fslc-xwayland/3.1/sysroots/aarch64-fslc-linux

Finally, click on "OK".

CodeBlock-DebuggerExecutable.jpg

Configure the compiler

The next steps depend on the previous section: here we assume the debugger is already configured.

From the menu bar, click on "Settings" and then on "Compiler...".

CodeBlock-MenuSettingsCompiler.jpg


In the "Global compiler settings" window, click on "Copy".

CodeBlock-GlobalCompilerSettings.jpg


In the "Add new compiler" window, enter the new compiler's name and click on "OK".

CodeBlock-AddNewCompiler.jpg


A popup windows will remind you to update the "Toolchain executables" page - just click on "OK".

Click on the "Toolchain executables" tab and fill in the fields according your Yocto toolchain.

For the Debugger, in the drop-down list you should select the one created above.

The default settings are

  • Compiler's installation directory: /opt/fslc-xwayland/3.1/sysroots/x86_64-fslcsdk-linux/usr/bin/aarch64-fslc-linux
  • C compiler: aarch64-fslc-linux-gcc
  • C++ compiler: aarch64-fslc-linux-g++
  • Linker for dynamic libs: aarch64-fslc-linux-g++
  • Linker for static libs: aarch64-fslc-linux-ar
  • Debugger: GDB/CDB debugger: Variscite GDB
  • Resource compiler: <empty>
  • Make program: make

CodeBlock-ToolchainExecutables.jpg


Click on the "Compiler settings" tab, then on the "Other compiler options" tab, and add the appropriate options.

E.g. add the sysroot option:

--sysroot=/opt/fslc-xwayland/3.1/sysroots/aarch64-fslc-linux

CodeBlock-OtherCompilerOptions.jpg


Click on the "Linker settings" tab, then on the "Other linker options" tab, and add the appropriate options.

E.g. add the sysroot option:

--sysroot=/opt/fslc-xwayland/3.1/sysroots/aarch64-fslc-linux

CodeBlock-OtherLinkerOptions.jpg

Create the reference helper scripts to deploy and debug with Code::Blocks

Here, we will create the following files:

/home/user/bin/var-sdk.conf configuration file hosting target parameters
/home/user/bin/var-sdk-deploy.sh script to automate binary deploy
/home/user/bin/var-sdk-debug.sh script to automate binary debug

Create the file /home/user/bin/var-sdk.conf using the right IP address of the target board:

TARGET_DIR="/home/root"
TARGET_IP="192.168.73.165"
TARGET_PORT="3000"
SSH_OPTIONS="-oStrictHostKeyChecking=no"


Create the file /home/user/bin/var-sdk-deploy.sh:

#!/bin/bash

readonly PROGRAM="$1"
PROGRAMNAME="$(basename $PROGRAM)"

. /home/user/bin/var-sdk.conf

echo "Deploying to target"

# prevent "Host key verification failed"
ssh-keygen -f "${HOME}/.ssh/known_hosts" -R "${TARGET_IP}"

# delete old binary
ssh ${SSH_OPTIONS} root@${TARGET_IP} "sh -c 'rm -rf ${TARGET_DIR}/${PROGRAMNAME}'"

# send the program to the target
scp ${PROGRAM} root@${TARGET_IP}:${TARGET_DIR}/${PROGRAMNAME}


Create the file /home/user/bin/var-sdk-debug.sh:

#!/bin/bash

readonly PROGRAM="$1"
PROGRAMNAME="$(basename $PROGRAM)"

. /home/user/bin/var-sdk.conf

echo "Starting GDB Server on Target"

# kill gdbserver on target
ssh ${SSH_OPTIONS} root@${TARGET_IP} "sh -c '/usr/bin/killall -q gdbserver'"

# start gdbserver on target and fork
ssh ${SSH_OPTIONS} -t root@${TARGET_IP} "sh -c 'XDG_RUNTIME_DIR=/run/user/0 gdbserver localhost:${TARGET_PORT} ${TARGET_DIR}/${PROGRAMNAME}'" &


Finally, make the above .sh files executable:

$ chmod +x /home/user/bin/var-sdk*.sh

Create a sample "Console application" project with Code::Blocks

From the menu bar, click on "File", then on "New" and then on "Project...".

CodeBlocks-NewProject.jpg


In the "New from template" window, select "Console application" and click on "Go".

CodeBlocks-TemplateConsoleApplication.jpg


Unless you prefer changing something, click "Next" in the "Welcome" and "Language" windows.

CodeBlocks-ConsoleApplicationWelcome.jpg

CodeBlocks-ConsoleApplicationLanguage.jpg


Fill in the project details according to your preferences and click on "Next".

CodeBlocks-ConsoleApplicationProject.jpg


Select from the drop-down list the compiler configured in the previous section and click on "Finish".

CodeBlocks-ConsoleApplicationCompiler.jpg


A sample "Hello world" project will show up in the IDE.

CodeBlocks-ConsoleApplicationHelloWorld.jpg

Configure the project

From the previous view, right click on the project name and then click on "Properties...".

CodeBlocks-ProjectProperties.jpg


In the "Project/targets options" window, click on the "Build targets" tab, and then click on the "Build options..." button.

CodeBlocks-ProjectBuildOptions.jpg


In the "Project build options" window, ensure that "Debug" is selected in the left pane. Select the "Compiler settings" tab, then select "Use target options only" from the Policy drop-down list.

CodeBlocks-DebugPolicy.jpg


Once done, ensure that "Release" is selected in the left pane, then repeat the previous step.


In the "Project build options" window, ensure that "Debug" is selected in the left pane. Select the "Linker settings" tab, then select "Use target options only" from the Policy drop-down list.

CodeBlocks-DebugLinkerPolicy.jpg


Once done, ensure that "Release" is selected in the left pane, then repeat the previous step.


In the "Project build options" window, ensure that "Debug" is selected in the left pane, then click on the "Pre/post build steps" and add the following under "Post-build steps":

/home/user/bin/var-sdk-deploy.sh $(TARGET_OUTPUT_FILE)

CodeBlocks-PrePostBuildSteps.jpg


Once done, ensure that "Release" is selected in the left pane, then repeat the previous step, and finally, click on "OK".


In the "Project/targets options" window, scroll the tabs and click on the "Debugger" tab, then ensure "<Project>" is selected in the bottom left pane.
In the "Remote connection" tab, fill in the "IP address" and "Port" fields, with the same values of TARGET_IP and TARGET_PORT from the /home/user/bin/var-sdk.conf file.

CodeBlocks-RemoteConnection.jpg


Still in the same window, click on the "Additional shell commands" tab and add the following under "Before connection":

/home/user/bin/var-sdk-debug.sh $(TARGET_OUTPUT_FILE)

Then click on "OK".

CodeBlocks-DebugAdditionalCommands.jpg

Debug the project

As the last step of our example, set a breakpoint by clicking to the right of the line number you want stop at, then start the debugger by clicking on the "Debug/Continue" icon.

CodeBlocks-StartDebugging.jpg