17

I've recently installed Ubuntu 11.10 and along with it the CodeBlocks IDE and I am aware that I have gcc and the std libraries by default.

My questions are:

  • Do you you have any tips for a new C++ programmer on Ubuntu?
  • Any libraries I should get from the start?
  • A really good IDE I'm missing? (YMMV but I prefer to work in IDE's)
  • Any programming boons or traps I should be aware of from the start?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
George Bora
  • 1,618
  • 6
  • 26
  • 45
  • Eclipse is nice from what I have seen of it, and it has built in support for gdb. I don't really know that much about it. I use vim, gdb, and gcc on the command line. – Brandon Feb 06 '12 at 07:04

10 Answers10

46

You don't need an IDE to code in C or C++ on Ubuntu. You can use a good editor (like emacs, which you can configure to suit your needs.).

Some few tips for a newbie:

  1. Always compile with -Wall -Wextra and perhaps even with -Werror -pedantic-errors
  2. Order of arguments to the compiler (gcc or g++) are really important; I recommend:

    • general warnings and optimization flags (e.g. -Wall, -g to get debug info, -O, -flto etc, or -c to avoid linking , ...)
    • preprocessor options like -I include-dir and -D defined-symbol (or -H to understand which headers get included) etc..
    • source file[s] to compile like hello.c or world.cc
    • if you want to link existing object files else.o, add them after the source files
    • linker options (if relevant), notably -L library-dir (and probably -rdynamic if your program uses plugins with dlopen(3) ....)
    • libraries (like -lfoo -lbar from higher-level libraries like libfoo.so to lower-level libraries.
    • output file (i.e. produced executable), e.g. -o yourexec.
  3. Always correct your source code till you got no warning at all. Trust the compiler's warnings and error messages.

  4. Learn how to use make and to write simple Makefile-s; see this example.

    there are other builders, e.g. http://omake.metaprl.org/ etc

  5. Compile your code with the -g flag to have the compiler produce debugging information; only when you have debugged your program, ask the compiler to optimize (e.g. with -O1 or -O2), especially before benchmarking.
  6. Learn how to use gdb
  7. Use a version control system like svn or git (even for a homework assignment). In 2015 I recommend git over svn
  8. Backup your work.
  9. Learn to use valgrind to hunt memory leaks.

NB

The advices above are not specific to Ubuntu 11.10, they could apply to other Linux distributions and other Ubuntu versions.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Someone had to suggest `emacs`... `Makefile`s and learning to use `gdb` are great ideas. `-pedantic` is another flag I use when coding in C. – AusCBloke Oct 29 '11 at 07:57
  • 17
    Or, you could use a better editor, like `vi`. – Dave Oct 29 '11 at 10:11
  • 5
    @George Bora: there is a "fanatic" war between people using `Emacs` and people using `Vim`. It is really a matter of taste and habits. – Basile Starynkevitch Oct 29 '11 at 11:38
  • 1
    It's possible to run gdb from within emacs (and follow step by step your program at the source level). Also, git can be driven from emacs (I use e.g. "magit" for that). So emacs is almost an IDE! – rafak Oct 29 '11 at 15:19
  • 1
    Makefiles can quickly become horrible to manage when non trivial. It is worth knowing the basics of make, but there are alternatives (e.g. "scons", "redo", etc.) – rafak Oct 29 '11 at 15:22
  • Better yet is to use `-Wall -Wextra -pedantic` systematically. Also +0.5 for learning Make, it is a matter of one afternoon, and another +0.5 for git. – Alexandre C. May 27 '12 at 08:33
  • 2
    "You don't need an IDE to code in C or C++ on Ubuntu" - same applies for iOS developers but they don't realize it. +1 for all the good advices. –  Nov 06 '12 at 07:12
  • "You don't need an IDE to code in C or C++ on Ubuntu" - well, same applies for any other OS. also +1 for all the good advices – Hayri Uğur Koltuk Mar 09 '13 at 15:01
12

QT Creator is a good IDE, that works well also with simple Makefile based projects. Also, as a C++ programmer you should check out Dia and Dia2Code for automatic generation of stubs from UML diagrams.

onemasse
  • 6,514
  • 8
  • 32
  • 37
  • 1
    +1 because i like qt creator – Hayri Uğur Koltuk Mar 09 '13 at 15:02
  • I highly recommend QtCreator, aka, the Ubuntu SDK for C++ and of course, especially Qt. It has a great fakevim implementation that is about 95% compatible, and in some ways even extends the functionality of Vim by allowing you to easily register custom commands to perform IDE functions. – Anon Nov 05 '16 at 08:52
7

Since you ask more than one question I will answer each separately.

Do you you have any tips for a new C++ programmer on Ubuntu?

  • Learn some build system such as CMake or SCons. Although understanding how make and Makefiles work is useful there is a tendency of moving away from make to more high-level tools which also provide configure-like functionality. Make is often used for command-line build, for example with CMake you can generate Makefiles and build your projects using make.

  • Use a version control system such as git or Mercurial. I also recommend keeping those your projects you care about on some external service like github at least for the purposes of backup.

  • Pay attention to compiler warnings but keep in mind that warnings only catch a fraction of possible errors. A more complete picture can be obtained using static analysis tools and dynamic analysis tools like Valgrind.

Any libraries I should get from the start?

  • You've already got the main one which is called the C++ Standard Library. Make sure that you know what it provides.
  • Boost will cover most of the remaining needs except GUI.
  • Gtkmm and Qt are two major C++ GUI frameworks.

A really good IDE I'm missing? (YMMV but I prefer to work in IDE's)

  • Eclipse - for a long time I've been thinking of it as a Java only IDE, but in fact it is an excellent IDE for almost anything (I even wrote my PhD thesis in it using TeXlipse plugin) and C/C++ support is improving all the time. Also CMake can generate Eclipse CDT project files.

  • Qt Creator - another excellent C++ IDE. It is very fast and has native CMake support

