I am working on a project that makes use of a library. The project is in a folder called TelescopeController. This contains TelescopeController.pde and a libraries folder. In the libraries folder I have four folders, each of which contains a library : CommandDetector, CommandProcessor, StepperMotor and StringSplitter. So far so good. Now the problem comes because I want to use the StringSplitter library in the code for the CommandProcessor library. At the head of CommandProcessor.cpp, which is in the CommandProcessor folder I have:
#include "..\StringSplitter\StringSplitter.h" // for splitting Strings into sections
which seems to work, but when I try and compile the whole thing I get this message :
CommandProcessor\CommandProcessor.cpp.o: In function `CommandProcessor::Process(String)':
C:\Programming\Arduino\TelescopeController\libraries\CommandProcessor/CommandProcessor.cpp:31: undefined reference to `StringSplitter::Split(String, int, String*, int)'
For some reason, although the CommandProcessor.cpp file has all the definitions for StringSplitter it cannot see the Split method. How come?
Here is CommandProcessor.cpp :
/*
* SOURCE FILE : CommandProcessor.cpp
*
* Class for processing incoming commands sent from another
* computer somehow, typically from a serial port.
*
*/
#include "..\StringSplitter\StringSplitter.h" // for splitting Strings into sections
#include "CommandProcessor.h" // this module's prototypes
/***************/
/* CONSTRUCTOR */
/***************/
CommandProcessor::CommandProcessor() {
}
/****************************/
/* PROCESS A COMMAND STRING */
/****************************/
// Pass command string in command parameter.
void CommandProcessor::Process( String command ) {
// Transmit back command.
Serial.print( '<' );
Serial.print( command );
Serial.println( '>' );
// Split command into sections separated by commas.
int count = StringSplitter::Split( command, ',', commandSections, maxSections );
// Transmit sections found.
for( int i = 0; i < count; ++i ) {
Serial.print( i );
Serial.print( " : <" );
Serial.print( commandSections[ i ] );
Serial.println( '>' );
}
}
and here is StringSplitter.h :
/*
* SOURCE FILE : StringSplitter.h
*
* Splits strings into sections.
*
*/
#ifndef StringSplitterIncluded
#define StringSplitterIncluded
#include "WProgram.h" // Arduino stuff - needed for String class
class StringSplitter {
public :
/********************************/
/* SPLIT A STRING INTO SECTIONS */
/********************************/
// Pass string to be split in s parameter.
// Pass character used to separate sections in separator parameter.
// Pass array of strings to accept sections in sections parameter.
// Pass length of the sections array in maxSections parameter.
// Returns number of sections extracted.
static int Split( String s, int separator, String sections[], int maxSections );
};
#endif
// END of StringSplitter.h
and StringSplitter.cpp :
/*
* SOURCE FILE : StringSplitter.cpp
*
* Splits strings into sections.
*
*/
#include "StringSplitter.h" // this module's prototypes
/********************************/
/* SPLIT A STRING INTO SECTIONS */
/********************************/
// Pass string to be split in s parameter.
// Pass character used to separate sections in separator parameter.
// Pass array of strings to accept sections in sections parameter.
// Pass length of the sections array in maxSections parameter.
// Returns number of sections extracted.
static int StringSplitter::Split( String s, int separator, String sections[], int maxSections ) {
sections[ 0 ] = s;
return 1;
}
Now I don't think there are any syntax errors in the code. I think it is a linking error. Any ideas?
To be honest, if I was working in any other environment I would just put all the cpp and h files in the same folder as TelescopeController.pde, but then it won't link at all, hence the proliferation of folders. Is there a better way?