Linux Project REST Server


Click here to view the source code

For the past few years, Microsoft is trying to be more cross platform friendly. In their attempt to make the development process seamless across different OS platforms, Visual Studio 2017 is able to create and develop C++ projects in Windows that can deploy, build, and run in Linux. Recently, I have been building projects creating ASP.NET Core REST API servers and wanted to explore how to build a server in C++ through a Linux project.

Running C++ REST SDK in Windows

Before creating a Linux project, let us get familiar with hosting a REST API server in Windows. For the server, I am going to leverage Microsoft’s recently developed C++ REST SDK to simplify the process. The SDK takes advantage of asynchronous operations, multi-platform development, and ability for easy debugging. We will first start by creating a C++ Win32 Console application

There are multiple ways to incorporate the SDK into our project depending on the development environment. For the purpose of this post, I am going to leverage Nuget.

After sifting through the SDKs Samples and Tutorials, the project consists of one rest server class.

class restServer
	restServer(utility::string_t url);

	void on_initialize(const string_t& address);
	void on_shutdown();

	pplx::taskopen() { return; }
	pplx::taskclose() { return m_listener.close(); }

	void handle_get(http_request message);
	void handle_put(http_request message);
	void handle_post(http_request message);
	void handle_delete(http_request message);
	void handle_error(pplx::task& t);
	http_listener m_listener;

When sending an HTTP GET request for “/api/status”, we successfully receive a response.

	"body": "GET /api/status HTTP/1.1 Accept: application/json Connection: Keep-Alive Host: localhost:34568",
	"path": "Status response"

Setting Up Windows Subsystem

Now that we are able to set up a simple REST server in Windows let us look into deploying the application into Linux. Recently, Microsoft released the Windows Subsystem for Linux (WSL) that allows for hosting a Linux environment within Windows using Bash on Windows. In order to work with WSL, there are a series of steps needed for setup, most important are making sure the Windows 10 Creators Update is installed on your OS and Visual Studio contains the Visual C++ for Linux configuration. Once the Linux environment is set up on the OS, we will start by creating a Linux Console Application.

Visual Studio will prompt for credentials to connect to the Linux system. Although this post describes how to build and deploy using WSL, any Linux environment will work if you have permission to connect.

Once the project is created, we can view the output when its executed by bringing up the Linux Console (Debug -> Linux Console).

Building the project though provides little information on what is going on underneath the scenes.

We can change this by going into Visual Studio’s Tools -> Options -> Projects and Solutions -> Build and Run and changing the build verbosity to ‘Normal’. The result will better illustrate how Visual Studio deploys the class files to Linux, builds in that environment, deploys the resulting ‘out’ file back to Windows for debugging purposes, and cleans up.

Running C++ REST SDK in Linux

Once the Linux project is able to build and run, we can go ahead and port the REST server code from the Windows C++ project. Even though the Linux project will still take advantage of NuGet to resolve the SDK’s dependencies, the Linux environment will need to contain the same dependencies, which NuGet in Windows does not resolve. After doing an ‘apt-get’ in the Linux environment and building the project you will notice an issue comes up when attempting to compile in Linux.

For some reason the Linux environment cannot resolve missing boost references. Looking through various posts online discussing undefined references seem to suggest the Linux project is not including the right command line arguments.We can fix this by going into the Linux Project’s properties and looking into Linker -> Input. Under Additional Dependencies, we need to include the missing command line arguments here at the end. I kept running into numerous undefined reference issues and after doing enough research online found the missing arguments needed are lboost_system, lcrypto, lssl, and lcpprest.

After including the command line arguments, the project successfully builds and run in my WSL environment.

We can go ahead and test this feature using a REST client and viewing the response. Here, I am using the Advanced REST client (ARC) and we can see the response provided when doing an ‘api/status’ request.


Overall, the ability to build Linux applications in Windows is quite elegant. Coming from a primarily C# .NET background made the process of getting the Linux project to work with Window’s C++ REST SDK quite difficult given how low level you need to understand in order to get everything working (hence changing the build verbosity). The next concept I want to work on is to leverage MAKE files to allow for non-Visual Studio developers to work with the same project in their native Linux environment without having to care what Visual Studio is doing for Windows developers.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s