Any programming boons or traps I should be aware of from the start?

  • From my experience the most common sources of errors in C++ are pointers and resource management in case of exceptions. Make sure you understand and use the RAII idiom and smart pointers.
  • For a more complete list of traps and recommendations see the answers to this question.
Community
  • 1
  • 1
vitaut
  • 49,672
  • 25
  • 199
  • 336
6

Some tips besides those which are already mentioned:

  1. Valgrind is your friend in finding memory leaks. You may also use valgrind --tool=callgrind and KCacheGrind to see where does your program spend time on execution.
  2. If you are going to distribute your program, you should learn autotools or cmake. The first is a classical tool, a bit bloated, the second is more modern.
  3. Geany is a nice IDE if you are looking for something lightweight. Otherwise, take a look at Code::Blocks, Eclipse/CDT and NetBeans.
  4. Since I am not sure what you meant by "std libraries", I should mention that besides standard C library, there are a lot of POSIX functions and interfaces, which are common to most *nix-systems, including Mac OS X.
Victor Vasiliev
  • 462
  • 2
  • 12
5

Eclipse/CDT runs really well on Ubuntu.

JesperE
  • 63,317
  • 21
  • 138
  • 197
4

Boost provide a whole bunch of libraries that are commonly used and can come in handy. Anyway, I'm not really sure this questions fits in too well on a Q&A board.

EDIT: As suggested by Basile, Makefiles and learning to use gdb are great ideas. There are plenty of neat flags to use with gcc also, for helping to debug your code, optimize it, produce assembly instructions, etc.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
2

I think Netbeans is good. Same UI in Microsoft Windows and Linux. Built-in version Controller and installed Git by default. No extra library added (as oposit of QT)

SuB
  • 83
  • 1
  • 4
2

On the first steps of programming you should not use IDE because you will better understand what happens backside :) GCC or G++ and stdlib will be sufficient. You also should read about Makefiles, SVN(CVS, GIT), Autotools or CMake to manage your projects. If you want make GUI applications you should learn GTK+ or Qt. If you want real IDE for your needs try Eclipse with C/C++ plugins. Good luck :)

selfsx
  • 581
  • 7
  • 23
2

If you are familiar with the command line you can use an editor like vim and gcc/g++ to compile your code, learning make svn git is also recommend.

In case you are not familiar with the command line or you prefer using the UI :NetBeans is also a good IDE you can use to develop in c/c++ and java.

To install netbeans: open firefox and point to apt://netbeans I hope this will help you.

ob_dev
  • 2,808
  • 1
  • 20
  • 26
1

Library: I recommend you to use Boost. You can find many libraries in it. IDE: Eclipse and QTCreator are good IDEs, but I think it is also very important to use text editor + makefile. Vim, Emacs or Sublime Text is good choice. Always remember to backup your code.

wyq10
  • 216
  • 2
  • 3