Creating a New EMAC OE SDK Project with CMake

From wiki.emacinc.com
Revision as of 11:07, 17 August 2015 by Kyoungmeyer (talk | contribs)
Jump to: navigation, search
TODO: {{#todo: Review (08.07.2015-12:59->KY+)(08.17.2015-12:07->KY+)|Klint Youngmeyer|OE 5.0,KY}}

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 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 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 script.

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)

The CMakeLists.txt contains the required information to automatically create a Makefile for a given architecture. This was created by the EMAC oe_init_project script, and will need to be modified over time as a project grows. The comments in the 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 your project grows. 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.

The README file contains more information on using CMake with the EMAC 5.0 SDK.

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.

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



    NOTE
    If the target board being used is x86, then 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. Change directories 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.



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.

  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, generally where this README 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}/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.

  1. The library directory must be included using the INCLUDE_DIRECTORIES() function. This function must be before the ADD_EXECUTABLE() function.
  2. Use the TARGET_LINK_LIBRARIES() function to link the desired library to the target binary. This function must be after the ADD_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 ode box.

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

Adding Additional C/C++ Sourrce Files as Libraries

Adding additional source file is accomplished similarly to adding external library files. See above.

  1. All included files must be included in the SOURCES list.
  2. All header files must be included in the HEADER_FILES list.
  3. Each C/C++ source file must be added as a library before adding the executable.
  4. Add the executable, including the ${HEADER_FILES}.
  5. 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)


Pages with Related Content