Difference between revisions of "Creating a New EMAC OE SDK Project with CMake"

From wiki.emacinc.com
Jump to: navigation, search
(Made formatting changes, still working on desktop/target simultaneous build)
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{todo| Buggy (08.07.2015-12:59->KY+)(08.17.2015-12:07->KY+)(08.17.2015-14:25->KY+)(08.17.2015-17:40->MD-)|Klint Youngmeyer| project=OE 5.0,KY,MD }}
+
{{todo| Complete (08.07.2015-12:59->KY+)(08.17.2015-12:07->KY+)(08.17.2015-14:25->KY+)(08.17.2015-17:40->MD-)(11.03.2015-13:38->KY+);(11.04.2015-11:20->MD-)(11.04.2015-13:04->KY+-);(11.04.2015-13:25->MD+)(11.04.2015-14:45->MG+)|Klint Youngmeyer| project=OE 5.0,KY,MD,Complete }}
 
{{#seo:
 
{{#seo:
 
|title=Creating a New EMAC OE SDK Project with CMake
 
|title=Creating a New EMAC OE SDK Project with CMake
Line 18: Line 18:
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:geninfo | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK Project which Builds via CMake | project=OE 5.0 }}
 
{{:Templateimpl:geninfo | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK Project which Builds via CMake | project=OE 5.0 }}
Creating a new EMAC CMake project is accomplished with the use of the <code>oe_init_project</code> program. This program is included in the <code>emac-tools-projects</code> apt package, which is part of the EMAC SDK. For instructions on installing the EMAC SDK, please see [[Getting_Started_with_the_EMAC_OE_SDK#Installing_EMAC_SDK |this]] section in the getting started guide.
+
Creating a new EMAC CMake project is accomplished with the use of the <code>oe_init_project</code> program. This program is included in the [ftp://oe50opkg:opkgoe50123@ftp.emacinc.com/5.1/x86_64-nativesdk/nativesdk-emac-tools_git-r0_x86_64-nativesdk.ipk emac-tools] opkg package, which is part of the ADT EMAC SDK. For instructions on installing the EMAC SDK, please see [[Installing_EMAC_OE_5.0_SDK | SDK Install]].
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*****************************************  Using/Working With  ******************************************/ -->
 
<!-- /*****************************************  Using/Working With  ******************************************/ -->
Line 29: Line 29:
 
For the purposes of this guide, the <code>arm</code> option will be used for the listed examples.
 
For the purposes of this guide, the <code>arm</code> option will be used for the listed examples.
 
=== Host Machine Compiling ===
 
=== Host Machine Compiling ===
Navigate to the directory where the project will be located. Then run the CMake new project tool, <code>oe_init_project</code>.
+
Navigate to the directory where the project will be located. Then run the CMake new project tool, <code>/opt/emac/5.x/bin/oe_init_project</code>.
 
{{clo}}
 
{{clo}}
{{clio | username=developer | hostname=ldc | pwd=~/projects | oe_init_project -n hello.c }}
+
{{clio | username=developer | hostname=ldc | pwd=~ |cd projects }}
 +
{{clio | username=developer | hostname=ldc | pwd=~/projects | 1 = export PATH=/opt/emac/5.1/bin:$PATH}}
 +
{{clio | username=developer | hostname=ldc | pwd=~/projects |oe_init_project -n hello.c }}
 
If desired, please enter a name for this project, otherwise press Enter to use the default: hello_emac <br />
 
If desired, please enter a name for this project, otherwise press Enter to use the default: hello_emac <br />
  
Line 68: Line 70:
 
{{clos}}
 
{{clos}}
  
The executable, in this case,  is now inside the <code> hello_emac/hello_emac-build </code> directory.
+
The executable, in this case,  is now inside the <code> hello_emac/hello_emac-build </code> directory and can be executed by the host machine (X86_64).
 
 
=== Target Machine Compiling ===
 
  
 
The CMake project script has now made a project directory that contains the following:
 
The CMake project script has now made a project directory that contains the following:
* CMakeLists.txt
+
* <code>CMakeLists.txt</code>
 
* Source code file (<code>hello.c</code> in this case)
 
* Source code file (<code>hello.c</code> in this case)
 
* <code>README</code> file
 
* <code>README</code> file
 
* Desktop Build Directory (<code>hello_emac-build</code> in this case)
 
* Desktop Build Directory (<code>hello_emac-build</code> in this case)
  
Despite what the file extension implies, the <code>CMakeLists.txt</code> is actually a script which contains the required information to automatically create a <code>Makefile</code> for any supported architecture.  This file was generated by the EMAC <code>oe_init_project</code> tool, and will need to be modified over time as a project grows.  The comments in the <code>CMakeLists.txt</code> file generated by the EMAC tool will provide a good starting point for how to add additional source files and how to perform other common tasks related to maintaining your CMake build system as you develop your software project. The CMake project provides extensive documentation on how to work with these files.
+
The <code>CMakeLists.txt</code> is a script which contains the required information to automatically create a <code>Makefile</code> for any supported architecture.  This file was generated by the EMAC <code>oe_init_project</code> tool, and will need to be modified over time as a project grows.  The comments in the <code>CMakeLists.txt</code> file generated by the EMAC tool will provide a good starting point for how to add additional source files and how to perform other common tasks related to maintaining your CMake build system as you develop your software project. The [http://cmake.org CMake] project provides extensive documentation on how to work with these files.
  
The source code file generated by the script (<code>hello.c</code>) contains a basic Hello World style program.  This can provide a starting point for your application, or be replaced by another source file with the same name.
+
The source code file generated by the script (<code>hello.c</code>) contains a basic Hello World style program.  This can provide a starting point for your application, or can be replaced by another source file with the same name.
  
The <code>README</code> file contains more information on using <code>CMake</code> with the EMAC 5.0 SDK.  You may refer to this file for quick reference.
+
The <code>README</code> file contains more information on using <code>CMake</code> with the EMAC OE 5.X SDK.  You may refer to this file for quick reference.
  
 
The Desktop Build Directory (<code>hello_emac-build</code>) contains the executable, <code>hello_emac</code>, the <code>Makefile</code>, and various cache files.  These were automatically created by CMake and by the build system, and can be recreated at any time.
 
The Desktop Build Directory (<code>hello_emac-build</code>) contains the executable, <code>hello_emac</code>, the <code>Makefile</code>, and various cache files.  These were automatically created by CMake and by the build system, and can be recreated at any time.
  
It is useful to have a Desktop Build Directory because it is easier (in the beginning) to use the desktop to verify all code changes before cross-compiling for a target board. This will be useful until the application under development depends upon resources which are only available on the target hardware, such as certain devices drivers or the touchscreen (if so equipped).
+
It is useful to have a Desktop Build Directory because it is easier (in the beginning) to use the desktop to verify all code changes before cross-compiling for a target board. This will be useful until the application under development depends upon resources which are only available on the target hardware, such as certain device drivers or the touchscreen (if so equipped).
  
<!--{{warning | How to set up a project so that the user may build both a desktop and a target version simultaneously should be described, for cases where both exist.  For example, the md5 tool I'm working on now is just such a project.  The desktop tool will add a few features which won't be in the board-only tool, so the build system will have to set #defines I'll be able to use to selectively compile portions of the code. }}-->
+
=== Target Machine Compiling ===
  
 
Use the following steps to cross-compile the project and send it to a target board.
 
Use the following steps to cross-compile the project and send it to a target board.
Line 96: Line 96:
 
* Create a build directory for cross compiling.  
 
* Create a build directory for cross compiling.  
 
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac | mkdir hello_emac-build-arm }}  
 
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac | mkdir hello_emac-build-arm }}  
* Change directories into the newly created directory.
+
* Navigate into the newly created directory.
 
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac | cd hello_emac-build-arm }}  
 
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac | cd hello_emac-build-arm }}  
 
