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

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
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?

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:
Always compile with -Wall -Wextra and perhaps even with -Werror -pedantic-errors
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.
Always correct your source code till you got no warning at all. Trust the compiler's warnings and error messages.
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
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.
Learn how to use gdb
Use a version control system like svn or git (even for a homework assignment). In 2015 I recommend git over svn
Backup your work.
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.

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.

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.

Some tips besides those which are already mentioned:
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.
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.
Geany is a nice IDE if you are looking for something lightweight. Otherwise, take a look at Code::Blocks, Eclipse/CDT and NetBeans.
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.

Eclipse/CDT runs really well on Ubuntu.

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.

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 :)

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.

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)

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.

Related

Better way to give provide path of libraries while compiling in C++

I pretty new to C++. I was wondering, as what is considered generally a neat way to provide paths for various files/libraries while compiling or executing c++ codes.
For ex:
I have Boost libraries installed in some location on my system. Lets call it X
In order to execute anything I have to type in
c++ -I LongpathWhichisX/to/boost_1_60_0 example.cpp -o example
Similarly, also Long path for the input file while executing the code.
Is there a better way to address it. Is it possible to create environment variables lets Y, which refers to path 'X'. And we can use following command to compile code
c++ -I Y/to/boost_1_60_0 example.cpp -o example
Best way is to use build tools. For example you can use Make. You define all your include paths (and other options) in the Makefile. In console you just have to call make to build your project or something like make run to run your project.
The usual way is to make a Makefile where you can specify all needed paths and compile options in proper variables.
If you don't want/need a Makefile and rather want to run compiler from command-line, then you may use the CPATH environment variable to specify a colon-separated list of paths to include files.
This is a broad question but the other answers highlight the most important step. It is essential to learn build tools like make because they will make it easier to build your projects during development and for others to build it later. In the modern programming age though this is not enough. If you are using something like Boost (which targets many platforms) you will probably want to make your build cross-platform as well. For this you would use either cmake or autotools which both have scripts that make it much easier to locate the Boost libraries (and others).
Any other build systems, in my opinion, are a pain and are the bane of maintainers of Linux distributions. CMake used to be in that catergory but it has gained wide acceptance now. CMake targets building cross-platform projects across operating systems (Windows and Unixes) better (again in my opinion) because it attempts to provide the native build system on each platform (for example: Visual Studio in Windows, Make on all Unices, XCode on Mac). The autotools instead target the Unix environment with much greater depth (you have a bit of a harder time on Windows, but you can target embedded Unix systems to high end Unix server systems with much more flexibility).
Note: Autotools support for cross-compiling is superior in almost every way to other solutions. I always cringe when I download something that needs to be cross compiled for Arm Linux and it uses some weird build system. Funnily enough, boost is one of these.
This is a bit of a long winded answer. In summary, it is essential that you learn a build system for native development. It is part of your skill set and until you have that skill you can't really contribute to open-source projects or even your employer developing closed-source projects.

Getting Started with Makefile for C++(CMake or GNUMake?)

I have got my first project for this semester and I have been asked to submit it with a makefile. The literature available on the internet is a bit overwhelming and combined with my laziness, I came to stackoverflow for simple answers. I have found this answer by Brendan Long as a good place to start with.
The example he gives is:
all: a3driver.o
g++ -o a3driver a3driver.o
a3driver.o: a3driver.cpp
g++ -c a3driver.cpp
which i understand. This looks exactly like the make files I have seen on a Unix system and which i used to compile c++ files(only used, did not need to understand).
Then i search further and an answer to this question suggests using CMake which is completely different from the code I have pasted above.
So my question at this stage is which direction should i take? Should I learn about the CMake or the GNUMake? I only intend to work on C++ files for now.
Only you can answer this question because it depends heavily on your needs. Cmake is a "build control file generator", not a build control program. It doesn't actually build code: instead it will create a makefile, or a Visual Studio / Xcode / Eclipse project file, etc. You then use that build program (make, Visual Studio, XCode, Eclipse) to actually build the code.
Cmake is good if you need to support all those different types of builds across all those different architectures using their native build environments. If you're happy to use make on whichever architecture you need to build on (GNU make runs on all of those as well and all those IDEs except possibly Visual Studio have good integration with native make) then using make directly is fine. GNU make has lots of advanced features which make it very flexible.
I don't really agree with esseks assessment of the autotools although I know it's a very common opinion. Also note that automake itself does not use unusual, verbose syntax: automake files are just makefiles. However they have to be processed, and autoconf is how that's done... autoconf is more obscure although not as bad as people make it out to be, depending on your needs. This isn't the place for that discussion however.
I personally find cmake format even more annoying and strange than autotools, and it doesn't meet my needs in many ways (for example it's support for cross-compilation is not as good as autotools'). However I have to say its ability to generate native project files is really excellent--if you need it.
If you need a really really dead simple makefile for compiling one or few files only, then you are done with:
compile:
g++ myprogram.cpp -o myprogram
(note that lines must be indented with tab, not spaces).
If you need flexibility, you are on the right path with CMake. I suggest you to explore CMake, starting from their good tutorial or a simple example -- as the basics are simpler to undestand from code rather than learn from manual.
My personal opionion is to avoid GNU Automake (colloquially known as Autohell) because of the unusual, verbose syntax that sometimes scares beginners and tricks more experienced users.
EDIT: CMake is not used to compile, rather, it can generate makefiles for you, starting from a synthetic description of the project (where are the files to be compiled? What libraries are required? etc.). And it does this by checking for libraries, identifying compiler and carrying out other sanity check you would need to code by yourself otherwise.

