Building libraries?

Hi! Im buidling a library for interfacing with serial device. So far i have
program.c which includes device.h, now device.c is using a lot of calls to Serial1.write() and Serial.write() both "WProgram.h" and <Serial.h> are #included in device.c.

When i "verify" program.c i get an error saying: device.c: 'Serial' undeclared.. what other files do i need to include to build my program?

Hi,
you need one less.
It's enough to include "WProgram.h". This makes the Serial.whatever() functions available to your code. Remove the #include<Serial.h> statement and try again. (actually there is no "Serial.h" file the arduino-core, so this include will always fail)
Eberhard

I finally reduced the number of errors down to a bunch of errors of this type:

In function 'serial_buf_read':
error: 'Serial1' undeclared (first use in this function)

-i -basically have this:
dev.c (containing a bunch of functions implementing a device protocol using the Serial class)
dev.h (Defining some magic number and structers, also, function prototypes)
program.c (talks to the hardware using the dev-library)

Now
dev.c contains:
#include "WProgram.h"

and program.c contains:
#include <dev.h>

Is there anything else i should do?

What include files does your sketch contain?

It looks like you need to change all "" to <> and vice versa.

Hi,
the static variables Serial1, Serial2 or Serial3 are only available when you comile the library/your sketch for an ArduinoMega.
This is not limited to libraries. Here is a short sketch

void setup() {
  Serial1.begin(19200);
}
void loop() {}

If you select an Arduino Nano board as the target it will raise the error you have seen. If you set the board to an ArduinoMega it will comile fine.

Is this a bug or feature? I'm undecided....

Eberhard

The sketch:
#include <dev.h>

The header:
#include <inttypes.h>

The header-c-file
#include "dev1.h"
#include "WProgram.h"

I am actually compiling for the mega and i still get "undeclared: Serial1.... "

The sketch:
#include <dev.h>

Ok

The header:
#include <inttypes.h>

Which header? Is this in the sketch? What is in the sketch? (For libraries I always start start with an empty sketch

#include "mylib.h"

void setup() {
}
void loop() {
}

to see if it compiles

The header-c-file
#include "dev1.h"
#include "WProgram.h"

WProgram should always be the first file to include.

Eberhard

The errors look like this:
C:\Users\Sailor\Desktop\arduino-0017\hardware\libraries\gx1\gx1.c: In function 'serial_buf_read':

C:\Users\Sailor\Desktop\arduino-0017\hardware\libraries\gx1\gx1.c:61: error: 'Serial1' undeclared (first use in this function)

C:\Users\Sailor\Desktop\arduino-0017\hardware\libraries\gx1\gx1.c:67: error: 'Serial' undeclared (first use in this function)

C:\Users\Sailor\Desktop\arduino-0017\hardware\libraries\gx1\gx1.c:68: error: 'DEC' undeclared (first use in this function)

C:\Users\Sailor\Desktop\arduino-0017\hardware\libraries\gx1\gx1.c:70: error: 'HEX' undeclared (first use in this function)

And the file structure looks like this
gx1.h:
#include "WProgram.h"

gx1.c:
#include "gx1.h"

sketch.pde
#include <gx1.h>
void setup(){}
void loop(){}

Hi,
I can't replicate this.
I created a minimal lib

gx.h

#include "WProgram.h"
void readserial();

gx.cpp

#include "gx.h"
void readserial() {
    Serial1.begin(1234);
}

sketch

#include <gx.h>
void setup() {
}
void loop() {
}

This compiles for a Mega-board and fails for a Arduino Nano (as expected).
Eberhard

I think including WProgram first, as wayoda says, will resolve your errors.

[edit]As all of you can see [below] wayoda was too quick for me (quoted me before I saw my err.) , I replied to this thread before reading it from the top.
/me is guilty :-[[/edit]

@wayoda
Does it work like this:
Code:
#include <gx.h>
void setup() {
readserial();
}
void loop() {
}

Why not? It's just a (public) function from a library.
Compiles for ArduinoMega, fails for an Arduino Nano. This is not an error! It is supposed to fail for a Nano, because it does not have a Serial1. (A more descriptive message would be a nice feature for future versions)

You could #include <HardwareSerial.h>

Don't do this. All the core functions are available to your library when "WProgram.h" is included. That's why someone named it the Core
Eberhard

You could #include <HardwareSerial.h>

Don't do this. All the core functions are available to your library when "WProgram.h" is included. That's why someone named it the Core
Eberhard

I disagree with you.
When writing a library I think it is much cleaner to explicitly show what you are using. It also takes less time to compile.
If one does not need all the core functions, there is no need to make them available either.
:slight_smile:

I disagree with you.
When writing a library I think it is much cleaner to explicitly show what you are using.

If its your 13'th lib, you may be right. Otherwise you will have a hard time finding and adding #include-statements for all of the functions provided by the core.
So I'll try again :
[ch945][ch946]

You could #include <HardwareSerial.h>

Don't do this unless you have a good knowledge about the inner workings of the Core. All the core functions are available to your library when "WProgram.h" is included.

It also takes less time to compile.

No to this one.
All the files that belong to the core are compiled and linked with the code that gets uploaded, no matter if you use any of the functions the core provides.
But this is only academic : compile times don't really matter when writing software with the IDE.

@gliderboy
After this little dog-member shootout is over, did this get you anywhere?
Otherwise you should probably post the library-code.
Eberhard

I ran into this same issue, and this was how I resolved it:

Serial is a c++ object, my filetype was .c, so the compiler was invoked as c only. renaming it to .cpp and everything worked fine.