* Run <code>cmake</code> using the target architecture.
 
* Run <code>cmake</code> using the target architecture.
Line 102: Line 102:
 
* Compile the code using <code>make</code>.
 
* Compile the code using <code>make</code>.
 
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | make }}  
 
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | make }}  
 
+
The <code> make </code> command creates the target executable in the <code>hello_emac-build-arm</code> directory.
The <code> make </code> command creates the target executable in the <code> hello_emac-build-arm </code> directory.
 
 
* Now copy the executable to the target board. For information on copying the executable file, see the [[ #Transferring_Files | Transferring Files ]] section.
 
* Now copy the executable to the target board. For information on copying the executable file, see the [[ #Transferring_Files | Transferring Files ]] section.
 
</cl>
 
</cl>
<br />
+
 
For other ways to generate the CMake files, visit the [[CMake | CMake]] page.
 
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*****************************************      Examples        *****************************************/ -->
 
<!-- /*****************************************      Examples        *****************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:examples | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK project for use with CMake | project=OE 5.0 }}
 
{{:Templateimpl:examples | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK project for use with CMake | project=OE 5.0 }}
This section will illustrate how to complete various different tasks associated with building a CMake project.
+
This section illustrates how to complete various tasks associated with configuring and building a CMake project.
===Adding Include Files===
+
===Debug Build Types===
To add an include file (in this case, the <code>tools.cpp</code> and <code>tools.h</code> in the <code>include</code> directory) to the cmake project, changes will need to be made to the CMakeLists.txt file. Please refer to the code sample below.
+
By default, new projects will have the Debug build type. To compile the project with the Release or Minimum Size Release build types, there are two options:
 +
*Make a secondary (or tertiary) build directory. (Recommended)
 +
*Clean the current build directory and re-run CMake.
 +
The different levels of build type correlate to usage of different C Flags. When cross-compiling, these C Flags are located in the <code>toolchain.<architecture>.cmake</code> files located, by default, in <code>/opt/emac/5.X/</code>, with ''X'' being the specific version of SDK. The following table will give a brief overview of the different build types.
 +
 
 +
 
 +
<br />
 +
<br />
 +
<table style="border-style:double; border-width:2px; margin-top:-10%; margin-bottom:-3%;"><tr style="border-style:double; border-width:2px;"><td>'''Name:'''</td><td>'''Type:'''</td><td>'''Debugging Features:'''</td><td>'''Description:'''</td><td>'''Optimization Level:'''</td><td>'''C Flags Used:'''</td></tr>
 +
 
 +
 
 +
<tr style="border-style:solid; border-width:2px;"><td>Release</td><td>Release</td><td>disabled</td><td>Release targeted build optimized for best performance.</td><td>-O2</td><td>CMAKE_C_FLAGS_RELEASE</td></tr>
 +
 
 +
 
 +
<tr style="border-style:solid; border-width:2px;"><td>MinSizeRel</td><td>Release</td><td>disabled</td><td>Release targeted build with experimental optimizations. (Builds using this should be thoroughly tested.)</td><td>-O3</td><td>CMAKE_C_FLAGS_MINSIZEREL</td></tr>
 +
 
  
# The directory to be included must be specified by the <code>INCLUDE_DIRECTORIES()</code> function. The variable <code>${PROJECT_SOURCE_DIR}</code> refers to the base path of the project which is generally where this <code>README</code> file is located.
+
<tr style="border-style:solid; border-width:2px;"><td>Debug</td><td>Debug</td><td>enabled</td><td>A standard debugging oriented build, optimized for improved debugging.</td><td>-O0</td><td>CMAKE_C_FLAGS_DEBUG</td></tr>
# The <code>SOURCES</code> must be set. This list of files should include all of the source files in the project.
 
  
 +
</table>
 +
<br />
 +
<br />
 +
<br />
 +
If you wish to add any additional flags to a build type, they can be defined in the project's <code>CMakeList.txt</code> file as shown in the code snippet below.
 
<syntaxhighlight lang="cmake">
 
<syntaxhighlight lang="cmake">
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
+
project(example)
 +
 
 +
SET(MAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wpedantic")
  
SET(SOURCES
+
# This will add the -Wall and -Wpedantic options to the existing debug build flags
    ${PROJECT_SOURCE_DIR}/main.cpp
 
    ${PROJECT_SOURCE_DIR}/include/tools.cpp
 
    ${PROJECT_SOURCE_DIR}/include/tools.h
 
    )
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<!--{{warning | This is really useful information for any developer.  Knowing the level of optimization is insightful, so that should be included.  Knowing whether or not the build was stripped of debugging information is important if they ever intend to release binaries to the public (such as for updates) and to ensure small size and max speed.  Knowing what preprocessor defines are #def'd with a target is really useful when writing debugging code.
  
===Adding External Library Files===
 
To add an external library to the CMake project, more changes will need to be made to the <code>CMakeLists.txt</code> file. Please refer to the code sample below. In this example, the ''ImageMagick'' library will be linked into the project.
 
#The library directory must be included using the <code>INCLUDE_DIRECTORIES()</code> function. This function must be before the <code>ADD_EXECUTABLE()</code> function.
 
#Use the <code>TARGET_LINK_LIBRARIES()</code> function to link the desired library to the target binary. This function must be after the <code>ADD_EXECUTABLE()</code> function.
 
  
<syntaxhighlight lang="cmake">
+
I would put all this in a table like the example I made above (but accurate and complete).  The border-radius and shadows will need to be overridden to make it look right.  Can always use css or a template for this.
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} "/usr/include/ImageMagick")
+
}}-->
  
ADD_EXECUTABLE(main main.cpp) # This should be here by default
 
  
TARGET_LINK_LIBRARIES(main MagickWand)
+
====Method 1: Create separate build directory====
</syntaxhighlight>
+
To create a Release build using the 1st (recommended) method, use the following steps. This method is recommended because it allows both the Debug and the Release executable to exist at the same time.
 +
<cl>
 +
1. In a terminal, navigate to the base directory of the project.
 +
* Create a new release build directory
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac | mkdir hello_emac-build-arm-release }}
 +
* Change directories into the newly created directory.
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac | cd hello_emac-build-arm-release }}
 +
* Run <code>cmake</code> using the target architecture.
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | 1 = cmake .. -DARCH:STRING=arm -DCMAKE_BUILD_TYPE:STRING=Release }}{{note | The string 'MinSizeRel' can be used in place of 'Release' for a potentially smaller executable}}
 +
* Compile the code using <code>make</code>.
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | make }}
 +
</cl>
 +
<br />
 +
====Method 2: Re-use build directory====
 +
Another option for creating a release build is as follows:
 +
<cl>
 +
1. In a terminal, navigate to the build directory of the project.
 +
* Clean the directory with <code>make</code>
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | 1 = make clean}}
 +
* Run <code>cmake</code> using the target architecture.
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | 1 = cmake .. -DARCH:STRING=arm -DCMAKE_BUILD_TYPE:STRING=Release }}{{note | The string 'MinSizeRel' can be used in place of 'Release' for a potentially smaller executable}}
 +
* Compile the code using <code>make</code>.
 +
{{cli | username=developer | hostname=ldc | pwd=~/projects/hello_emac/hello_emac-build-arm | make }}
 +
</cl>
  
 
===Renaming the Target Executable===
 
===Renaming the Target Executable===
Line 154: Line 188:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Adding Additional C/C++ Sourrce Files as Libraries===
+
===Adding Include Files===
Adding additional source files is accomplished similarly to adding external library files. See [[#Adding_External_Library_Files | above]].
+
To add an include file and implementation file (in this case, the <code>tools.cpp</code> and <code>tools.h</code> in the <code>util</code> and <code>include</code> directory, respectively) to the CMake project, changes will need to be made to the CMakeLists.txt file. Please refer to the code sample below.
# All included files must be included in the <code>SOURCES</code> list.
+
 
# All header files must be included in the <code>HEADER_FILES</code> list.
+
# The directory to be included must be specified by the <code>INCLUDE_DIRECTORIES()</code> function. The variable <code>${PROJECT_SOURCE_DIR}</code> refers to the base path of the project which is generally where this <code>README</code> file is located.
# Each C/C++ source file must be added as a library before adding the executable.
+
# The <code>SOURCES</code> must be set. This list of files should include all of the source files in the project.
# Add the executable, including the <code>${HEADER_FILES}</code>.
 
# Link the target libraries to the executable.
 
  
 
<syntaxhighlight lang="cmake">
 
<syntaxhighlight lang="cmake">
SET(SOURCES
+
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}include)
    ${PROJECT_SOURCE_DIR}/include/tools.cpp
 
    ${PROJECT_SOURCE_DIR}/include/funcs.cpp
 
    ${PROJECT_SOURCE_DIR}/include/tools.h
 
    ${PROJECT_SOURCE_DIR}/include/funcs.h
 
    )
 
  
 +
SET(SOURCES
 +
    ${PROJECT_SOURCE_DIR}main.cpp
 +
    ${PROJECT_SOURCE_DIR}util/tools.cpp
 +
    )
 +
SET(HEADERS
 +
    ${PROJECT_SOURCE_DIR}include/tools.h
 +
    )
  
SET(HEADER_FILES
+
ADD_EXECUTABLE(main ${SOURCES} ${HEADERS})
    ${PROJECT_SOURCE_DIR}/include/tools.h
 
    ${PROJECT_SOURCE_DIR}/include/funcs.h
 
    )
 
 
 
 
 
ADD_LIBRARY(tools include/tools.cpp ${HEADER_FILES})
 
ADD_LIBRARY(funcs include/funcs.cpp ${HEADER_FILES})
 
ADD_EXECUTABLE(main main.cpp ${HEADER_FILES})
 
 
 
 
 
TARGET_LINK_LIBRARIES(main tools funcs)
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 188: Line 211:
 
<!-- /******************************************  More Information  *****************************************/ -->
 
<!-- /******************************************  More Information  *****************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
+
{{:Templateimpl:moreinfo | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK project for use with CMake | project=OE 5.0 }}
{{:Templateimpl:whatnext | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK project for use with CMake | project=OE 5.0 }}
+
<!--{{:Templateimpl:whatnext | initials=KY | title=Creating a New EMAC OE SDK Project with CMake | desc=Creating an EMAC OE SDK project for use with CMake | project=OE 5.0 }}-->
 
* [[System_Log_In | System Log In]]
 
* [[System_Log_In | System Log In]]
 
* [[Serial_Connections | Serial Connections ]]
 
* [[Serial_Connections | Serial Connections ]]
Line 195: Line 218:
 
* [[System_Log_In | System Log In ]]
 
* [[System_Log_In | System Log In ]]
 
* [[Remote_Debugging_EMAC_OE_SDK_Projects_with_gdbserver | Remote Debugging EMAC OE SDK Projects with gdbserver ]]
 
* [[Remote_Debugging_EMAC_OE_SDK_Projects_with_gdbserver | Remote Debugging EMAC OE SDK Projects with gdbserver ]]
* [[CMake | CMake ]]
 
 
* [[System_Logging | System Logging ]]
 
* [[System_Logging | System Logging ]]

Latest revision as of 15:46, 11 February 2019

TODO: {{#todo: Complete (08.07.2015-12:59->KY+)(08.17.2015-12:07->KY+)(08.17.2015-14:25->KY+)(08.17.2015-17:40->MD-)(11.03.2015-13:38->KY+);(11.04.2015-11:20->MD-)(11.04.2015-13:04->KY+-);(11.04.2015-13:25->MD+)(11.04.2015-14:45->MG+)|Klint Youngmeyer|OE 5.0,KY,MD,Complete}}

The EMAC OE SDK is distributed with the intent that CMake be used with the EMAC OE toolchain and libraries to build most projects. This page demonstrates the process of creating and compiling a new project with CMake, and running it on the target machine.

General Information

Creating a new EMAC CMake project is accomplished with the use of the oe_init_project program. This program is included in the emac-tools opkg package, which is part of the ADT EMAC SDK. For instructions on installing the EMAC SDK, please see SDK Install.

Creating a New EMAC OE SDK Project with CMake

This section demonstrates how to use the EMAC CMake tool, oe_init_project, to generate CMake files automatically for a project. When using the EMAC SDK there are currently two options for cross compiling:

  • arm
  • x86

For the purposes of this guide, the arm option will be used for the listed examples.

Host Machine Compiling

Navigate to the directory where the project will be located. Then run the CMake new project tool, /opt/emac/5.x/bin/oe_init_project.

developer@ldc:~# cd projects
developer@ldc:~/projects# export PATH=/opt/emac/5.1/bin:$PATH
developer@ldc:~/projects# oe_init_project -n hello.c

If desired, please enter a name for this project, otherwise press Enter to use the default: hello_emac

-- Creating new project directory...
-- Creating new source file...
-- Building custom CMakeLists.txt file...
-- Done.

Do you want to create a build directory for this project? (y/n) y

-- Creating build directory...

Do you want to run cmake for this project? (y/n) y

-- Using system compiler
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/developer/projects/hello_emac/hello_emac-build

Do you want to compile this project? (y/n) y

Scanning dependencies of target hello_emac
[100%] Building C object CMakeFiles/hello_emac.dir/hello.c.o
Linking C executable hello_emac
[100%] Built target hello_emac

The executable, in this case, is now inside the hello_emac/hello_emac-build directory and can be executed by the host machine (X86_64).

The CMake project script has now made a project directory that contains the following:

  • CMakeLists.txt
  • Source code file (hello.c in this case)
  • README file
  • Desktop Build Directory (hello_emac-build in this case)

The CMakeLists.txt is a script which contains the required information to automatically create a Makefile for any supported architecture. This file was generated by the EMAC oe_init_project tool, and will need to be modified over time as a project grows. The comments in the CMakeLists.txt file generated by the EMAC tool will provide a good starting point for how to add additional source files and how to perform other common tasks related to maintaining your CMake build system as you develop your software project. The CMake project provides extensive documentation on how to work with these files.

The source code file generated by the script (hello.c) contains a basic Hello World style program. This can provide a starting point for your application, or can be replaced by another source file with the same name.

The README file contains more information on using CMake with the EMAC OE 5.X SDK. You may refer to this file for quick reference.

The Desktop Build Directory (hello_emac-build) contains the executable, hello_emac, the Makefile, and various cache files. These were automatically created by CMake and by the build system, and can be recreated at any time.

It is useful to have a Desktop Build Directory because it is easier (in the beginning) to use the desktop to verify all code changes before cross-compiling for a target board. This will be useful until the application under development depends upon resources which are only available on the target hardware, such as certain device drivers or the touchscreen (if so equipped).

Target Machine Compiling

Use the following steps to cross-compile the project and send it to a target board.

  1. In a terminal, navigate to the base directory of the project.


    NOTE
    If the architecture of the target board you're using for your appliction x86, then you will need to change all occurrences of arm in the following sections below to x86.
  2. Create a build directory for cross compiling.

    developer@ldc:~/projects/hello_emac# mkdir hello_emac-build-arm
  3. Navigate into the newly created directory.

    developer@ldc:~/projects/hello_emac# cd hello_emac-build-arm
  4. Run cmake using the target architecture.

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# cmake .. -DARCH:STRING=arm
  5. Compile the code using make.

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# make

    The make command creates the target executable in the hello_emac-build-arm directory.

  6. Now copy the executable to the target board. For information on copying the executable file, see the Transferring Files section.

Examples

This section illustrates how to complete various tasks associated with configuring and building a CMake project.

Debug Build Types

By default, new projects will have the Debug build type. To compile the project with the Release or Minimum Size Release build types, there are two options:

  • Make a secondary (or tertiary) build directory. (Recommended)
  • Clean the current build directory and re-run CMake.

The different levels of build type correlate to usage of different C Flags. When cross-compiling, these C Flags are located in the toolchain.<architecture>.cmake files located, by default, in /opt/emac/5.X/, with X being the specific version of SDK. The following table will give a brief overview of the different build types.







Name:Type:Debugging Features:Description:Optimization Level:C Flags Used:
ReleaseReleasedisabledRelease targeted build optimized for best performance.-O2CMAKE_C_FLAGS_RELEASE
MinSizeRelReleasedisabledRelease targeted build with experimental optimizations. (Builds using this should be thoroughly tested.)-O3CMAKE_C_FLAGS_MINSIZEREL
DebugDebugenabledA standard debugging oriented build, optimized for improved debugging.-O0CMAKE_C_FLAGS_DEBUG




If you wish to add any additional flags to a build type, they can be defined in the project's CMakeList.txt file as shown in the code snippet below.

project(example)

SET(MAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wpedantic")

# This will add the -Wall and -Wpedantic options to the existing debug build flags


Method 1: Create separate build directory

To create a Release build using the 1st (recommended) method, use the following steps. This method is recommended because it allows both the Debug and the Release executable to exist at the same time.

  1. In a terminal, navigate to the base directory of the project.

  2. Create a new release build directory

    developer@ldc:~/projects/hello_emac# mkdir hello_emac-build-arm-release
  3. Change directories into the newly created directory.

    developer@ldc:~/projects/hello_emac# cd hello_emac-build-arm-release
  4. Run cmake using the target architecture.

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# cmake .. -DARCH:STRING=arm -DCMAKE_BUILD_TYPE:STRING=Release


    NOTE
    The string 'MinSizeRel' can be used in place of 'Release' for a potentially smaller executable
  5. Compile the code using make.

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# make


Method 2: Re-use build directory

Another option for creating a release build is as follows:

  1. In a terminal, navigate to the build directory of the project.

  2. Clean the directory with make

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# make clean
  3. Run cmake using the target architecture.

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# cmake .. -DARCH:STRING=arm -DCMAKE_BUILD_TYPE:STRING=Release


    NOTE
    The string 'MinSizeRel' can be used in place of 'Release' for a potentially smaller executable
  4. Compile the code using make.

    developer@ldc:~/projects/hello_emac/hello_emac-build-arm# make

Renaming the Target Executable

To rename the executable, the ADD_EXECUTABLE() function arguments must be changed. The first argument refers to the name of the executable, and the second refers to the source code file.

To change the name of the executable from "main" to "new_name", change the ADD_EXECUTABLE() from the first code box to the second code box.

ADD_EXECUTABLE(main main.cpp)
ADD_EXECUTABLE(new_name main.cpp)

Adding Include Files

To add an include file and implementation file (in this case, the tools.cpp and tools.h in the util and include directory, respectively) to the CMake project, changes will need to be made to the CMakeLists.txt file. Please refer to the code sample below.

  1. The directory to be included must be specified by the INCLUDE_DIRECTORIES() function. The variable ${PROJECT_SOURCE_DIR} refers to the base path of the project which is generally where this README file is located.
  2. The SOURCES must be set. This list of files should include all of the source files in the project.
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}include)

SET(SOURCES
     ${PROJECT_SOURCE_DIR}main.cpp
     ${PROJECT_SOURCE_DIR}util/tools.cpp
     )
SET(HEADERS
     ${PROJECT_SOURCE_DIR}include/tools.h
     )

ADD_EXECUTABLE(main ${SOURCES} ${HEADERS})

Further Information

Where to Go Next