generate .o files from compiler instead of .obj files

I am working in an embedded project.
target : ppc
compiler : windriver
Host environment : windows 10
build generator : cmake, MinGW makefiles

during compilation i see .c.obj getting generated.
I want .o instead of .c.obj.
from cmake side can we do this or it is compiler specific ?

The extension is compiler-specific. Why does the extension matter in this case?

And the .c is always going to be there under CMake (some projects have foo.c and foo.cxx and CMake’s strategy avoids this conflict unconditionally).

Sorry to revive this topic, but I have the same issue. cmake is used to build a project for embedded device (e.g. STM32 controlllers). The toolchain used is arm-none-eabi-gcc.
I created a configuration file as described in cmake documentation about embedded.
CMakeLists.txt typically contains :

cmake_minimum_required(VERSION 3.26)
set(CMAKE_TOOLCHAIN_FILE $ENV{PATH_UKOS_KERNEL}/Ports/Mkfiles/Toolchains_cmake/cortex-m.cmake)

project(
  Nucleo_L4R5_Variant_Test
  VERSION 1.0
  LANGUAGES C)

The extension .obj is not chosen by arm-none-eabi-gcc by defaults. This extension is set by the command generated by cmake. This is seen using VERBOSE=1 to build the system, for instance

[  1%] Building C object CMakeFiles/rtcb_p.dir/Users/Dev/u169/os-kernel-iv/Ports/EquatesModels/Generic/Runtime/crt0.obj
/opt/embedded/cross/gcc-current/cortex-M/bin/arm-none-eabi-gcc -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g3 -pedantic -std=c2x -Os -o CMakeFiles/rtcb_p.dir/Users/Dev/u169/os-kernel-iv/Ports/EquatesModels/Generic/Runtime/crt0.obj -c /Users/Dev/u169/os-kernel-iv/Ports/EquatesModels/Generic/Runtime/crt0.c

The extension .obj is forced by the compiler option
-o CMakeFiles/rtcb_p.dir/Users/Dev/u169/os-kernel-iv/Ports/EquatesModels/Generic/Runtime/crt0**.obj**

I tried to use set(CMAKE_C_OUTPUT_EXTENSION .o) but nothing changes.

Unless the extension is causing something to fail, just ignore it. You’re fighting a battle you don’t need to otherwise.

It is just making harder to support building project either using legacy make or cmake because need to provide different linker scripts, which is weird as the toolchain used is the same in both cases

Ok, that addresses the “why does the extension matter” question. Where did you try setting CMAKE_C_OUTPUT_EXTENSION? I think it should be part of the toolchain file.

I tried to put it in the file describing the toolchain, also in the CMakeLists.txt of the project, just after the command project().
(sorry as new user I cannot upload the toolchain file)
I am using cmake 3.28.1
the command i Have try is

set(CMAKE_C_OUTPUT_EXTENSION .o)

Can you try passing --trace-expand to the initial configure (and put stderr into a file for analysis) to see if the variable is set anywhere else?

I hope you can dowload the file produced by

cmake -S . -B build --trace-expand

stdout

Indeed it is set elswhere else



/Applications/CMake.app/Contents/share/cmake-3.28/Modules/CMakeCInformation.cmake(18):  set(CMAKE_C_OUTPUT_EXTENSION .obj )

Here is the content of the file defining the toolchain (cortex-m.cmake)

if(NOT DEFINED ENV{PATH_GCC_CORTEXM})
    message( FATAL_ERROR "Environment variable PATH_GCC_CORTEXM is not defined." )
endif()

option(USE_PICOLIBC "Use library picolibc instead of newlib" OFF)
if(DEFINED ENV{USE_PICOLIB})
	set(USE_PICOLIBC ON)
endif(DEFINED ENV{USE_PICOLIB})

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)
set(CMAKE_C_OUTPUT_EXTENSION .o)

set(PREFIX arm-none-eabi-)
set(PATH_TOOLCHAIN_PREFIX $ENV{PATH_GCC_CORTEXM}/bin/arm-none-eabi-)
set(CMAKE_C_COMPILER "${PATH_TOOLCHAIN_PREFIX}gcc")
set(CMAKE_CXX_COMPILER "${PATH_TOOLCHAIN_PREFIX}g++")
set(CMAKE_AR "${PATH_TOOLCHAIN_PREFIX}ar")
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_LINKER "${PATH_TOOLCHAIN_PREFIX}ld")
set(CMAKE_OBJCOPY "${PATH_TOOLCHAIN_PREFIX}objcopy")
set(CMAKE_OBJDUMP "${PATH_TOOLCHAIN_PREFIX}objdump")
set(CMAKE_RANLIB "${PATH_TOOLCHAIN_PREFIX}ranlib")
set(CMAKE_STRIP "${PATH_TOOLCHAIN_PREFIX}strip")
set(CMAKE_SIZE "${PATH_TOOLCHAIN_PREFIX}size")

set(GENERATE_LST ${PATH_TOOLCHAIN_PREFIX}objdump -f -p -D -d -h -t -s)
set(GENERATE_DIS ${PATH_TOOLCHAIN_PREFIX}objdump -S)

set(CORE_ENDIANNESS "LITTLE_ENDIAN")

# Common flags for arm bare-metal
if(USE_PICOLIBC OR DEFINED ENV{USE_PICOLIB})
set(CMAKE_C_FLAGS_INIT "--specs=picolibc.specs -mthumb")
else()
set(CMAKE_C_FLAGS_INIT "-mthumb")
endif()
set(CMAKE_CXX_FLAGS_INIT "-fno-rtti -fno-exceptions")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-nostartfiles")

It looks like it is supposed to be the compiler module that overrides it based on the comment before the setting you found. I think an issue might be in order to allow toolchain files to set it by guarding it with if (NOT CMAKE_C_OUTPUT_EXTENSION). Can you please file an issue about this?

Isn’t this the same issue? https://gitlab.kitware.com/cmake/cmake/-/issues/18713

Yes, looks like similar issue. It has not been addressed in 5 years, shall file a new issue anyway??

No point filing a new issue, it would just be closed as a duplicate of the existing one.