C++ Library Wrapping Weather.gov REST API

I’ve began creating a library that wraps the REST API offered by weather.gov. It’s not much right now, you can only input a latitude and longitude to get the highs and lows for the next 7 days, but it’s pretty neat nonetheless, and I had a pretty good time trying to get everything to work the way that I wanted it to.

Information

Why create a library for this? TO LEARN! I wanted to learn how to make a library in C++, and learn how to use the CURL library in C++, and then take these libraries and use them in a project. I don’t have a real project to use them in for now, but eventually I will use it, and add more to it.

Pro-Tips for C++ Libraries

In Visual Studio, you’ll see that there is a template for creating a Class Library in C++. This is for creating a library that uses managed code that is compiled with CLR (Common Language Runtime). This is not what you want. It’s a pain to get this set up if you plan on solely using C++ for your library. The best way to do it is to create an empty project then go from there.

Quick Tutorial:

First, create a new Empty Project, call it TestApp. Now, in the same solution, create a new project, and make it an Empty Project, this will be our library. You can call it TestDLL. The next thing we are going to do is create some classes to use in our library. Add in a header file called TestAPI.h. We will not be adding in any C++ Source Files (.cpp) because it creates issues in the namespacing. We are going to start it off really simple:

#pragma once //sorry, windows only. Too lazy to make ifndef

namespace TestAPI
{
	class Mathematics
	{
	public:

		Mathematics() {}

		int Multiply( int num1, int num2 )
		{
			return num1 * num2;
		}

	};
}

That’s all you need as far as code for the library. Now we are going to configure our TestApp project to use our library:

 

Resolve some dependencies
Add a reference to the TestDLL Library

Now, after that, you should be able to just reference your Mathematics class really easily. Now lets create the main function for our TestApp project that’s going to use our library. Go ahead and create a C++ Source File called main.cpp, and add the following code:

#include <iostream>
#include "TestAPI.h"

using namespace std;

int main()
{
	TestAPI::Mathematics math;
	int result = math.Multiply(3, 5);

	cout << "Results: " << result << endl;
	// Results: 15

	return 0;
}

That’s it!

Problems

In my implementation of the Weather REST API, I found that when I tried to use libcurl in my project, I had to include it in both my TestApp and my TestDLL. Other than that, everything went smoothly.

 

Download: C++ Weather REST API Wrapper

Leave a Reply

Your email address will not be published. Required fields are marked *