Pages: [1]   Go Down
Author Topic: Including header files  (Read 1140 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
#include <WProgram.h>
#include "test.h"

void setup(){
    test();
}

void loop(){
  while(1);
}
Code: (TEST.H)
#ifndef TEST_H
#define TEST_H
  void test(void);
#endif
Code: (TEST.C)
#include "test.h"
#include <stdint.h>

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Italy
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49237
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

    #include <foo.h>

and

    #include "foo.h"

?
Logged

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49237
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS, thanks, had been through the avr-libc manual, etc., and couldn't find an explanation. Maybe I need to re-read K&R.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49237
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

*kick*

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12894
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Follow the links.
Logged

Pages: [1]   Go Up
Jump to: