Compiler Problem with own library | including is not working

Hello together,
I want to create my own library (some basic c-functions). I’m using mostly the Arduino Mega 2560, sometimes Arduino Uno.
So I created a directory in the standard library folder with the same name: MyStdLib

  • Arduino\libraries\MyStdLib\MyStdLib.h
  • Arduino\libraries\MyStdLib\MyStdLib.c

When I’m compiling my sketch file, the Arduino Compiler finds the file, but I get the error:

  • MyStdLib.h:89:34: error: unknown type name 'SerialRead';*

So I found out (because of Serial.print) , that I need to change the extension of my file for c++ coding:

  • Arduino\libraries\MyStdLib.cpp

But then I get this strange compiler error:

  • sketch\output.ino.cpp.o (symbol from plugin): (.text+0x0): first defined here*

  • collect2.exe: error: ld returned 1 exit status*

  • exit status 1*

The coding in my MyStdLib.c is working fine, I have tested in a couple of times, separately!
I also found out that following workarounds helps:
If I change the extension to ino => MyStdLib.ino then i have 2 possibilities how it is working:

  • I can put my library file in the sketch file folder and it works:
  • I also can include my library if I use the hole path:
#include "C:\Users\xxx\Arduino\libraries\MyStdLib\MyStdLib.ino"

I'm still wondering, why it is compiling if I include my library like this. :o

(But not really a good solution, when using different pc, systems, etc.)

Can't really help but when the include statement is literally replaced by the content of the included file.

There are a number of rules for the Arduino libraries; not sure where to find them. But I'm reasonably sure that you can't dump files straight into the libraries directory; it needs at least a subdirectory. For the rest of the rules, I don't know.

I suggest that you post the code of the library files as well as a basic sketch that makes use of them.

No!!! Don't #include .ino files.
The best place to put custom libraries is in the "libraries" folder under your "sketchbook" folder.
To understand what belongs in the .h file and what belongs in the .cpp file, see My Reply #3 in this Thread.
Finally, it's unlikely anyone can be of further help to you until you post a complete code example as noted by @sterretje.

It might be because ".cpp" files don't automatically include <Ardino.h>. Does it help to put this line at the top of your MyStdLib.cpp file?

#include <Ardiuino.h>

Hello, thanks for the tip, I could find the problem.
Actually I wanted to post the code but I did not know which of the three files are important for the problem finding. So I reduced the coding for better understanding...

As @gfvalvo wrote in his thread: declaration of "extern" is necessary.
That was my first mistake.
Also I did not know that this variables have to be defined in the *.cpp file, where the functions are.
My Mistake: I defined all global variable in the header file.

thanks for the fast support :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.