I’m new on Arduino.
can I write my own .c and .h files and use them with main.pde project?
Here is what I did:
1/ I wrote funcion void tonTimer(T_TMR *t) and store it to file FC060.c
2/ I wrote line extern void tonTimer(T_TMR t); and store it to file FC060.h
3/ I included #include “FC060.h” in file main.pde and
I called this function tonTimer(&tmr1); but I got compiling error
undefined reference to 'tonTimer(T_TMR)’
What’s wrong ? Files FC060.c, FC060.h, main.pde are compiled OK when I delete line tonTimer(&tmr1);
Exactly what I would have put. Libraries are convenient to use, as other people can easily use them (should you choose to release them), and the functions that you can add are used in the same way as the standard ones are, so you do not need to add in lots of extra (non-hidden) code into the main sketch.
I have found myself with the same requirement. I have wound up with 1,000s of lines of code crammed into a single sketch merely because it is inconvenient in Arduino to build code that is divided into logical modules. Libraries appear to have to be C++ classes, and that is not what I want. The modules I want to make are bog-standard C code and I know well how to build and link these in a conventional C environment. I know that Arduino libraries are preferable in some cases, but that is not what I am trying to do.
Is there any provision in Arduino for conventional C modules, or is it necessary to go directly to the AVR toolchain to make this work?
Is there any provision in Arduino for conventional C modules, or is it necessary to go directly to the AVR toolchain to make this work?
That is what the separate code tabs are for. There is a arrow button pointing right, near top of the IDE edit window that allows you to write or copy separate functions that will then be compiled along with your main sketch. You do have to use the proper file extension name.
I do a lot of debugging in a real C development environment, and establishing a system of CPP macros and such that allow the same file to be built in Arduino and UNIX has been a challenge. This is an excellent step. Thank you.
In order to support overloading, C++ performs an operation called name mangling. The result is that a function's real name is defined by the user-supplied function name, and the types of all the arguments.
C doesn't support overloading, so it doesn't play well when the C++ compiler has mangled the names.
The C++ compiler CAN compile C code without name mangling, so that the c extension can be used, if some compiler directives are added to the header file.
ifdef __cplusplus
extern "C" {
endif
// The C function declarations go here
ifdef __cplusplus
}
endif
The __cplusplus symbol is defined by the C++ preprocessor, but not by the C preprocessor, so, when the C++ compiler runs, it sees the extern "C" wrapper, but the C compiler does not.
The extern "C" directive tells the C++ compiler not to perform name mangling, since the functions in the block are to be called by C code, too.
Since the code in the .c file is compiled by the C compiler, which does not perform name mangling, the un-mangled names are what the C++ compiler needs to add to the symbol table for the linker to use.
Without the extern "C" directive, the mangled names are added to the symbol table by the C++ compiler, but not by the C compiler, so the linker can't find the function since the name in the symbol table (of the compiled function) doesn't match the name of the called function.
Hello,
the problem is solved. It works after renaming *.c to *.cpp and after few mistakes correction. Now I know how to split large project. @Vanyamba Thank you, that was what helped.
Edit1: @PaulS Thank you for the explanation, I try also your way, it works too. I will use it in the future, it seems to be clearer.
Here is the final working project