Monday, 14 September 2015

Running c++ Hello World in the cloud: Codenvy and Docker

Running Hello World with the default runner

Go to Codenvy


Sign Up or Login if you have already done so.


To create the Hello World program click on "Create New Project"



Pick the "C/C++ Project", give the project the name "Hello World" and optionally a description. Click "Create"


You now have an empty workspace. Let's create the hello world program as follows:

Click on File > New > File


and give the file the name hello.cpp


Then type in the Hello World code

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    return 0;
}



In order to make your life easier you want a Makefile that remembers all the special characters you need to type to compile and build your program. Here is a very simple Makefile:

all:
g++ -std=gnu++11 hello.cpp

# Note that Makefiles require a tab before the g++ 
# It probably doesn't copy/paste well from this blog post

Create a new file with the name Makefile and enter the above lines. Do this by clicking on the HelloWorld label next to the downward facing triangle and the click File > New > File


Now you are ready to run the Hello World program. You need to pick the C++ runner from the dropdown in the top right and then click the green Run icon.


The runner opens at the bottom of the window and shows the Console. A lot of stuff scrolls by from Docker. Codenvy uses Docker to spin up a clean Linux environment into which it loads your code. It then asks the Makefile to build your program and executes your compiled program afterwards. You can see the output towards the bottom of the Console output.


Interactive shell

That was fun but can we have an interactive shell? How can we make the program prompt the user for her name and play it back? We need a different runner!

Click at the bottom of the screen on "Runners" and then pick "Configs" on the left side.



Then pick + Create New and the Properties dialog opens up for the new runner with the disingenuous name "ENV1-HelloWorld". Rename it to Interactive.


The green text is the Docker file that Codeenvy will use to start up the Docker runtime. The available commands are documented on the Docker website. Replace all the lines in the Dockerfile with these commands:

FROM codenvy/shellinabox
RUN sudo apt-get update && sudo apt-get -y install g++
ENV CODENVY_APP_BIND_DIR /home/user/app
CMD sleep 365d

Then click save on the right hand side.


I had to untick and tick the "Matches project" to make it show up in the left list.

Also make this the default Runner for the Project with the sliding button in the bottom middle.


Now run the project with the new "Interactive" runner. Pick the Interactive runner in the top right corner and click the green run icon. The Console scrolls through...


Now click on "Terminal". After a moment you are connected to the terminal of the Docker environment that was spun up. You can now use standard Linux shell commands to compile your program and run it. Try these commands:

cd app
make
./a.out


Now let's make our program interactive by asking for the name and playing it back. 
Go to the hello.cpp program and change the code to read as follows:

#include <iostream>
#include <string>

int main()
{
   std::cout << "Your name please: "; 
   std::string name;
   std::cin >> name;
   std::cout << "Hello: " << name << "!\n";

   return 0; 
}



If you kept the terminal session open you don't need to do cd app. Then enter the following commands:

make
./a.out

The shell will rebuild the changed program and the ./a.out will make it run, asking for your name and playing it back.

When you are done with the Docker runtime close it by clicking the red button.






12 comments:

  1. Awesome tutorial.

    One quick note: When you asked us to make the MakeFile file you posted the code as

    all:
    g++ -std=gnu++11 hello.cpp

    But if you just copy it like that it won't go. Something about not using spaces and needed tabs instead. I think it's a Linux thing.

    ReplyDelete
  2. Makefiles are fussy and HTML swallows whitespace so it probably just doesn't copy/paste well. The line doing the task *must* start with a tab.

    Glad you find this helpful!

    ReplyDelete
  3. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  4. as far as I understand this is not fully featured online IDE for C++, correct?
    this doesn't support even basic functionality like a : cmake-based project management, no autocomplete, no class tree, no static analisys. is it correct?

    ReplyDelete
    Replies
    1. They could probably do more for the C++ coder, yes. But with the Docker based runners you can get it to do an awful lot without having to install anything locally (just install CMake, Linters, whatever). Depends what you are looking for. It's a tool and it might be perfect for some use cases, less great for others. One area that comes to mind is training sessions where you don't have to wait 5 hours till everyone has installed 8GB of Visual Studio before they can start doing Hello World.

      Delete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. what I get is g++ command not found.

    ReplyDelete
  7. Hello. Just a quick question. Codenvy's layout has changes quite a bit since you made this guide, and I cannot find the runners part anywhere. Any idea as to where I can find it?

    ReplyDelete
    Replies
    1. Indeed Codenvy has changed since I wrote the blog post 2 years ago. I will have to redo the post.
      From a first look it seems that runners have morphed into "Stacks" that you connect to your workspace. Stacks appear to be configurable so that is where I would imagine the docker bits to be hiding.

      Delete
    2. Thank you for your assistance and I apologize for the long response time.

      Delete