chipKIT® Development Platform

Inspired by Arduino™

How to make function in c file

Created Sat, 15 Jun 2013 02:20:08 +0000 by shahrul


shahrul

Sat, 15 Jun 2013 02:20:08 +0000

Hi, I have succesfull write code. Then I try to put some function in .pde file into .c file. How I want to call function in the .c file? What command to add?


majenko

Sat, 15 Jun 2013 09:34:23 +0000

You have to reference the function you want to access as "external". Further more, as the function is in a C file, and not a C++ file, you have to set that reference within the C context:

// test.pde
extern "C" {
  extern int add(int a, int b);
}

void setup()
{
  Serial.begin(9600);
  Serial.println(add(5, 10));
}

void loop()
{
}
// myfuncs.c

int add(int a, int b)
{
  return a+b;
}

If the functions were in a C++ file (.cpp) then you wouldn't need the C context, just the "extern int add(int a, int b);" line.


shahrul

Sun, 16 Jun 2013 11:31:46 +0000

Thanks, now I'm understand a little bit to do .cpp file. I just edit .cpp file using software Visual Studio. Is there any other software that other people use?

Then, I hope can do own library. I have just do library for DHT11. Can I share it here?


majenko

Sun, 16 Jun 2013 11:33:58 +0000

I'm a bit of a minimalist. If I'm not using MPIDE then I'm using vi. I don't often use IDE software unless I have to.