Skip to main content
Removed phrase that might have been misinterpreted.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

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? No, skip that, it's a dumb question.


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.

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? No, skip that, it's a dumb question.


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.

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.

Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

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? No, skip that, it's a dumb question.


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.