This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Need assistance setting up CMakeLists.txt

My application is composed by three different source code repositories, organized as follows:

  +--- app

          L---- lib1

          |         L-- src

          |              L-- include

          L---- lib2

           |        L-- src

           |               L-- include

           L---- src

                   L-- include

The sub-folders "lib1" and "lib2" contain libraries that I need to build my application, which has its source under "src".

How can I create the CMakeList.txt file to build lib1 and lib2 as static libraries, then link them to my source ? 

I tried to use the example here "https://cmake.org/examples/", but the libraries are built without the Zephyr and board specific options. As

a result, when it tries to link the static libs it complains that the CPU architectures does not match.

I tried as well to use the CMakeLists.txt from the Zephyr example "samples\application_development\external_lib" but it appears to need

make, which is something I don't have on Windows.

Parents
  • I installed make on my computer, and I copied the example from Zephyr example "samples\application_development\external_lib".

    Now my external library gets build, but it fails at linking with this message:

    c:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: error: ../mbus/build/mbusStack.lib(MBUS_dataLinkLayerWired.o): conflicting CPU architectures 17/2

    I honestly cannot understand why it complains about the different CPU. This is the output of the build for my library (only 1 file, to avoid clutter):

    C:/Nordic/v1.5.0/toolchain/opt/bin/arm-none-eabi-gcc.exe src/MBUS_applicationLayer.c -c "-IC:/workspace/repos/DNG_nrf9160/zephyr/include -IC:/workspace/repos/DNG_nrf9160/app/build/zephyr/include/generated -IC:/workspace/repos/DNG_nrf9160/zephyr/soc/arm/nordic_nrf/nrf91 -IC:/workspace/repos/DNG_nrf9160/zephyr/lib/libc/minimal/include -IC:/workspace/repos/DNG_nrf9160/nrf/include -IC:/workspace/repos/DNG_nrf9160/modules/hal/cmsis/CMSIS/Core/Include -IC:/workspace/repos/DNG_nrf9160/modules/hal/nordic/nrfx -IC:/workspace/repos/DNG_nrf9160/modules/hal/nordic/nrfx/drivers/include -IC:/workspace/repos/DNG_nrf9160/modules/hal/nordic/nrfx/mdk -IC:/workspace/repos/DNG_nrf9160/zephyr/modules/hal_nordic/nrfx/. -Ic:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include -Ic:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed -DKERNEL -D__ZEPHYR__=1 -D_FORTIFY_SOURCE=2 -DBUILD_VERSION=v2.4.99-ncs1 -DFIRMWARE_INFO_MAGIC=0x281ee6de,0x8fcebb4c,23298 -DEXT_API_MAGIC=0x281ee6de,0xb845acea,23298 -DUSE_PARTITION_MANAGER=1 -D__PROGRAM_START -DNRF9160_XXAA -DNRF_TRUSTZONE_NONSECURE -Os -imacros C:/workspace/repos/DNG_nrf9160/app/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -mcpu=cortex-m33 -mthumb -mabi=aapcs -imacros C:/workspace/repos/DNG_nrf9160/zephyr/include/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wno-address-of-packed-member -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=C:/workspace/repos/DNG_nrf9160/app=CMAKE_SOURCE_DIR -fmacro-prefix-map=C:/workspace/repos/DNG_nrf9160/zephyr=ZEPHYR_BASE -fmacro-prefix-map=C:/workspace/repos/DNG_nrf9160=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -isystemC:/workspace/repos/DNG_nrf9160/zephyr/lib/libc/minimal/include -isystemc:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include -isystemc:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed" -I src/include -I src/include/public -o build/MBUS_applicationLayer.o

    (.. more files are build .. )


    C:/Nordic/v1.5.0/toolchain/opt/bin/arm-none-eabi-ar.exe -rcs build/libmbusStack.a build/MBUS_applicationLayer.o build/MBUS_dataLinkLayerWired.o build/MBUS_dataLinkLayerWireless.o

    The C source files are compiled with the correct CPU options:  -mcpu=cortex-m33 -mthumb -mabi=aapcs

    This issue does not occur for the Zephyr example, here is the output when compiling the external source:

    C:/Nordic/v1.5.0/toolchain/opt/bin/arm-none-eabi-gcc.exe -c -IC:/workspace/repos/DNG_nrf9160/zephyr/include -IC:/workspace/repos/DNG_nrf9160/zephyr/samples/application_development/external_lib/build/zephyr/include/generated -IC:/workspace/repos/DNG_nrf9160/zephyr/soc/arm/nordic_nrf/nrf91 -IC:/workspace/repos/DNG_nrf9160/zephyr/lib/libc/minimal/include -IC:/workspace/repos/DNG_nrf9160/nrf/include -IC:/workspace/repos/DNG_nrf9160/modules/hal/cmsis/CMSIS/Core/Include -IC:/workspace/repos/DNG_nrf9160/modules/hal/nordic/nrfx -IC:/workspace/repos/DNG_nrf9160/modules/hal/nordic/nrfx/drivers/include -IC:/workspace/repos/DNG_nrf9160/modules/hal/nordic/nrfx/mdk -IC:/workspace/repos/DNG_nrf9160/zephyr/modules/hal_nordic/nrfx/. -Ic:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include -Ic:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed -DKERNEL -D__ZEPHYR__=1 -D_FORTIFY_SOURCE=2 -DBUILD_VERSION=v2.4.99-ncs1 -DFIRMWARE_INFO_MAGIC=0x281ee6de,0x8fcebb4c,23298 -DEXT_API_MAGIC=0x281ee6de,0xb845acea,23298 -DUSE_PARTITION_MANAGER=1 -D__PROGRAM_START -DNRF9160_XXAA -DNRF_TRUSTZONE_NONSECURE -Os -imacros C:/workspace/repos/DNG_nrf9160/zephyr/samples/application_development/external_lib/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -mcpu=cortex-m33 -mthumb -mabi=aapcs -imacros C:/workspace/repos/DNG_nrf9160/zephyr/include/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wno-address-of-packed-member -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=C:/workspace/repos/DNG_nrf9160/zephyr/samples/application_development/external_lib=CMAKE_SOURCE_DIR -fmacro-prefix-map=C:/workspace/repos/DNG_nrf9160/zephyr=ZEPHYR_BASE -fmacro-prefix-map=C:/workspace/repos/DNG_nrf9160=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -isystemC:/workspace/repos/DNG_nrf9160/zephyr/lib/libc/minimal/include -isystemc:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include -isystemc:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed -MD -Iinclude src/mylib.c -o C:/workspace/repos/DNG_nrf9160/zephyr/samples/application_development/external_lib/build/mylib/obj/mylib.o

    The only relevant difference I see is the use of absolute paths to the source files in the Zephyr example vs relative paths in my case. Could this be the problem  ?

  • This is the CMakeLists.txt that I am using:

    cmake_minimum_required(VERSION 3.13.1)

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(app)

    # sources for bulding the application
    FILE(GLOB sources src/*.c)
    target_sources(app PRIVATE ${sources})
    target_include_directories(app PUBLIC src/include
    mbus/src/include/public
    dlms/src/include/public)

    # The M-Bus and DLMS libraries are added as external projects
    # They need the compilation options from Zephyr and the project board
    zephyr_get_include_directories_for_lang_as_string( C includes)
    zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
    zephyr_get_compile_definitions_for_lang_as_string( C definitions)
    zephyr_get_compile_options_for_lang_as_string( C options)

    set(external_project_cflags "${includes} ${definitions} ${options} ${system_includes}")

    include(ExternalProject)

    ExternalProject_Add(
    mbus_ext # name of the project
    PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/mbus # root dir for entire project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbus # where the CMakeLists.txt file is located
    BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbus # where the library is build
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/mbus/build/libmbusStack.a
    CONFIGURE_COMMAND ""
    INSTALL_COMMAND ""
    BUILD_COMMAND "make"
    nrf9160
    CC_NORDIC=${CMAKE_C_COMPILER}
    AR_NORDIC=${CMAKE_AR}
    CFLAGS_NORDIC="${external_project_cflags}"
    )

    ExternalProject_Add(
    dlms_ext # name of the project
    PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server # root dir for entire project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server # where the CMakeLists.txt file is located
    BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server # where the library is build
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server/build/libDlmsServer.a
    CONFIGURE_COMMAND ""
    INSTALL_COMMAND ""
    BUILD_COMMAND "make"
    CC=${CMAKE_C_COMPILER}
    AR=${CMAKE_AR}
    CFLAGS=-c ${external_project_cflags}
    )

    # Create a wrapper CMake library that our app can link with
    add_library(mbus_lib STATIC IMPORTED GLOBAL)
    add_library(dlms_lib STATIC IMPORTED GLOBAL)
    add_dependencies(mbus_lib mbus_ext dlms_lib dlms_ext)

    set_target_properties(mbus_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/mbus/build/libmbusStack.a)
    set_target_properties(mbus_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/mbus/src/include/public)

    set_target_properties(dlms_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server/build/libDlmsServer.a)
    set_target_properties(dlms_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server/src/include/public)

    target_link_libraries(app PUBLIC mbus_lib dlms_lib)

Reply
  • This is the CMakeLists.txt that I am using:

    cmake_minimum_required(VERSION 3.13.1)

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(app)

    # sources for bulding the application
    FILE(GLOB sources src/*.c)
    target_sources(app PRIVATE ${sources})
    target_include_directories(app PUBLIC src/include
    mbus/src/include/public
    dlms/src/include/public)

    # The M-Bus and DLMS libraries are added as external projects
    # They need the compilation options from Zephyr and the project board
    zephyr_get_include_directories_for_lang_as_string( C includes)
    zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
    zephyr_get_compile_definitions_for_lang_as_string( C definitions)
    zephyr_get_compile_options_for_lang_as_string( C options)

    set(external_project_cflags "${includes} ${definitions} ${options} ${system_includes}")

    include(ExternalProject)

    ExternalProject_Add(
    mbus_ext # name of the project
    PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/mbus # root dir for entire project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbus # where the CMakeLists.txt file is located
    BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbus # where the library is build
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/mbus/build/libmbusStack.a
    CONFIGURE_COMMAND ""
    INSTALL_COMMAND ""
    BUILD_COMMAND "make"
    nrf9160
    CC_NORDIC=${CMAKE_C_COMPILER}
    AR_NORDIC=${CMAKE_AR}
    CFLAGS_NORDIC="${external_project_cflags}"
    )

    ExternalProject_Add(
    dlms_ext # name of the project
    PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server # root dir for entire project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server # where the CMakeLists.txt file is located
    BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server # where the library is build
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server/build/libDlmsServer.a
    CONFIGURE_COMMAND ""
    INSTALL_COMMAND ""
    BUILD_COMMAND "make"
    CC=${CMAKE_C_COMPILER}
    AR=${CMAKE_AR}
    CFLAGS=-c ${external_project_cflags}
    )

    # Create a wrapper CMake library that our app can link with
    add_library(mbus_lib STATIC IMPORTED GLOBAL)
    add_library(dlms_lib STATIC IMPORTED GLOBAL)
    add_dependencies(mbus_lib mbus_ext dlms_lib dlms_ext)

    set_target_properties(mbus_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/mbus/build/libmbusStack.a)
    set_target_properties(mbus_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/mbus/src/include/public)

    set_target_properties(dlms_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server/build/libDlmsServer.a)
    set_target_properties(dlms_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/dlms_server/src/include/public)

    target_link_libraries(app PUBLIC mbus_lib dlms_lib)

Children
No Data
Related