Skip to content

Using CMake in NECTO Studio

CMake is a powerful build system generator used to manage the build process in NECTO Studio. Here's how to use CMake effectively:

Understanding CMakeLists.txt

  • The CMakeLists.txt file is the core of CMake-based projects.
  • It contains commands and instructions for defining targets, specifying source files, setting compiler options, and linking libraries.

Creating CMakeLists.txt

  • When creating a new project, NECTO Studio will automatically generate a basic CMakeLists.txt file.
  • Customize this file according to your project's needs, adding source files, include directories, and library dependencies.

Configuring CMake Project

  • Open the CMakeLists.txt file in the code editor to make changes.
  • Define targets using commands such as add_executable() or add_library().
  • Specify source files, include directories, and compiler options using commands like target_sources(), target_include_directories(), and target_compile_options().

Building CMake Project

  • To build a CMake project, click on the Build button or use the Project menu.
  • NECTO Studio will run CMake to generate the necessary build files and then compile the project.

Troubleshooting CMake Issues

  • If there are issues during the CMake configuration or build process, check the Output Console (which has three cards: Issues, Compile Output and Application Output) for error messages.
  • Review the CMakeLists.txt file for syntax errors or incorrect commands.

CMake Code Snippet Example

The following content from CMakeLists.txt file is a template for creating an embedded project in NECTO Studio using the mikroSDK 2.0 framework.

It sets up the project to use either MikroC or standard C and Assembly languages, includes necessary core or system libraries, and configures the linker with appropriate scripts and options.

This setup ensures that the project is properly configured for building and deploying to an embedded target.

Here is an example of a simple CMakeLists.txt file for a basic Application type of NECTO Studio projects:

cmake_minimum_required(VERSION 3.19)

if (${TOOLCHAIN_LANGUAGE} MATCHES "MikroC")
project(ExampleProject LANGUAGES MikroC)
else()
project(ExampleProject LANGUAGES C ASM)
endif()
set(PROJECT_TYPE "mikrosdk" CACHE STRING "" FORCE)
add_executable(ExampleProject
    main.c
)

############################ ExampleProject GENERATED CODE START ###########################**
############################ ExampleProject GENERATED CODE END ###########################**

find_package(MikroC.Core REQUIRED)

target_link_libraries(ExampleProject PUBLIC
    MikroC.Core
)

if (${MIKROSDK_TYPE} STREQUAL "legacy")
find_package(MikroC.System REQUIRED)
target_link_libraries(ExampleProject PUBLIC
    MikroC.System
)
endif()

target_sources(ExampleProject PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/.meproject/setup/FileLinker/stm32f407zg.ld ${CMAKE_CURRENT_SOURCE_DIR}/.meproject/setup/FileStartup/stm32f407zg.s)

target_link_options(ExampleProject PUBLIC -T ${CMAKE_CURRENT_SOURCE_DIR}/.meproject/setup/FileLinker/stm32f407zg.ld)
  • CMake Code Snippet - explanation

  • cmake_minimum_required(VERSION 3.19)

    • This line sets the minimum required version of CMake to 3.1It ensures that the project is built using this version or a newer one.
  • if (${TOOLCHAIN_LANGUAGE} MATCHES "MikroC")

    • This conditional checks if the toolchain language is set to "MikroC". Depending on the result, it sets up the project accordingly.
  • project(ExampleProject LANGUAGES MikroC)

    • If the toolchain language is MikroC, it sets up the project to use the MikroC language.
  • project(ExampleProject LANGUAGES C ASM)

    • If the toolchain language is not MikroC, it sets up the project to use C and Assembly languages.
  • set(PROJECT_TYPE "mikrosdk" CACHE STRING "" FORCE)

    • This line sets a CMake variable PROJECT_TYPE to "mikrosdk". The CACHE STRING "" FORCE part ensures that this variable is stored in the cache and can be accessed later, with the force option to override any previous value.
  • add_executable(ExampleProject main.c)

    • This command creates an executable target named ExampleProject and specifies main.c as the source file for this target.
  • find_package(MikroC.Core REQUIRED)

    • This line searches for the MikroC.Core package and ensures it is available. The REQUIRED keyword makes it mandatory, so the build process will fail if this package is not found.
  • target_link_libraries(ExampleProject PUBLIC MikroC.Core)

    • This command links the MikroC.Core library to the ExampleProject target, making its functions available to the project.
  • if (${MIKROSDK_TYPE} STREQUAL "legacy")

    • This conditional checks if the MIKROSDK_TYPE is set to "legacy". If true, additional steps are taken to include legacy components.
  • find_package(MikroC.System REQUIRED)

    • If the MIKROSDK_TYPE is "legacy", this line ensures that the MikroC.System package is found and included.
  • target_link_libraries(ExampleProject PUBLIC MikroC.System)

    • Links the MikroC.System library to the ExampleProject target, providing additional system-level functionalities.
  • target_sources(ExampleProject PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/.meproject/setup/FileLinker/stm32f407zg.ld ${CMAKE_CURRENT_SOURCE_DIR}/.meproject/setup/FileStartup/stm32f407zg.s)

    • This command specifies additional source files for the ExampleProject target. Here, it includes the linker script (stm32f407zg.ld) and the startup file (stm32f407zg.s).
  • target_link_options(ExampleProject PUBLIC -T ${CMAKE_CURRENT_SOURCE_DIR}/.meproject/setup/FileLinker/stm32f407zg.ld)

    • This command adds linker options to the ExampleProject target, specifically telling the linker to use the specified linker script (-T option).