Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C and C++ programming on Ubuntu 11.10 [closed]

Tags:

c++

c

linux

ubuntu

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?
like image 312
George Bora Avatar asked Oct 29 '11 07:10

George Bora


1 Answers

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.

like image 82
Basile Starynkevitch Avatar answered Sep 22 '22 00:09

Basile Starynkevitch