How to export a C program that compiles from command line in Linux to an IDE?

There are a lot of open source C/C++ projects, most of them can be compiled using "make" in Linux. Is there any easy way to export this to an IDE, for example Microsoft Visual C++, CodeBlock, or Eclipse?
Even if it is an involved work, is there any step by step help for doing that?
Thanks for the help.
TJ
Considering that most (if not all) of the IDEs support importing a makefile or cmake based project, that should be a no-brainer.
Be careful about configure scripts though, as they generally do important stuff not supported automatically by an IDE.
In fact, build systems like cmake, premake and scons were invented because there was no standard way for an IDE to know what a script is doing. (That, while sacrificing flexibility, makes the system standard.)

What GNU make substitute do you recommend? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Imagine you're free to choose a tool like GNU make for a new C++ project. What would you choose? Are any usable substitutes out there?
It shall have/be
a command line interface
"easy" to understand
easy to set up for a default c++ project
may support src/bin seperation as common for Java
may not add too much dependencies to other software/libs
platform independent (new)
features:
build rules / templates like make but in an human readable way
recursively crawling directories and applying the rules if there is no other
"Makefile"
configuration by exception
Note:
Nothing's wrong with GNU make. I just don't like its grammar, all the stuff that grows in the years and the silly recursive make problems. I'm using gmake for years now.
I use cmake, and I'm very glad I made the switch.
EDIT
Feature list as found in the wikipedia article:
Configuration files are CMake scripts, which use a programming
language specialized to software builds
Automatic dependency analysis built-in for C, C++, Fortran and Java
Support of SWIG, Qt, via the CMake scripting language
Built-in support for many versions of Microsoft Visual Studio including versions 6, 7, 7.1, 8.0, and 9.0
Generates build files for Eclipse CDT (C/C++ Development Tools)
Detection of file content changes using traditional timestamps,
Support for parallel builds
Cross-compilation
Global view of all dependencies, using CMake to output a graphviz diagram
Support for cross-platform builds, and known to work on
Linux and other POSIX systems (including AIX, *BSD systems, HP-UX, IRIX/SGI, and Solaris)
Mac OS X
Windows 95/98/NT/2000/XP, Windows Vista and MinGW/MSYS
Integrated with DART (software), CDash, CTest and CPack, a collection of tools for software testing and release
But to be honest: Just about anything is better than the gnu toolchain.
SCons and waf are good (well, IMHO anyway) build systems, and depend only on Python.
How about "gnu make"?
You asked for something like it without giving any indication of what features you want that aren't supported by gnu make.
Boost.Jam
It has the features you named
command line interface;
easy;
it comes from the C++ library collection Boost, so it has good support for C++ (and it's not limited to C++, either);
it stores executables in places under the bin directory, depending on what build request you've commanded. If you use gcc 4.3.2, than you get the executables under
bin/gcc-4.3.2/debug -- when executing bjam
bin/gcc-4.3.2/release -- when executing bjam release
bin/gcc-4.3.2/release/inlining-off/threading-multi -- when executing bjam release inlining=off threading=multi
bin/gcc-4.3.2/release/threading-multi -- for bjam release inlining=full threading=multi because inlining=full is default for release.
it doesn't need the full Boost library collection, only Boost.Build and Boost.Jam are necessary;
platform independent;
the Jamfile syntax is easy, but powerful;
you can divide the build config into many Jamfiles in subdirectories.
CMake should answer most, if not all for your requirements.
It will generate the Makefiles for you.
It has a good domain specific primitives, plus a simple language for the times you need to do something special.
It solves most of the problems with recursive make (see recursive make is considered harmful paper).
It uses an out-of-source build, so you have your bin / src separation.
I found it easy to write, easy to maintain, and fast to build.
... Plus:
It is cross platform.
With CText and CDash it has what you need for setup a continues integration service.
See also this answer to Recursive Make - friend or foe?
SCons + swtoolkit
G'day,
I'd agree with the couple of answers, so far, that recommend sticking with gmake.
Maybe have another look after reading the first few chapters of Robert Mecklenburg's excellent book "Managing Projects with GNU Make" (sanitised Amazon link).
Or, even better, is to search out a copy of the previous edition called "Managing Projects With make" by Andrew Oram and Steve Talbott. The first few chapters give an excellent description of (g)make and [Mm]akefile basics.
And I see you can buy a second hand copy of the 2nd ed. from Amazon for the princely sum of $0.01! (sanitised Amazon link)
After reading that intro you'll even understand how make is backward chaining, which is a bit non-intuitive when just looking at make's behaviour.
HTH
cheers,
Autotools -- autoconf/automake/libtool they are very poweful build instruments.
Its take some time to start with them, but after -- they serv you very well.
And what is more important they are significantly more powerfull then their replacements (CMake, BJam, SCons etc).
How are they more powerfull?
Transparrent support of building both static and shared libraries via libtool.
Support of uninstall (no in CMake)
Support of standard installation paths -- very important in packaging.
Full support and integration of gettext.
Transparent and strightforward support of cross compilation.
Many things. CMake may do most of things but for each one of them you should write long scripts or specify many things manually.
What's wrong with gmake?
What issues does it have that mean you want to use it. There's no point in recommending another tool if it has the same perceived issues as gmake.
we're using gmake in our build system and we're extremely happy with it's performance, flexibility and features

Help on Porting a SIP library to PSP

I'm currently trying to port a SIP stack library (pjSIP) to the PSP Console (using the PSPSDK toolchain), but I'm having too much trouble with the makefiles (making the proper changes and solving linking issues).
Does anyone know a good text, book or something to get some insight on porting libraries?
The only documentation this project offers on porting seems too dedicated to major OS's.
Look at other libraries that were ported over to the PSP. Doing diffs between a linux version of a library, and a PSP version should show you.
Also, try to get to know how POSIX compatible the PSP is, that will tell you how big the job of porting the library over is.
The PSP is not UNIX and is not POSIX compliant, however the open source toolchain is composed by gcc 4.3, bintutils 1.16.1 and newlib 1.16.
Most of the C library is already present and can compile most of your code. Lots of libraries have been ported just by invoking the configure script with the following arguments:
LDFLAGS="-L$(psp-config --pspsdk-path)/lib -lc -lpspuser" ./configure --host psp --prefix=$(pwd)/../target/psp
However you might need to patch your configure and configure.ac scripts to know the host mips allegrex (the PSP cpu), to do that you search for a mips*--) line and clone it to the allegrex like:
mips*-*-*)
noconfigdirs="$noconfigdirs target-libgloss"
;;
mipsallegrex*-*-*)
noconfigdirs="$noconfigdirs target-libgloss"
;;
Then you run the make command and hope that newlib has all you need, if it doesn't then you just need to create alternatives to the functions you are missing.
Porting is very platform specific, so I don't think you will find much general literature on the subject.
Off the top of my mind, some things you may encounter:
endianness
word size
available libraries
compiler differences
linker differences (you've already seen that one)
peripheral hardware differences
...
I did some more research and found this post at ps2dev forum:
The PSP is not a Unix system, and the pspsdk is not POSIX compliant. It's close in some places, but you can't expect to just take any code that compiles fine on a POSIX system and have it work. For example:
pspsdk uses newlib, which lacks some of glibc's features and headers.
libc is not linked by default, so typical autoconf tests will fail to build
autoconf knows nothing about the PSP
defining PSP_MODULE_INFO and running psp-fixup-imports on the executable are required, otherwise it won't run
You should look at all of the other libraries and programs that have been ported (in the psp and pspware repositories). All SDL libs use autoconf, for example.
I think this delivers more detail into what I was looking for, and also show the #[Jonathan Arkell] point of looking into libraries that already been ported.
Thanks for your replies.