Go Down

Topic: Including header files (Read 1 time) previous topic - next topic

ArduiNoNo

Hi,

Although I'm pretty experienced with programming AVR's, I have a very basic question the arduino IDE. I'm trying to include a header file, but no matter what I do, I keep getting the following error:
"undefined reference to `test()'"

Here's my stripped code:
Code: (MAIN) [Select]

#include <WProgram.h>
#include "test.h"

void setup(){
    test();
}

void loop(){
  while(1);
}

Code: (TEST.H) [Select]

#ifndef TEST_H
#define TEST_H
  void test(void);
#endif

Code: (TEST.C) [Select]

#include "test.h"
#include <stdint.h>

void test(void){
  static uint8_t test;
  test=2;
}


Has anyone an idea what I'm doing wrong??

ArduiNoNo

Well I already found a (sort of) solution. When I rename test.c to test.cpp, it works.

However, I'm not using C++ and since I want my libraries to be usable in avrstudio too, I just want to use plain c files. Does anyone know how I can arrange that in the arduino IDE?

ramo102

AFAIK, you can use both .c or .cpp file extension for sketches, but you must use .cpp extension for Arduino libraries.

See:
http://www.arduino.cc/en/Guide/Environment (under "Tabs, Multiple Files, and Compilation")
http://arduino.cc/en/Hacking/LibraryTutorial

PaulS

C++ allows for function overloading, where one function, like Serial.print(), can print characters, arrays of characters, ints, array of ints, etc.

To support this, the C++ compiler performs a process known as name mangling, where the function name is actually constructed from the name of the function and the types of all the arguments.

C does not support function overloading, so it does not support name mangling.

The C++ compiler is invoked when the main program is C++, which is what the sketch is converted to.

You can tell the C++ compiler not to perform name mangling for a function, by using extern "C" in front of each function name in the header file:

extern "C" void test();

If there are multiple functions you want to define, you can do this:

extern "C" {
   int foo();
   double bar(int n);
   void test();
}

If you do this, the C compiler will have a tizzy fit, so:

#ifdef __cpluscplus
   extern "C" {
#endif
   int foo();
   double bar(int n);
   void test();
#ifdef __cpluscplus
   }
#endif

The __cplusplus term is defined when the C++ compiler is invoked, but not when the C compiler is.

Jack Christensen

One thing I haven't been able to figure out yet, what is the difference between

    #include <foo.h>

and

    #include "foo.h"

?
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

PaulS

The difference between <> and "" is where the compiler looks for header files. The "" means local (in the sketch folder). The <> is for library files (not in the sketch directory). Some compilers are not rigorous in enforcing this distinction.

Jack Christensen

@PaulS, thanks, had been through the avr-libc manual, etc., and couldn't find an explanation. Maybe I need to re-read K&R.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

ArduiNoNo

@Pauls, thanks for your clear explanation. That makes it work.

Just one remark to make this topic useful for other readers: you made a typo in your example. It should be #ifdef __cplusplus instead of __cpluscplus.

PaulS

Quote
It should be #ifdef __cplusplus instead of __cpluscplus.

I knew that. My fingers were operating on their own this morning.

Yeah, that's my story, and I'm sticking to it.

ArduiNoNo

Now I've got another problem:
When I try to include SD.h in my library, the compiler says "No such file or directory". But when I include it in the main-file, i get no complaints.
Does anybody have an idea how that comes?

ArduiNoNo

*kick*

Does no one know why I can't include the arduino libraries in other files than the main sketch file?

Coding Badly


http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1280467066

Follow the links.

Go Up