Programming - C and C++
**********************************************************************
This is the top level page that serves to provide material for
review of C and C++. Note that this material is based on material
taught by Clemson SoC faculty in the past.
We begin with a brief intro to programming in a Unix/Linux
Environment. The following summarizes the main ideas:
- When you write a program in C/C++ (let's assume we used gcc),
our program has access to libraries from the compiler.
These translate to the standard libraries defined by the C/C++
language.
- Some of the library routines run within user space (as opposed
to having to switch to the kernel to run the
routine). For example, a "C" function such as
strlen() runs synchronously on the process or thread of your
program.
- Some of the routines however require OS functions, such as the
"C" library calls, fprintf(). These are referred to
as system calls. The compiler provides a C/C++ wrapper but
underneath the code must issue a syscall() which is an
application's entry to kernel system calls.
- Issue a 'man syscall' and 'man
syscalls'.
- Your program might need to access a routine that is
located NOT in the C/C++ libraries, but in some other library
that either is provided when the compiler/build environment was
created (or provided by the OS distribution), or you might
need to download the library if it is not on your system.
Examples that are considered part of the compiler environment
: math library. An example of a library separate
from the compiler, would be the sockets library or a
widely used library for providing geo location coordinates,
gpsd.
- An example of a program that issues a syscall gettid() (to get
a threads id) is shown below. This is an example of
a Linux system call that does not have a wrapper in the GCC
library.
Review of C and C++:
- Review of "C" programming concepts
- Review of "C++" programming concepts
GNU compiler home page
C++ standards
- The ISO (a specific group in the ISO referred to as isocpp) leads the effort to
maintain and evolve the C++ standards
- The official current versions of the C++ standard is C++-2017
- This is a good visual
of where things stand related to versions. (status)
- On Ubuntu, we use GNU g++ - see this page
for their standards support
- To find the exact g++ version: issue : g++ --version
- This appears to work on ubuntu 16.04 -
- g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
- And this returns with : #define __cplusplus 199711L
which I assume means c++-2011.
- Recommended to use the most stable standard that works on a
platforms
- sometimes this is a problem...example raspberry pi's -
currently ships with Raspberian gcc v 4.6
- See
this article
- toInstall gcc 4.8 (or later, I believe 4.9 is
out as well, though I didn't try it)
- sudo apt-get install gcc-4.8 g++-4.8
- Then, you can remove gcc version 4.6, as it is
no longer needed.
- sudo apt-get remove gcc-4.6 g++-4.6
- How to tell what iso standard a compiler supports?
- specify -ansi : in C this is -std=c90, iin C++, this is
-std=c++98
- Recommendation: use -std=c++11 (eventually move to -std=c++17
)
- How to build a program in 32 bit mode?
Review of Makefiles
As we tend to use GNU tools, here are links:
Misc. Topics
Very misc. topics:
- files named as .cc or .cpp ? The convention at this point
seems to be cpp. There should be know difference if you use cc
or cpp naming conventions. Confusion occurs if we see within a
makefile a 'CC' or 'CXX' - The GNU implicit rule is 'CC'
identifies the C preprocessor and CXX identifies the C++
compiler (g++ by default)
- Use of std in a C++ context: implies or refers to the standard
library. This is a namespace that provides access to the
standard library for C++ programs.
- If you issue cout, the compiler searches the
global namespace. If you use std::cout the compiler
searches the std namespace (and finds it!).
- Arguably, the best approach is to issue 'use namespace
std' at the top of your cpp file. This adds the std
namespace to the global namespace
- Online sources of C++ help:
- Additional sources of information that is high level but
considered very useful
Last update 3/15/2019