Go Down

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


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(){

void loop(){

Code: (TEST.H) [Select]

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

Code: (TEST.C) [Select]

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

void test(void){
  static uint8_t test;

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

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?


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

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


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" {
   int foo();
   double bar(int n);
   void test();
#ifdef __cpluscplus

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>


    #include "foo.h"



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.

@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.


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.

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?


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

Coding Badly


Follow the links.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131