I am trying to convert an old uvProjx to cmake. It is supposed to generate the hex file for a robot. There is no main method, instead there is some custom stuff in the c files, but it should essentially still be compiled like an executable for arm. Compiling works (i think), hoverer right after, the linker throws an error. I have used a converter and ended up with this CMakeLists.txt:
# CMake minimum version
cmake_minimum_required (VERSION 3.1)
# Project Infomation
project( Target )
enable_language(ASM)
enable_language(C)
# Reset output path
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
# STDLIB
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)
# Set include path
include_directories (./Src)
include_directories (C:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/Firmware)
include_directories (C:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/Middleware/Src)
# The need build source path and build all files
set (SRC_FILE0 ./Lib/entry.cpp)
set (SRC_FILE1 ./Lib/main.cpp)
set (SRC_FILE2 ./Src/Application.h)
# CFLAGS
set (CMAKE_C_FLAGS "-g -Wextra -Wshadow -Wimplicit-function-declaration -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes -fno-common -ffunction-sections -fdata-sections -MD -Wall -Wundef -mthumb -mcpu=cortex-m4 " CACHE INTERNAL "c compiler flags")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D USE_UART -D STM32F405xx -D __ARM_STM32F405__ -D _HSE_CLK=8000 -D _HSE_BYPASS_OFF -D HSE_VALUE=8000000 -D HSI_VALUE=8000000 -D _KEIL ")
# CXXFLAGS
set (CMAKE_CXX_FLAGS "-Wextra -Wshadow -Wredundant-decls -Weffc++ -fno-common -ffunction-sections -fdata-sections -MD -Wall -Wundef -mthumb -mcpu=cortex-m4 " CACHE INTERNAL "cxx compiler flags")
# ASMFLAGS
set (CMAKE_ASM_FLAGS "-g -mthumb -mcpu=cortex-m4 " CACHE INTERNAL "asm compiler flags")
# LDFLAGS
set (CMAKE_EXE_LINKER_FLAGS "-g -Wl,--gc-sections -Wl,-Map=Target.map -mthumb -mcpu=cortex-m4 " CACHE INTERNAL "executable linker flags")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${CMAKE_SOURCE_DIR}/STM32FLASH.ld -L ")
# Generate the target
add_executable (${CMAKE_PROJECT_NAME}.elf ${SRC_FILE0} ${SRC_FILE1} ${SRC_FILE2} )
# Link the library to the target
target_link_libraries (${CMAKE_PROJECT_NAME}.elf )
# Generate the binary file
add_custom_target (${CMAKE_PROJECT_NAME}.bin ALL arm-none-eabi-objcopy -Obinary "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf" "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.bin" DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf)
# Generate the hex file
add_custom_target (${CMAKE_PROJECT_NAME}.hex ALL arm-none-eabi-objcopy -Oihex "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf" "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.hex" DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf)
# Echo the size Infomation
add_custom_target (size ALL arm-none-eabi-size "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf" DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf)
# Make flash to the board by st-link
add_custom_target (flash COMMAND st-flash write ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.bin 0x8000000)
# Make clean-all
add_custom_target (clean-all COMMAND rm -rf ${CMAKE_BINARY_DIR}/*)
Running make throws this error after compiling:
PS C:\Users\Laslo\Development\cpp\hbrs-praktikum\OpenRoboticBoard\UserApp\_Project\CMakeTest\build> make
[ 33%] Linking CXX executable bin\Target.elf.exe
c:/msys64/mingw64/bin/../lib/gcc/arm-none-eabi/10.1.0/../../../../arm-none-eabi/bin/ld.exe: unrecognized option '--major-image-version'
c:/msys64/mingw64/bin/../lib/gcc/arm-none-eabi/10.1.0/../../../../arm-none-eabi/bin/ld.exe: use the --help option for usage information
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles\Target.elf.dir\build.make:116: bin/Target.elf.exe] Error 1
make[1]: *** [CMakeFiles\Makefile2:90: CMakeFiles/Target.elf.dir/all] Error 2
make: *** [Makefile:90: all] Error 2
You can find the c files that are supposed to be compiled here: github link
Doing my research, I found that some people could solve a similar issue by adding
set(CMAKE_SYSTEM_NAME Generic)
I did add this right before project, however, this did not solve the issue. Any ideas and suggestions how to get this running? Again, my goal is to get the hex file of these compiled .c, .h and .cpp files.
Edit: I moved the above to a toolchain file: toolchain.cmake:
include(CMakeForceCompiler)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_CROSSCOMPILING 1)
set(CROSS_TARGET_TRIPLET "arm-none-eabi-")
set(CMAKE_C_COMPILER "${CROSS_TARGET_TRIPLET}gcc")
set(CMAKE_ASM_COMPILER "${CROSS_TARGET_TRIPLET}gcc")
set(CMAKE_CXX_COMPILER "${CROSS_TARGET_TRIPLET}g++")
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
I have set to include the argument in VS Code, it does the following:
[main] Configuring project: CMakeTest
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE=C:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/UserApp/_Project/CMakeTest/toolchain.make -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\msys64\mingw64\bin\arm-none-eabi-gcc.exe
This still throws the same error when running make. Configure output:
[main] Configuring project: CMakeTest
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE=C:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/UserApp/_Project/CMakeTest/toolchain/toolchain.cmake -DCMAKE_CXX_LINK_EXECUTABLE=C:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/UserApp/_Project/CMakeTest/STM32FLASH.ld -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\msys64\mingw64\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\msys64\mingw64\bin\arm-none-eabi-g++.exe -SC:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/UserApp/_Project/CMakeTest -Bc:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/UserApp/_Project/CMakeTest/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.1.0
[cmake] -- The CXX compiler identification is GNU 10.1.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/msys64/mingw64/bin/arm-none-eabi-gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/msys64/mingw64/bin/arm-none-eabi-g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- The ASM compiler identification is GNU
[cmake] -- Found assembler: C:/msys64/mingw64/bin/arm-none-eabi-gcc.exe
[cmake] -- Configuring done (3.1s)
[cmake] -- Generating done (0.1s)
[cmake] -- Build files have been written to: C:/Users/Laslo/Development/cpp/hbrs-praktikum/OpenRoboticBoard/UserApp/_Project/CMakeTest/build
I also found that there is a custom linker in the old project, could that be of use? (Dumb question, of course there is a purpuse, I just have no Idea whats going on so...). You can find the linker in the github repo branch Edit 2: I deleted the build folder to get the output, then it complained it couldn't find the toolchain, turns out I spelled it wrong. Now I get a different error!
[ 33%] Linking CXX executable bin\Target.elf
Reaping winning child 0000000000688e00 PID 6865232
CreateProcess(C:\Program Files\CMake\bin\cmake.exe,"C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\Target.elf.dir\link.txt --verbose=,...)
Live child 0000000000688e00 (bin/Target.elf) PID 6852208
Error running link command: %1 is not a valid Win32 application
Reaping losing child 0000000000688e00 PID 6852208
make[2]: *** [CMakeFiles\Target.elf.dir\build.make:112: bin/Target.elf] Error 2
Removing child 0000000000688e00 PID 6852208 from chain.
Reaping losing child 0000000000b50d80 PID 11881248
make[1]: *** [CMakeFiles\Makefile2:90: CMakeFiles/Target.elf.dir/all] Error 2
Removing child 0000000000b50d80 PID 11881248 from chain.
Reaping losing child 0000000000684ea0 PID 6819552
make: *** [Makefile:90: all] Error 2
Removing child 0000000000684ea0 PID 6819552 from chain.