Creating CMake Project with Amazon Freertos

Hello, I am using the ESP32 devkitc andrying to use the amazon freertos with my own applications. I ran the demo application successfully however now i want to develop my own application so I tried to use CMake and Add the amazon freertos as subdirectory as guided by Amazon document “Getting started with the Espressif ESP32-DevKitC and the ESP-WROVER-KIT”.

I use following CMake Example from here

cmake_minimum_required(VERSION 3.13)

project(my_app)

add_executable(my_app main.c)

#Tell IDF build to link against this target
set(IDF_PROJECT_EXECUTABLE my_app)

#AFR_BOARD tells which board we need to target.
set(AFR_BOARD espressif.esp32_devkitc CACHE INTERNAL “”)

add_subdirectory(amazon-freertos)

target_link_libraries(my_app PRIVATE AFR::mqtt)

but when i try to build the project it report following error:
add_executable cannot create target “my_app” because another target with
the same name already exists. The existing target is an executable created
in source directory

Please give me some directions In order to create the Project with amazon freertos.

Hi, can you share more information about the folder structure that you have? The location of the CMakeLists.txt file is relevant for setting up the amazon-freertos repository as a submodule.

You may find this previous forum post helpful:

The original question was related to a specific file, but it contains information about setting up your own custom project as well.

Hello!
I want create cmake project in following structure:
-amazon-freertos
-components
-src
- main.c
CMakeLists.txt

and the CMakeLists.txt looks like this :
cmake_minimum_required(VERSION 3.13)

project(freertos_examples)

add_executable(my_app src/main.c)
#Tell IDF build to link against this target.
set(IDF_PROJECT_EXECUTABLE my_app)

#Add FreeRTOS as a subdirectory. AFR_BOARD tells which board to target.

set(AFR_BOARD espressif.esp32_devkitc CACHE INTERNAL “”)

add_subdirectory(freertos)

#Link against the mqtt library so that we can use it. Dependencies are transitively

#linked.

target_link_libraries(my_app PRIVATE AFR::mqtt)

target_link_libraries(my_app PRIVATE AFR::freertos)

target_link_libraries(my_app PRIVATE AFR::kernel)

but the problem is in the following Cmake lines :
add_executable(my_app src/main.c)
#Tell IDF build to link against this target.
set(IDF_PROJECT_EXECUTABLE my_app)

when i use my_app in these two lines above as All the CMakeLists Example I have seen online i get the error that says : add_executable cannot create target “my_app” because another target with
the same name already exists. The existing target is an executable created
in source directory

To solve that i changed those two lines as following :
add_executable(my_app src/main.c)
#Tell IDF build to link against this target.
set(IDF_PROJECT_EXECUTABLE my_app2) #replaced my_app with my_app2

after this I can generate the build directory.
however when i run command : cmake --build build i get following errors :
FAILED: my_app
cmd.exe /C “cd . && C:\Users\Naitik.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wl,–gc-sections -Wl,–cref -Wl,–Map=aws_demos.map -Wl,–undefined=uxTopUsedPriority @CMakeFiles\my_app.rsp -o my_app && cd .”
c:/users/naitik/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: cannot open linker script file esp32.project.ld: No such file or directory
collect2.exe: error: ld returned 1 exit status
[1784/1804] Generating ld/esp32.project.ld
ninja: build stopped: subcommand failed.

can you please Help me to understand why CMakeLists.txt from examples is not working.

I went through the forum post you’ve linked but its seems like CMake file there consists microchip related directories as well I tried to remove those and edited for esp32 and specified src/main.c but that neither worked for me.

Hello,

I looked over your CMake file and the issue could be with this line:
add_subdirectory(freertos)

This is effectively searching the specified directory for a CMakeLists.txt file and then including that file if it can find one. In the folder structure that you posted, you don’t have a folder called “freertos”. Instead you have a folder called “amazon-freertos”. Can you change the name of the folder to be just “freertos”? This should make it so that the CMakeLists.txt file is properly included.

There may be other issues, but this needs to be resolved at the minimum.

Hello, Thanks for your response.
Sorry for creating misunderstanding in the actual project i have indeed renamed amazon-freertos directory to freertos that is why you see freertos in cmakelists.txt and Errors are occurring even when cmake pointing to right directory.

I apologize for the delay, I’ll see if I can recreate the issue.

@naitik I was unable to reproduce either issue locally. I’d still like to help to see if we can get your project to build. I have some questions that may help us close in on the issue.

  1. What libraries in amazon-freertos are you attempting to link with?
  2. What version of CMake are you using?
  3. Can you share your CMake file as it is? It is slightly confusing to read the file that you have posted. If you can share this in the form of a code block, that would be great.

Hello,

  1. I am trying to link following libraries for now : mqtt, freertos, kernel
  2. Currently i have installed CMake 3.20.0.
  3. CmakeLists :
cmake_minimum_required(VERSION 3.13)

project(freertos_examples)

add_executable(my_app src/app_main.c)
#Tell IDF build to link against this target.
set(IDF_PROJECT_EXECUTABLE my_app)

#Add FreeRTOS as a subdirectory. AFR_BOARD tells which board to target.

set(AFR_BOARD espressif.esp32_devkitc CACHE INTERNAL “”)

add_subdirectory(freertos)

#Link against the mqtt library so that we can use it. Dependencies are transitively
#linked.

target_link_libraries(my_app PRIVATE AFR::mqtt)

target_link_libraries(my_app PRIVATE AFR::freertos)

target_link_libraries(my_app PRIVATE AFR::kernel)

Due to errors I am having the app_main.c file contains empty void app_main()function.

additional information : I use following commands in sequence to build the project.

  1. Export the idf.py command in command prompt :
    freertos\Vendors\espressif\esp-idf\export.bat
  2. make Build files.
    cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=freertos/tools/cmake/toolchains/xtensa-esp32.cmake -GNinja
  3. Build the project
    cmake --build build

hope It helps. let me know if there is any issue in these steps as well.
Thank You!

Hi @naitik, did you download the git submodule? After adding this to the list of submodules, you can download the submodule with the command:

git submodule update --init --recursive

In my case, the file that is missing (according to your error) can be found here:
freertos\vendors\espressif\esp-idf\components\esp32\ld\esp32.project.ld.in

Hello, Yes I have downloaded submodules as well. as As you pointed out I have indeed the esp32.project.ld.in

However I notice when you see this error carefully :

/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: cannot open linker script file esp32.project.ld: No such file or directory
collect2.exe: error: ld returned 1 exit status

It looks like its trying to look up for the .ld file in Esp-IDF Installed in My OS environment instead of looking into freertos directroy. can you tell why this happens?

Hi @naitik, I apologize for the delay.

I’m not familiar with why it would be checking there first. If you post the question on the Espressif section of the forums, there may be some other users that have ran into that specific issue. Otherwise, do you need to have that local copy of esp-idf installed? In my case I don’t have it installed locally since it is already provided as part of the repository.