Using Custom Source File Extensions with Eclipse CDT Gnu Make Builder Projects

One great thing with the Eclipse Gnu Make Builder (aka ‘auto make’ or ‘auto build’) feature: just add source files (*.c, *.cpp, …), and with kind of magic, they all get compiled and linked properly.

But for something easy and convenient: is it hard to use custom file extensions? So what if I want to use a different file extension for my source files, different from the standard ones? Actually Eclipse CDT can do this too, it just takes two settings to recognize, compile and link source files with custom extension.

Custom file extension with Eclipse auto-build

Usually Eclipse projects are built by the ‘Gnu Make Builder’: It recognizes the standard file extensions of files inside the project and you don’t have to do anything:

GNU Make Builder

This article explains how you can use any file extension and use it for example as C or C++ source file.

The first step is to add the new extension to the Preferences > C/C++ > File Types:

Adding custom file extension

With this, the file gets recognized for example as C Source File with all the benefits like syntax coloring and adding it to the list of files to be compiled and linked.

There is just one more step missing: if compiling a new custom source file extension, gcc or the gnu linker/ld needs to be aware of the custom file extension, different from the standard/built ones. Otherwise you might get an error like this:

arm-none-eabi-gcc: warning: ../source/blinky.source: linker input file unused because linking not done

So we need to tell gcc that it needs to treat the file as C file. From the gcc man pages:

 -x language
       Specify explicitly the language for the following input files
       (rather than letting the compiler choose a default based on the
       file name suffix).  This option applies to all following input
       files until the next -x option.  Possible values for language are:

               c  c-header  cpp-output
               c++  c++-header  c++-cpp-output
               objective-c  objective-c-header  objective-c-cpp-output
               objective-c++ objective-c++-header objective-c++-cpp-output
               assembler  assembler-with-cpp
               ada
               f77  f77-cpp-input f95  f95-cpp-input
               go
               java

So what we need here is to add the option -x c to the compiler options.

In the NXP MCUXpresso IDE I can do it here:

-x c compiler option

With this, the project builds and links fine with my custom file extension.

Files compiled and linked

Happy file-extending 🙂

What do you think?

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

WordPress.com Logo

You are commenting using your WordPress.com 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.