Getting started with C/C++#

If you want to create a SimpleSwitch™ application using C/C++, please follow this guide to get you easily started.

Example Project#

Here is a very small C project, with a single C source file, test.c:

// SPDX-FileCopyrightText: (C) 2022 Avnet Embedded GmbH
// SPDX-License-Identifier: LicenseRef-Avnet-OSS-1.0
#include <stdio.h>

int main()
{
	while(1) {
		fprintf(stderr, "Test C SDK\n");
		sleep(5);
	}

	return 0;
}

And a Makefile:

# SPDX-FileCopyrightText: (C) 2022 Avnet Embedded GmbH
# SPDX-License-Identifier: LicenseRef-Avnet-OSS-1.0
BIN = c_test_prog
OBJS = test.o

all: $(BIN)

$(BIN): $(OBJS)
	$(CC) -o $@ $<

clean:
	rm -f $(BIN) $(OBJS)

install: $(BIN)
	install -m 0755 -d "$(PREFIX)/opt/$(BIN)"
	install -m 0755 $(BIN) "$(PREFIX)/opt/$(BIN)"

.PHONY: clean install

One can build this project for the host system by putting both file in the same directory and running make

$ make
cc -c -o test.o test.c
cc -o c_test_prog test.o
$ ./c_test_prog
Test C SDK

Build the project using a Makefile#

With a makefile aware of cross-compilation, building the binary is as simple as sourcing the SDK and running make, e.g.

$ . /opt/sc/0.1.0/sm2s-imx8plus/environment-setup-cortexa53-crypto-simplecoredistro-linux
$ make
aarch64-simplecore-distro-linux-gcc ... -o test.o test.c
aarch64-simplecore-distro-linux-gcc ... -o c_test_prog test.o

If the makefile has an install target using the PREFIX variable as installation path, it can be used directly with the --makefile-dir option of the Generate a SimpleSwitch™ package script

  $ simpleswitch-generate-package --name simpleswitch-example --makefile-dir . \
  --template C --startup-command /opt/c_test_prog/c_test_prog

Or if you want to be able to debug your application:

$ simpleswitch-generate-package --name simpleswitch-example --makefile-dir . \
  --template C --startup-command "while true; do gdbserver :2159 /opt/c_test_prog/c_test_prog ; done"

If your makefile lack of a compatible install target, see below.

Build the project using an external tool#

You can also build with any external tool and create a package by copying the content of a directory. All you need is this directory to reproduce the structure desired in the package

$ find deploy_dir -printf "%y %p\n"
d deploy_dir
d deploy_dir/usr
d deploy_dir/opt/c_test_prog
f deploy_dir/opt/c_test_prog/c_test_prog

You can then use the --copy-dir option of the simpleswitch-generate-package the Generate a SimpleSwitch™ package

  $ simpleswitch-generate-package --name simpleswitch-example --copy-dir deploy_dir \
 --template C --startup-command /opt/c_test_prog/c_test_prog

Or if you want to be able to debug your application:

$ simpleswitch-generate-package --name simpleswitch-example --copy-dir deploy_dir \
 --template C --startup-command "while true; do gdbserver :2159 /opt/c_test_prog/c_test_prog ; done"

Deploy to the target#

Now it is time to deploy the generated SimpleSwitch™ container to the device. For this please see Deploy a SimpleSwitch™ package

Remote debugging a C/C++ application#

To remote debugging a C or a C++ application, you can see Remote debug a SimpleSwitch™ package

Further reading#