Skip to content

CMake: Build multiple binaries from single mbed-os source #14220

Open
@nat45928

Description

@nat45928

Description of defect

Follow up from #12837

I am trying to create two binaries from a single repo using the new CMake build system but not having much sucess. I have setup a root CMakeLists.txt which creates a base library target which depends on the mbed-os build sources and adds some common project sources. In two folders I have a CMakeLists.txt which adds an executeable and additional target specific source files.

When building I either get multiple copies of mbed-os (instead of a single build follow by linking for each executable) or a get errors about multiple definitions / symbols.

I'm fairly new to CMake-based projects so this might just be user error, but I'm not sure what the right definitions are. Additionally I get the following error:

CMake Error: Files to be generated by multiple different commands: "{project}/cmake-build-debug/compile_time_defs.txt"

The project structure looks like the following:

root
 |-> mbed-os
 |-> common
 |   |-> include
 |   |-> src
 |-> foo
 |   |-> CMakeLists.txt
 |   |-> main.cpp
 |-> bar
 |   |-> CMakeLists.txt
 |   |-> main.cpp
 |-> CMakeLists.txt
 |-> mbed-lib.json

The root CMakeLists.txt

cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)

set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "")
set(MBED_CONFIG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/.mbedbuild CACHE INTERNAL "")

include(${MBED_PATH}/tools/cmake/app.cmake)
add_subdirectory(${MBED_PATH})

add_library(project-common)

project(my-project)

target_sources(
    project-common
    PRIVATE
        common/src/utility.cpp
)
target_include_directories(
    project-common
    PUBLIC
        .
        common/include
)

target_link_libraries(
    project-common
    PUBLIC
        mbed-os
        mbed-events
)

# add binary directories
add_subdirectory(foo)
add_subdirectory(bar)

And the foo/CMakeLists.txt file looks like:

add_executable(foo)

mbed_configure_app_target(foo)
mbed_set_mbed_target_linker_script(foo)

target_sources(
    foo
    PRIVATE
        main.cpp
)

target_link_libraries(
    foo
    PRIVATE
        project-common
)

mbed_set_post_build(foo)

Target(s) affected by this defect ?

Testing with an STM32 F4

Toolchain(s) (name and version) displaying this defect ?

GCC ARM

What version of Mbed-os are you using (tag or sha) ?

mbed-os-6.7.0

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

mbed-tools: 5.3.0

How is this defect reproduced ?

Explained above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions