3

The Arduino IDE by default is oriented towards C++. The linker for example requires .cpp files and not .c files, otherwise it leads to errors during the linking process. See name mangling if you want to know more.

So, what are the tools that I need to setup a C environment? Can this be done from the Arduino IDE itself?

I work on a Linux system (Ubuntu 12.04).

  • Why would you want to prgram Arduino in "pure C"? C++ is often seen as superset of C (although purists may say that is not 100% true). Is your problem related to how you can link to libraries developed in C? Or is it more related to performance? – jfpoilpret Mar 2 '14 at 14:20
  • 1
    @jfpoilpret Its a matter of personal preference. I prefer using C to C++, most of the time. – asheeshr Mar 2 '14 at 14:24
  • 1
    OK. But, if you setup a pure C environment for development, that means you won't be able to use 3rd-paarty libraries based on C++, wouldn't that be a problem? – jfpoilpret Mar 2 '14 at 14:28
  • @jfpoilpret Hmm. I didn't give that aspect much thought. Yes, that will be a problem. – asheeshr Mar 2 '14 at 14:33
  • 2
    There are features supported in C that are not supported in C++, such as __flash introduced recently in avr-gcc. – Ignacio Vazquez-Abrams Mar 2 '14 at 17:32
3

Well, if you want to do more advanced stuff with your AVRs, you should definitely leave the Arduino environment, and integrate your AVR development in your "usual" favorite development environment, whatever it is.

Then you've got two choices, either do 100% pure C with AVR libraries, for which you only need a simple makefile that calls avr-gcc with avr-libc libraries as include path. You can also use Atmel Studio that helps you a lot on that stuff. But if you do so, you're loosing the advantages of the libraries built for/by the arduino community – which, sadly, is not a huge loss for a strong majority of those.

Or you can work your code out as a 100% C external library, used from within the sketch, and included at linking time. Then you'll have all the advantages of working with any C++ library you'd like to have, but still doing the important code in pure C. Having only the .ino used to generate the main .cpp and any .cpp library you'd need. Though, for many basic features like writing on the serial port, you'll have to do it [the good old way][2] or by doing some weird encapsulation of Serial.println() call sent as a functor to the pure C parts.

In the makefile I've written (debian offers another one with the non-gui install of arduino), every .c file from declared libraries are detected and compiled with avr-gcc, every .cpp file is compiled with avr-g++ and both gets linked when doing the .elf/.hex file.

| improve this answer | |
  • Why do you say that Atmel Studio will prevent you from using the Arduino libraries? They work just fine. But I agree fully with your recommendation to exit the Arduino IDE. With Atmel Studio you can add an AVR Dragon for $US50 or so that will support debugwire (for the UNO and friends) and JTAG for the Mega 2560. – kiwiron May 26 '14 at 7:38
0

Take a look at ino on linux and PCs and Mac. It's available in the fedora repos, not sure about other distros; though, I'm sure everyone can install it from the github project site, regardless of the distribution. According to the website, you can also install it with python setup tools:

pip install ino

or

    easy_install ino

BTW: sorry for not including this "answer" in a comment due to reputation restrictions.

Here's the description from the fedora repos; I think it's taken directly from the ino tool website:

Summary     : Command line toolkit for working with Arduino hardware
URL         : http://inotool.org/
License     : MIT
Description : Ino is a command line toolkit for working with Arduino hardware.
            : 
            : It allows you to:
            : Quickly create new projects
            : Build a firmware from multiple source files and libraries
            : Upload the firmware to a device
            : Perform serial communication with a device (aka serial monitor)
            : 
            : Ino may replace Arduino IDE UI if you prefer to work with command line and
            : an editor of your choice or if you want to integrate Arduino build process
            : to third party IDE.
            : 
            : Ino is based on make to perform builds. However Makefiles are generated
            : automatically and you'll never see them if you don't want to.
| improve this answer | |
0

The linker for example requires .cpp files and not .c files, otherwise it leads to errors during the linking process

No, it doesn't.


So, what are the tools that I need to setup a C environment?

Why do you want to?


The answer

Make a new tab in the IDE (top RH corner). Call it, for example, foo.c. That's your C file.

Put whatever C code you want into it. For example:

#include <Arduino.h>
void foo ()
{
PORTD = bit (1);
}

Now in your "main" sketch call that function, eg.

extern "C"
 {
 void foo ();
 }

void setup ()
  {
  foo ();
  }  // end of setup

void loop ()
  {
  }  // end of loop

Repeat as required for any C functions you care to write.

Of course, you can't use Serial (that's a C++ class) or Wire (that's a C++ class) or SPI (that's a C++ class), but if you want to work with C, you can certainly do that.

| improve this answer | |
0

You're already there with the Arduino IDE; C++ compilers understand plain C just fine, but may interpret C syntax slightly differently in some cases. I write most sketches in C, invoking C++ objects from libraries as necessary.

If you're to the point where you want finer grained control over which compiler, C or C++, gets used for a given module, then it's time to graduate to another environment. Eclipse is a very popular and flexible IDE for a large range of languages, compilers & cross-compilers, and provides a very flexible Arduino environment. Or your favorite shell, programming editor, Make, and the avr-gcc cross-compilation tool-set will give you the ultimate access to and control of every detail - perhaps more than you wanted - of the edit/compile/link/download/debug process.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.