Pages: [1]   Go Down
Author Topic: how to use additional .c files in a sketch  (Read 349 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a beginner with the Arduino environment and I need some help figuring out how to use some .c source files in a sketch without creating a library. I tried to find the answer on the forum but I couldn't, I apologize if the solution to my problem is somewhere in the posts and I just missed it.

So, I have a test.h file that contains only:

Code:
void doSomething(void);

The test.c file contains:

Code:
#include "test.h"

void doSomething(void)
{
      // there should be some code here
}

In my sketch, I have:

Code:
#include "test.h"

void setup()
{
  doSomething();
}

void loop()
{
}

I am not sure if this was needed or not but I also went to the Sketch menu and use Add File... to include both .h and .c file.

When I compile I get:

Code:
o: In function `setup':
C:\DOCUME~1\coprea\LOCALS~1\Temp\build50525.tmp/Temporary_3386_6704.cpp:6: undefined reference to `doSomething()'


C:\DOCUME~1\coprea\LOCALS~1\Temp\build50525.tmp/Temporary_3386_6704.cpp:8: undefined reference to `doSomething()'


If I understand correctly from the Build Process page, this should work so I am sure I am missing something. Thanks for your help in advance!
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

beginner--

The problem stems from the fact that you are trying to link a C function from a C++ module, without explicitly declaring that fact.  There are several solutions, including:

a) rename test.c to test.cpp
b) remove the #include "test.h" from your main sketch and replace it with
Code:
extern "C" void doSomething(void);
c) change the header to the clumsy:

Code:
#if defined(__cplusplus)
extern "C"
{
#endif
void doSomething(void);
#if defined(__cplusplus)
}
#endif

etc.

Mikal

« Last Edit: December 09, 2008, 11:48:30 pm by mikalhart » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very very much for taking the time to explain this to me, I used the first alternative and it works great.
« Last Edit: December 10, 2008, 08:42:06 am by merlin13 » Logged

Pages: [1]   Go Up
Jump to: