0

I am trying to create an executable from a project consisting purely of C files. When I try to create a library as in the following:

add_library(application)
target_sources(application PUBLIC main.c)
target_include_directories(application PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(application PUBLIC lib1 lib2 lib3)

The creation of library is sucessful. But what I need is an executable (.elf file to be more precise) if I add something similar to the following:

add_executable(application.elf)
target_sources(application.elf PUBLIC main.c)
target_include_directories(application.elf PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(application.elf PUBLIC lib1 lib2 lib3)  

I get the following error: AR19DD~1.EXE: fatal error: cannot specify -o with -c, -S or -E with multiple files compilation terminated.

The following are my Compiler and linker flags :

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -x c -mthumb -D__SAM3X8E__ -DDEBUG -DBOARD=ARDUINO_DUE_X -Dscanf=iscanf -DARM_MATH_CM3=true -Dprintf=iprintf -D__SAM3X8E__ -fdata-sections -ffunction-sections -mlong-calls -g3 -Wall -mcpu=cortex-m3 -c -pipe -fno-strict-aliasing -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror-implicit-function-declaration -Wpointer-arith -std=gnu99 -ffunction-sections -fdata-sections -Wchar-subscripts -Wcomment -Wformat=2 -Wimplicit-int -Wmain -Wparentheses -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs -Wunused -Wuninitialized -Wunknown-pragmas -Wfloat-equal -Wundef -Wshadow -Wbad-function-cast -Wwrite-strings -Wsign-compare -Waggregate-return  -Wmissing-declarations -Wformat -Wmissing-format-attribute -Wno-deprecated-declarations -Wpacked -Wredundant-decls -Wnested-externs -Wlong-long -Wunreachable-code -Wcast-align --param max-inline-insns-single=500")
set(CMAKE_EXE_LINKER_FLAGS "-mthumb -Wl,--gc-sections -mcpu=cortex-m3 -Wl,--entry=Reset_Handler -Wl,-T\"../flash.ld\"")
  • `-c` well, so don't pass `-c`? How do you want to produce an executable if you always pass `-c` option to your compilter? Did you go through every flag you pass to `CMAKE_C_FLAGS` and check it? Also why pass `-x c`? – KamilCuk Mar 30 '21 at 08:37
  • @KamilCuk You're right. I removed that. Now I get the following error: arm-none-eabi-gcc.exe: fatal error: no input files compilation terminated. I have setup CMAKE_C_LINK_EXECUTABLE to arm-none-eabi-gcc.exe – sanjanaraju_inde Mar 31 '21 at 07:27
  • `I have setup CMAKE_C_LINK_EXECUTABLE` But why? Don't. Here 5 min google search: https://github.com/vpetrigo/arm-cmake-toolchains/blob/master/arm-gcc-toolchain.cmake toolchain file, and read about toolchains in cmake. – KamilCuk Mar 31 '21 at 07:54

0 Answers0