CMake

How to build debug in CMake

If you want to build an executable / library with debug symbols in CMake, run

cmake -DCMAKE_BUILD_TYPE=Debug .
make

Conversely, if you want to build an executable / library in release mode, run

cmake -DCMAKE_BUILD_TYPE=Release .
make

 

Posted by Uli Köhler in C/C++, CMake

How to change CMake object file suffix from default ‘.o’

In order to configure CMake to use an alternate object file suffix (default: .o on Linux) use these lines in your CMakeLists.txt:

set(CMAKE_C_OUTPUT_EXTENSION ".rel")
set(CMAKE_CXX_OUTPUT_EXTENSION ".rel")

This example changes the output extension from .o to .rel (which is required for the SDCC compiler). Be sure to replace ".rel" by your desired output suffix.

Note that in order for these to take effect, you might need to completely remove CMakeCache.txt, CMakeFiles & cmake_install.cmake:

rm -rf CMakeCache.txt CMakeFiles cmake_install.cmake

 

Posted by Uli Köhler in C/C++, CMake

A working SDCC STM8 CMake configuration

If you have been looking desperately for a working CMake example for the SDCC compiler for STM8 microcontrollers here’s my take on it:

cmake_minimum_required(VERSION 3.2)

set(CMAKE_C_OUTPUT_EXTENSION ".rel")
set(CMAKE_C_COMPILER sdcc)
set(CMAKE_SYSTEM_NAME Generic) # No linux target etc

# Prevent default configuration
set(CMAKE_C_FLAGS_INIT "")
set(CMAKE_EXE_LINKER_FLAGS_INIT "")

project(STM8Blink C)
SET(CMAKE_C_FLAGS "-mstm8 --std-c99")
add_executable(main.ihx main.c)

# Flash targets
add_custom_target(flash ALL COMMAND stm8flash -c stlink -p stm8s105c6 -w main.ihx)

This will build main.ihx from main.c. main.ihx is a Intel Hex file which can be directly flashed using stm8flash.

The last lines setup make flash ; you might need to use the correct microcontroller (stm8s105c6 in this example, run stm8flash -l to show supported devices) and the correct flash adapter (stlink, stlinkv2, stlinkv21, stlinkv3 or espstlink).

The setup example shown here is for the STM8S eval board.

I suppose it can be easily modified for other microcontrollers, but I haven’t tried that so far.

Posted by Uli Köhler in CMake, Embedded, Hardware

How to fix CMake ‘make: *** No targets specified and no makefile found. Stop.’

Problem:

You are trying to build a software that is using the CMake build system.

You are trying to run make to build, but you see this error message:

make: *** No targets specified and no makefile found.  Stop.

Solution:

Before running make, you need to configure your build using CMake.

The simplest way of doing that is to run

cmake .

Typically you only need to do that once for each project ; CMake will automatically detect changes to CMakeLists.txt when you run make.

After that, you can run make again. If the build is successful, you’ll see a message like this:

[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
[100%] Built target main
Posted by Uli Köhler in CMake

How to fix CMake Parse error: Expected a command name, got unquoted argument with text "//"

Problem:

You want to build an application using CMake but you see an error message like this:

CMake Error at CMakeLists.txt:1:
  Parse error.  Expected a command name, got unquoted argument with text
  "//".


-- Configuring incomplete, errors occurred!
See also "/ram/CMakeFiles/CMakeOutput.log".
Makefile:176: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1

Solution:

You CMakeLists.txt likely looks like this:

// Create main executable
add_executable(main main.cpp)

The issue is in the first line:

// Create main executable

CMake comments start with #, not with // !

Change any comment line starting with // to start with # instead. In our example:

# Create main executable
add_executable(main main.cpp)

Then try building again (e.g. using cmake . or make). Unless you have other issues in your CMake configuration, the output should now look like this (for simple builds):

-- Configuring done
-- Generating done
-- Build files have been written to: /ram
[100%] Built target main

 

Posted by Uli Köhler in CMake

Minimal CMakeLists.txt for executables

This is the minimal CMakeLists.txt for building an executable named myproject from main.cpp:

cmake_minimum_required (VERSION 2.8.11)
project (MyProject)
add_executable (myproject main.cpp)

 

Posted by Uli Köhler in CMake