Monday, 14 September 2015

Running c++ Hello World in the cloud: Codenvy

Running Hello World in Codenvy

(Updated October 2018 to reflect the changes in Codenvy)

Go to Codenvy


Sign Up or Login via the "GET STARTED" link.

Create a new Workspace. Give it a name like "CPP" and pick the stack C++. And Create the workspace.



To create the Hello World program click on "Workspace > Create Project..." Fill in a Name and description for the project and click Create.


Now expand the project in the Projects Explorer. You have a hello.cpp file. Double click on it and it already has everything to print Hello World!


The run command (the blue triangle top right) is wired to call make which expects a Makefile with the recipe to run the C++ compiler. Makefiles have annoying demands on tabs in them. We have to tell the editor to keep the tabs. That means truning off Tab expansion. Go to Profile > Preferences > IDE > Editor > Expand Tab and turn the option off:



Now let's create the Makefile with the following content. Note the tab before the g++.

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 (Project > New > File) with the name Makefile and paste the above lines. Make sure to change the space before the g++ to a tab!


Now you are ready to run the Hello World program. Click on the blue triangle and execute



Didn't run so great... We got a "run" tab in the Processes section at the bottom of the screen. And a nice big error. Looks like the C++ code is a bit off! Double click the hello.cpp to open up the file in the editor. Change the include statement on line 3 from iostream.h to iostream. Also cout is in the namespace std which isn't mentioned so prefix it with std:: Then hit the triangle and it works!




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!

Let's see. Here is an interactive C++ program:

#include <iostream>
#include <string>

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


Let's run it with the blue triangle:


Looks like it used /dev/null as input. Not cool.

But we have the "Terminal". Let's use that and do the following steps:
Much better!

SSH

Assuming you are on Linux and have run ssh-keygen then you have your public key in .ssh/id_rsa.pub Upload this to Codenvy. Go to Profile > Preferences > SSH > Machine > Upload Key and upload your public key. The Workspace then needs to be restarted Workspace > Stop > Start 

In the Processes panel, next to the dev-machine there is the word SSH. Click on that and you receive connection information:


You can now cut&paste the ssh connection command into your local console. Then cd to /projects and you can run your program:










16 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
  8. This blog is fully fabulous in all aspects.
    learn c++

    ReplyDelete
  9. I'm having trouble, exactly how do you run the code in the terminal?

    ReplyDelete