6

I have a few logging functions that I commonly use for different Arduino programs. Since I use them so much, I decided to try to make a custom library for them. Unfortunately, the compiler crashes at the header file with the error:

unknown type name 'String'

I'm a bit confused as to why this is happening because I am including the standard Arduino libraries (which I believe should contain the String class) at the top of my header. Here's the whole thing:

#ifndef logging_h
#define logging_h

#include "Arduino.h"

void logEvent(String msg);
void debugOut(String msg);
void errOut(String err);
void document(String parameter, float value);

#endif 

I reinstalled the Arduino IDE (1.0.5) so I think I should have the most recent standard library. If anyone has some suggestions I would really appreciate it.

3
  • Your code works for me in Arduino IDE 1.0.5. I was adding it as a new tab named "logging.h". What steps are you following to create your library? Commented Mar 25, 2014 at 15:37
  • Right now I just have the .h and .c files in the same directory as my main.ino. They both show up as tabs named logging.h and logging.c respectively. Other than that, I haven't done anything special to create the library
    – sdrendall
    Commented Mar 25, 2014 at 15:41
  • 1
    Try renaming the ".c" file to ".cpp". Commented Mar 25, 2014 at 15:42

2 Answers 2

11

(This answer is based on our discussion in comments.)

The problem was that the source file for your library was named *.c. That caused the compiler to treat it as C code instead of C++, which means it couldn't handle classes/objects (such as String).

Naming the file *.cpp instead lets the compiler treat it correctly as C++ code.

-1

I had same issue yesterday. The code you included in your question should be your .h file, isn't? My question is: is your library written in C or in C++?

I assume you use C code.

You can't import code from in a user C library with the Arduino IDE. The reason is that use C++ code, and it can't be called from your C library.

Solution: rewrite your library in C+, it's not too difficult.

You can find a lot of help on google on how to write library in C++. You can also check my example at https://github.com/romain-viollette/AverageFilter/

best regards,

3
  • I'm a little confused about you mean. Do you mean that I have to rewrite my functions as a C++ class? My understanding is that a C++ compiler should be able to compile C code, so should a library written 'in C' essentially be 'in C++'?
    – sdrendall
    Commented Mar 25, 2014 at 15:37
  • I was not clear enough. I mean that I think it's not possible to call Serial functions from a C file. Everything is compiled with g++ both C and C++ files. But I have noticed that When you include <Arduino.h> from a C file, as it is compiled separately, all C++ function are not included. My solution was to rewrite my library in C++, and I could import all the Arduino libraries I want without problem. Maybe you should have a look here: arduino.cc/en/Hacking/BuildProcess Commented Mar 25, 2014 at 15:39
  • Ah I see, that makes sense. The problem was ultimately solved by renaming my .c files to .cpp which seems consistent with your explanation. Thanks for the help!
    – sdrendall
    Commented Mar 25, 2014 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.