Declaring a file-scope object in a function in a library

Ah, perfect, that was it, thanks! In classic shower-thoughts style, I figured it should have just been a pointer so I could call the constructor later, but still wasn't sure of the details. Got it running nicely now.

To help anyone in the future with a similar issue, here's the code I ended up going with:

#if _NEEDSERIAL 
	ReceiveOnlySoftwareSerial* sonar;
#endif

//...

uint8_t sonarHRXL::setMode(unsigned int mode, uint8_t signalPin){
	// if previously configured, disable previous mode
	#if _NEEDSERIAL
		if(_configured && (_mode == TTL || _mode == RS232))
			sonar->end(); // calls destructor
	#endif 

	// configure new mode
	if(mode == TTL || mode == RS232){
		#if _NEEDSERIAL
			sonar = new SoftwareSerial(signalPin, 0xFF, mode == RS232);
			sonar->begin(9600);
		#else
			return SERIALDISABLED;
		#endif
	} else if (mode == PWM || mode == ANALOG)
		pinMode(signalPin, INPUT);
	else
		return INVALIDINPUT;

	_signalPin = signalPin;
	_mode = mode;
	_configured = true;
	return SUCCESS;
}

Remember that you have to use the -> operator to call member functions from the pointer.

Unfortunately, with this setup, the compiler doesn't quite optimize away much of the SoftwareSerial library if it's not used. I ended up using preprocessor commands to remove the library if it's not needed, but that's not ideal.

FLASH RAM
With serial removed via flag 2948 bytes (9%) 289 bytes (14%)
With serial enabled via flag and not used 5726 bytes (17%) 329 bytes (16%)
With serial enabled via flag and used 6868 bytes (21%) 389 bytes (18%)
With serial enabled via flag and implicitly used
(iterates through modes)
6950 bytes (21%) 391 bytes (19%)

If there something I'm missing for why the preprocessor is still linking the library if it's not needed? Certain things need to be declared static?

Thanks again for the help!