What is the Minimum Requirements for a Library Folder

I understand the principles of how to use " # include <filename.h> " to link to an external .h file where coded functions or subroutines can be referenced.

By example to use the RTClib library use #include <RTClib.h> & then ensure the RTClib library folder is accessable under the arduino library folder. The RTClib folder then contains text readable files of type .h and .CPP and also a file keywords.txt and a sub folder of example codes.

My question is: could I have a library folder with just a text file that will have the .h suffix and dump my common subroutines into that file, I intend that these lines of code will just be extracted from my top level .ino file OR must I use subroutines in the .h file that calls up further code in the .CPP file ?

I've not found a specific tutorial on librarys under the arduino.cc page.

Thanks for any assistance

My question is: could I have a library folder with just a text file that will have the .h suffix and dump my common subroutines into that file

Yes, and you can try this without a risk :wink:

update - one of my .h only libs

// 
//    FILE: temperature.h
// VERSION: 0.1.00
// PURPOSE: temperature functions for Arduino
//
// HISTORY:
// 2011-04-17 first version 
// 2011-05-01 optimized dewPointFast()
// 2012-12-25 added C2F variations
// 

#ifndef temperature_h
#define temperature_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#define TEMPERATURE_LIB_VERSION "0.1.00"

// TEMPERATURE
#define Celsius2Fahrenheit(C)	((C)*9/5+32)
#define Fahrenheit2Celsius(F)		(((F)-32)*5/9)
#define Celsius2Kelvin(C)		((C)+273.15)
#define Kelvin2Celsius(K)		((K)-273.15)


// DEWPOINT
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
	double A0= 373.15/(273.15 + celsius);
	double SUM = -7.90298 * (A0-1);
	SUM += 5.02808 * log10(A0);
	SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
	SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
	SUM += log10(1013.246);
	double VP = pow(10, SUM-3) * humidity;
	double T = log(VP/0.61078);   // temp var
	return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
// optimized by 
double dewPointFast(double celsius, double humidity)
{
#define A 17.271
#define B 237.7
	double temp = (A * celsius) / (B + celsius) + log(humidity*0.01);
	return (B * temp) / (A - temp);
}

/*
optimized speed -273 - 3000
long CelciusToFahrenheit(long celcius)
{
  if (celcius >=0 )
    return (celcius * 471861 >> 18) + 32;
  else
    return -((-celcius) * 471861 >> 18) + 32;
}

// range not tested yet
long FahrenheitToCelsius(int f)
{
  // http://www.codeproject.com/KB/cs/FindMulShift.aspx
  long x = f - 32; 
  if (x >=0 )
    return (x * 3641 >> 15) * 5;
  else
    return -((-x) * 3641 >> 15) * 5;
	
 long x = (f - 32) * 5 ; 
  if (x >=0 )
    return (x * 3641 >> 15);
  else
    return -((-x) * 3641 >> 15);	
}

// integer version that does rounding
int Celcius2Fahrenheit(int celcius)
{
  return (celsius * 18 + 5)/10 + 32;
}

int Celcius2Fahrenheit(int prevCelsius, int celcius)
{
  return ((prevCelsius + celcius) * 9 + 325)/10;
}
  
*/

#endif
//
// END OF FILE
//

Yes you can do that but there is a danger if you use the .h file in multiple places (multiple files) then you will get duplicate symbols.

To avoid that put the function prototypes in the .h file, and the implementation in the .cpp file.

rbright:
I've not found a specific tutorial on librarys under the arduino.cc page.

Yes you can do that but there is a danger if you use the .h file in multiple places (multiple files) then you will get duplicate symbols.

@Nick,
don't the scope rules apply?
and where they don't the #ifdef construction takes care of redeclaration?

robtillaart:

Yes you can do that but there is a danger if you use the .h file in multiple places (multiple files) then you will get duplicate symbols.

@Nick,
don't the scope rules apply?
and where they don't the #ifdef construction takes care of redeclaration?

redeclaration is indeed fixed by the include guards. The problem is that, since the header file is pasted right into multiple compiled files, two files both declare the same functions, so the linker doesn't know which one to use.

The #ifdef only guards against nested includes.

If a.cpp and b.cpp both include foo.h and foo.h has an implementation (not just a function prototype) for function bar, then the linker will complain about duplicate implementations of bar.

But if you move the implementation into a .cpp file, the .cpp file is only compiled once, and this won't be a problem.

Thanks for explaining (never encountered this problem but still good to know)