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:


Brief Explanation of the code structure I use in all git repos
After you issue a git clone to get a local repo of on of our code projects, the usual directory structure is:
./bin -   these are  a set of help scripts you can use.   Cp the contents to ~/bin  and modify ~/.bashrc to extend the environment PATH variable with ~/bin.
./bashScripts:   More advanced scripts
./code : Usually I have 3 subdir's:
    cCode CPPCode Python

in cCode there will be a number of specific programs and a directory ./commonCode.  Each program sub directory has a soft link to commonCode.
Example:   in  cCode/App1  you will find a Makefile, C source, an possibly helper scripts and a readme.txt. You should also see ./commonCode
                The Makefile has the correct variable set to tell it to look for source in the current directory and in ./commCode

                 When making a new APP that conforms to our system,  
                            1.Cp and existing Makefile  and modify the program targets and src code
                           2.  Notice the Makefile inserts  ./commonCode/Make.defines   - the latter attempts to set the build environment to be correct
                                  on Linux,  Darwin, and freeBSD.
                            3. Issues   'ln -s  ../commonCode   commonCode'  to create the soft link to the single directory commonCode

           For CPP APPs,  all of the above applies.    Make the softlink   : ln -s ../commonCPPCode commonCPPCode'

 ./Matlab - a number of helpful matlab scripts that can be used to analyze results of certain applicatoins

 

Review of C and C++:

 

 GNU compiler home page


C++ standards

 

GCC Versions

The various GCC versions are:

C++ Standard Support

There are various C++ standards:

The default mode is C++98 for GCC versions prior to 6.1, and C++14 for GCC 6.1 and above. You can use command-line flag -std to explicitly specify the C++ standard. For example,


Issue 'gcc --version'    on 18.04 shows 7.5 which means C++14 is the default.
Issue 'gcc --version'    on 16.04 shows  5.4  which means C++98 is the default.

Review of Makefiles

As we tend to use GNU tools, here are links:

 Example Makefiles:

If ever need to specify a specific gcc version, or to specify 32 or 64 bits:


Misc. Topics

 

Very misc. topics:

 

Misc. notes:



  enum class  RNGType {
    None=0,
    UniformRNG=1,
    ExponentialRNG=2,
    GaussianRNG=3,
    ParetoRNG=4
  };



For int64_t type:

#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);

for uint64_t type:

#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);

you can also use PRIx64 to print in hexadecimal.

cppreference.com has a full listing of available macros for all types including intptr_t (PRIxPTR). There are separate macros for scanf, like SCNd64.


A typical definition of PRIu16 would be "hu", so implicit string-constant concatenation happens at compile time.

For your code to be fully portable, you must use PRId32 and so on for printing int32_t, and "%d" or similar for printing int.




Handout on Linux Programming

 

Last update 9-30-2021