Compiling C Files with GNU ARM G++

If I want a C++ project for my KL25Z Freedom board, I select C++ during the project creation:

C++ Project Creation for GCC

C++ Project Creation for GCC

This creates a gcc C++ project with all the needed settings.

This worked fine until I added a *.c file to my project which had code in it which was not accepted by the C++ compiler. Wait! Should the *.c not be compiled in C mode, as I was used to with other compilers? It turned out that things are different with gcc (or g++) :-(: the *.c files in my project are compiled in C++ mode. So the question is: how to compile in C mode with the ARM g++ compiler?

I was assuming that the ARM g++ compiler will compile the sources based on the file extension:

  • *.c as C
  • *.cpp and *.cxx as C++

Obviously I was wrong when I wanted to compile a C file, which has non-C++ compliant syntax: it gets compiled by g++ in C++ mode. This can be easily demonstrated with the following example:

C file compiled as C++

C file compiled as C++

As already discovered in this article, an ARM Eclipse C project is using a completely different tool chain (‘nature’).

C projects are using arm-non-eabi-gcc:



While a C++ project is using arm-none-eabi-g++:



So obviously G++ compiles my C file in C++ mode. Searching the internet indeed showed that I was wrong with my assumption that G++ treats *.c as C files. So how to tell g++ it has to compile my C files in C mode?

Searching the gcc man pages finally revealed an interesting an interesting option:

       -x language
              Specify explicitly the language for  the  following
              input  files  (rather than choosing a default based
              on the file name suffix) .  This option applies  to
              all  following  input files until the next `-x' op-
              tion.  Possible values of language are `c', `objec-
              tive-c',  `c-header',  `c++', `cpp-output', `assem-
              bler', and `assembler-with-cpp'.

       -x none
              Turn off any specification of a language,  so  that
              subsequent  files  are  handled  according to their
              file name suffixes (as they are  if  `-x'  has  not
              been used at all).

I checked the command line generated by Eclipse, but I have not seen that -x option. It seems to me that g++ internally is using the -x option to map the *.c to C++?

Anyway, my solution is to add

-x none

to the g++ command line in the project settings:

-x none added to the command line

-x none added to the command line

With this, g++ compiles my *.c in C mode:

Compiled as C

Compiled as C

Happy C-ing 🙂

1 thought on “Compiling C Files with GNU ARM G++

  1. Pingback: C++ with Kinetis Design Studio | MCU on Eclipse

What do you think?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.