Creating a New EMAC OE SDK Project with CMake
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-projects
apt package, which is part of the EMAC SDK. For instructions on installing the EMAC SDK, please see this section in the getting started guide.
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, oe_init_project
.
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.
Target Machine Compiling
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)
Despite what the file extension implies, the CMakeLists.txt
is actually 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 be replaced by another source file with the same name.
The README
file contains more information on using CMake
with the EMAC 5.0 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 devices drivers or the touchscreen (if so equipped).
Use the following steps to cross-compile the project and send it to a target board.
-
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 ofarm
in the following sections below tox86
. -
Create a build directory for cross compiling.
developer
@
ldc
:
~/projects/hello_emac
#
mkdir hello_emac-build-arm
-
Change directories into the newly created directory.
developer
@
ldc
:
~/projects/hello_emac
#
cd hello_emac-build-arm
-
Run
cmake
using the target architecture.developer
@
ldc
:
~/projects/hello_emac/hello_emac-build-arm
#
cmake .. -DARCH:STRING=arm
-
Compile the code using
make
.developer
@
ldc
:
~/projects/hello_emac/hello_emac-build-arm
#
make
The
make
command creates the target executable in thehello_emac-build-arm
directory. -
Now copy the executable to the target board. For information on copying the executable file, see the Transferring Files section.
For other ways to generate the CMake files, visit the CMake page.
Examples
This section will illustrate how to complete various different tasks associated with building a CMake project.
Adding Include Files
To add an include file (in this case, the tools.cpp
and tools.h
in the include
directory) to the cmake project, changes will need to be made to the CMakeLists.txt file. Please refer to the code sample below.
- 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 thisREADME
file is located. - 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}/include/tools.cpp
${PROJECT_SOURCE_DIR}/include/tools.h
)
Adding External Library Files
To add an external library to the CMake project, more changes will need to be made to the CMakeLists.txt
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
INCLUDE_DIRECTORIES()
function. This function must be before theADD_EXECUTABLE()
function. - Use the
TARGET_LINK_LIBRARIES()
function to link the desired library to the target binary. This function must be after theADD_EXECUTABLE()
function.
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)
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 Additional C/C++ Sourrce Files as Libraries
Adding additional source files is accomplished similarly to adding external library files. See above.
- All included files must be included in the
SOURCES
list. - All header files must be included in the
HEADER_FILES
list. - Each C/C++ source file must be added as a library before adding the executable.
- Add the executable, including the
${HEADER_FILES}
. - Link the target libraries to the executable.
SET(SOURCES
${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(HEADER_FILES
${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)