Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

C/C++ programming on Linux

3 min read

For a course at the university we have to develop a system using C++ on Linux. Being from the Java and C# world this won't become an easy task, already by choosing the right development tools and environment (coding, debugging, deploying...). As I wrote already in a post some time ago, the suggestion was to use the VIM or GVIM editors for editing source files. I tried it out for a while but that becomes a really tedious job if you go ahead with those editors. I know, deep-rooted linux fanatic people will hate me now...VI is not just an "editor" for them :) I mean, if you're accustomed in using it, you may also be fast, but to produce a piece of software where you have to keep track of multiple files it may invoke a lot of switching between source files, typing in commands etc...So initially we actually started using VIM, but then today I decided to go over to one of my favourite IDEs: Eclipse. I've seen that there exists also a C/C++ distribution but I didn't ever try it out...today was a good day for doing so.
I downloaded the Eclipse's C/C++ distribution and launched it on my Ubuntu Hardy system. Everything worked fine, I was also not surprised since I already used Eclipse on Ubuntu for developing Java apps.

The first step is to create a new project. Just start the Eclipse project wizard as usual and then select the "Hello World C++ Project" from the "Makefile project" category.

Pay attention to choose the Linux GCC compiler. I think you have to have g++ installed, so please check your system. When you press "Finish" Eclipse will create you the initial project structure. You should already be able to start it and get a nice message printed on the console:

It becomes however more complicated once you add new classes which get linked from your main class (which printed the "Hello World"). Assume you create a class "Hello" and reference it from within your main class as follows:

You'll most probably get an error message like "undefined reference to `Hello::Hello()'". The problem is that you make (created by Eclipse) needs to be updated s.t. also Hello.cpp and its header Hello.h are compiled correctly and linked to your main class. Now if you don't want to do that manually you can also let Eclipse do it for you by generating your make files. For doing so you have to right-click on your project, goto "Preferences" > "C/C++ Build" where you check the item "Generate Makefiles automatically".

Now it should work, and Eclipse generates automatically make files for new classes.

Developing using an IDE gives you so much more benefits as I believe. For instance creating a new class, Eclipse automatically splits it up in a header and implementation file. Furthermore it indicates you (with an appropriate icon) the position where a compiler error occured, helps finding the right/available methods with "intellisense" and much more.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus