View Full Version : Compile C++ program for the Teensy 3.0 (Lat/Long to MGRS coordinate conversion)

12-18-2012, 05:31 PM
I want to convert latitude/longitude to MGRS coordinates. Using this wonderful library called "GeographicLib", I can do that quite easily on the windows platform. Here's my little program utilizing that library:

#include <iostream>
#include <exception>
#include <string>
#include "GeographicLib\UTMUPS.hpp"
#include "GeographicLib\MGRS.hpp"

using namespace std;
using namespace GeographicLib;

int main() {

double lat = 33.3, lon = 44.4; // Baghdad
int zone;
bool northp;
double x, y;
UTMUPS::Forward(lat, lon, zone, northp, x, y);
string mgrs;
MGRS::Forward(zone, northp, x, y, 5, mgrs);
cout << mgrs << "\n";

return 0;

It compiles, runs, and gives me the MGRS coordinates accurately. Geographic lib can be downloaded from: http://geographiclib.sourceforge.net/

HOWEVER, I'm having serious issues getting this to compile in the arduino IDE that comes with the Teensy 3.0. I was just going through the errors as they came to me during compilation, but it's become apparent to me that I'm going to have to fix a hell of a lot of errors. And some of them are starting to become beyond my ability to fix or even understand (I'm a rather novice C++/C programmer). Anyone have any ideas?

12-18-2012, 09:09 PM
Post the errors.

12-19-2012, 02:59 PM
It's possible to compile and run C++ code on a microcontroller like the Teensy3, but some C++ language features depend on fairly complex and heavyweight library support (for example, STL and exceptions). In your case, if the GeographicLib library actually uses exceptions and C++ strings (as suggested by the #includes) then you're right: you may have to fix a hell of a lot of errors (actually, you may have to provide a hell of a lot of C++ language support).

If I was you, I'd start with the C version of the library. It's apparently just 2 files, and any problems you have compiling them are likely to be math-support related (and also problems you'd face in the C++ version, if you ever got through the STL, exception and memory-allocation issues of supporting C++).

12-19-2012, 04:50 PM
Yeaahhhhhh, I was thinking of doing that spacewrench. Thanks for the advice.