Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Nächste Überarbeitung
Vorhergehende Überarbeitung
software:linux:yocto:getting_started_cpp [2020-03-25 14:44]
Lammerich Moritz angelegt
software:linux:yocto:getting_started_cpp [2020-03-31 11:48] (aktuell)
Lammerich Moritz
Zeile 26: Zeile 26:
 ==== Setting the machine ==== ==== Setting the machine ====
  
-Setting the machine tells bitbake what hardware to build for. This build will be a generic x86_64 ​image, so we can set the machine to ''​genericx86-64''​.+Setting the machine tells bitbake what hardware to build for. This build will be a 64bit qemu image, so we can set the machine to ''​qemu86-64''​. This means we can run this image in Qemu.
  
 Open ''​local.conf''​ Open ''​local.conf''​
Zeile 33: Zeile 33:
 </​code>​ </​code>​
  
-and add this line to set the machhine:+and add this line to set the machine:
 <​code>​ <​code>​
-MACHINE = "genericx86-64"+MACHINE = "qemux86-64"
 </​code>​ </​code>​
  
Zeile 47: Zeile 47:
 bitbake core-image-minimal bitbake core-image-minimal
 </​code>​ </​code>​
 +
 +Once the image is built, it can be run in qemu using
 +
 +<​code>​
 +runqemu qemux86-64 nographic
 +</​code>​
 +
 +''​runqemu''​ is a script that comes with bitbake, ''​qemux86-64''​ specifies the machine, and ''​nographic''​ tells qemu to run in the terminal rather than its own window. This is useful when building on a system with no graphical environment such as a container or when building in the cloud.
 +
 +The default login is ''​root''​ with no password. You can use ''​poweroff''​ to shut down the virtual machine and exit qemu.
 +
 +===== Creating a Hello World C++ Application =====
 +
 +This section will show you how to create a hello world application in C++ with Cmake.
 +
 +First, create a directory in your yocto folder. This will contain the application itself.
 +The location is arbitrary, and was chosen merely for convenience. It could be anywhere.
 +Assuming you are still in the ''​build''​ directory:
 +
 +<​code>​
 +mkdir ../​helloworld
 +</​code>​
 +
 +Next, create a ''​main.cpp''​ in the new directory
 +
 +<​code>​
 +cd ../​helloworld
 +nano helloworld.cpp
 +</​code>​
 +
 +with the following content:
 +
 +<​code>​
 +#​include<​iostream>​
 +
 +int main()
 +{
 +        std::cout << "Hello World!\n";​
 +        return 0;
 +}
 +</​code>​
 +
 +Now create a ''​CMakeLists.txt''​ for cmake:
 +
 +<​code>​
 +nano CMakeLists.txt
 +</​code>​
 +
 +CMakeLists.txt
 +<​code>​
 +cmake_minimum_required(VERSION 3.5)
 +
 +project(yocto-test LANGUAGES CXX)
 +
 +add_executable(hello helloworld.cpp)
 +
 +install(TARGETS hello DESTINATION bin)
 +
 +</​code>​
 +
 +==== Optional: Verifying the application works ====
 +
 +This is an optional step to verify the application itself is built correctly before adding it to the image.
 +Create a ''​build''​ directory inside the application folder for cmake to store its data in.
 +Then execute ''​cmake''​ inside that folder to create the build configuration and build the 
 +applucation by running ''​make''​.
 +Now there should be a ''​hello''​ executable that prints "hello world" when executed.
 +After confirming it works, change back to ''​yocto/​build''​ before continuing with the next step.
 +
 +<​code>​
 +mkdir build
 +cd build
 +cmake ..
 +make
 +./hello
 +cd ../../build
 +</​code>​
 +
 +===== Creating a Recipe for the application =====
 +
 +Bitbake povides a tool to create [[ .:recipes | recipes ]] with relative ease. This tool is called ''​devtool''​.
 +To add a [[ .:recipes | recipe ]] for our ''​hello''​ application,​ run:
 +<​code>​
 +devtool add ../​helloworld/​
 +</​code>​
 +
 +This should finish with a message similar to this one:
 +<​code>​
 +INFO: Recipe /​home/​ubuntu/​yocto/​build/​workspace/​recipes/​helloworld/​helloworld.bb has been automatically created; further editing may be required to make it fully functional
 +</​code>​
 +
 +This means devtool ran successfully and created a [[ .:recipes | recipe ]] at the specfied location.
 +
 +To test whether this [[ .:recipes | recipe ]] works, build it by running bitbake:
 +
 +<​code>​
 +bitbake helloworld
 +</​code>​
 +
 +This should run without errors and print something like this when it's finished:
 +
 +<​code>​
 +NOTE: Tasks Summary: Attempted 506 tasks of which 497 didn't need to be rerun and all succeeded.
 +</​code>​
 +
 +This means the package provided by the [[ .:recipes | recipe ]] was built successfully.
 +
 +===== Adding the Recipe to the Image =====
 +
 +Now that we have a recipe, we need to add it to the image to test it out.
 +Adding packages to recipes is done in ''​conf/​local.conf''​ by appending them to ''​CORE_IMAGE_EXTRA_INSTALL'':​
 +
 +Edit ''​local.conf''​
 +<​code>​
 +nano conf/​local.conf
 +</​code>​
 +
 +and add the following:
 +<​code>​
 +CORE_IMAGE_EXTRA_INSTALL += "​helloworld"​
 +</​code>​
 +
 +Rebuild the image
 +<​code>​
 +bitbake core-image-minimal
 +</​code>​
 +
 +And run qemu
 +
 +<​code>​
 +runqemu qemux86-64 nographic
 +</​code>​
 +
 +Log in as ''​root''​ and you can now run our ''​hello''​ application:​
 +<​code>​
 +root@qemux86-64:​~#​ hello
 +Hello World!
 +</​code>​
 +
 +Congratulations! You have now developed your own C++ application and added it to a yocto image.