What are the gcc system include paths?

The #include preprocessor directive in c++ is one of the first things that people learn. They come in two varieties:

  1. #include<> – usually meant for system-level includes such as iostream or other headers from libraries installed at the system level.
  2. #include " " – usually meant for files included from a location relative to the code being written… for instance, another header file for a class you just wrote.

But, where are system level headers stored?  On Linux with the gcc tool chain installed, you can execute the following command to find out:

g++ -E -x c++ - -v < /dev/null

  • -E – Stop after the pre-processing step
  • -x c++ – language of interest is c++
  • -v – verbose output

In the output, look for the section that starts with #include <...> search starts here.  A collection of paths is listed after this line which is where the pre-processor will look for any includes that are in <...> (angle brackets).

FWIW, there’s also a way to include additional paths to be used as system includes on the command line when compiling code.

Customizing CLion

Clion LogoSome folks get a lot of enjoyment out of tweaking the UI of a piece of software.  In an IDE like CLion, the color scheme used for the code editor is highly customizable.  If you’d like to explore the settings and/or find a new theme for CLion or just the code editor, check out the CLion JetBrains tutorial at https://www.jetbrains.com/help/clion. Specifically, here are some tutorials you might want to look at:

Linux, C++, and Libraries like myHTML

Whenever you write c++ code for a project, have you ever wondered where the actual implementations for iostream’s << and >> operators are?  What about the implementations for all of the algorithms that are in the algorithm header? Whenever you’re building a project and linking in things from external libraries, where’s the compiled version of those functions?  The secret is in libraries of code external to your project. [Read more…]

Testing More than One Thing with Catch

In a modern piece of software, you wouldn’t have tests for just one class.  You’d have exhaustive tests for all the functionality in your program.  However, it would be challenging to put all of the tests in one single tests.cpp file.   [Read more…]

Binary Trees

Binary trees are a very fundamental data structure in computer science.  As you continue to learn and explore in different sub-domains of CS, you’ll see them pop up quite frequently.  Here are some things you should Binary Trees and their cousins.

  • General Binary Trees
    • Pre -, In -, and Post-order traversals
    • The height (or depth) of a tree
    • Different node terminology (leaf, level, ancestor, descendant, etc.)
    • Some info to peruse
  • Binary Search Trees
    • Remember, binary search trees are binary trees that also conform to the binary search property: all values in the left subtree of a node are smaller and all values in the right subtree of a node are larger (duplicates not withstanding)
    • Some algorithms you should think about w.r.t. bin search trees:
      • inserting a new value
      • searching for a value
      • deleting a value from the tree
      • determining the height of the tree
      • determining if a binary tree is indeed a binary search tree
      • what’s the most efficient way to create a copy of a binary search tree?
      • what’s the best way to destroy (delete all nodes) a binary search tree?
    • Some info to peruse
      • David Eck link above
      • Cliff Shaffer’s Data Structures and Algorithms book linked above Section 5.4 starting on page 168.
  • AVL Tree – a Self Balancing Binary Search Tree
    • AVL Balance Property: for every node n in an AVL tree, the height of the left subtree and the height of the right subtree may differ by no more than 1.
    • Some info to peruse

Lists, Stacks, and Queues

Lists, stacks, and queues are some of the most fundamental data structures to computer science.  Below are some links to information you may find helpful as you explore these data structures:

There are plenty of videos on Youtube about these topics as well. Check them out.

C++ and Catch – Adding your Own Main Method

When you begin coding on a project, it is perfectly acceptable and even advisable to allow the Catch library to generate the main method for you.  That is what the #define CATCH_CONFIG_MAIN (very first line in the tests.cpp file)  directive tells Catch to do.

As you transition from implementing the data structures to implementing a higher-level project, you will want to eventually create your own main method.  Here is how to transition to using your own main without getting rid of tests and testing.

In QtCreator, follow these steps

  1. Add a new cpp file to your project that will contain your main driver.  If you still have the original main.cpp that was added when you created the project, that is fine to use as well; make sure it is listed in the project explorer on the left side of the code window.
  2. Comment out#define CATCH_CONFIG_MAIN at the top of the tests.cpp file.  This will tell the Catch library NOT to generate its own main method.
  3. In your main driver file, copy and paste the following code (to start with). Read the comments throughout to help you understand what is going on.
//CATCH_CONFIG_RUNNER tells the catch library that this 
//project will now explicitly call for the tests to be run. 
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

//A macro used in main to determine if you want to run
//the tests or not. If you don't want to run your tests,
//change true to false in the line below.
#define TEST true

/*
* runCatchTests will cause Catch to go ahead and
* run your tests (that are contained in the tests.cpp file.
* to do that, it needs access to the command line
* args - argc and argv. It returns an integer that
* ultimately gets passed back up to the operating system.
* See the if statement at the top of main for
* a better overview.
*/
int runCatchTests(int argc, char* const argv[])
{
    //This line of code causes the Catch library to 
    //run the tests in the project. 
    return Catch::Session().run(argc, argv);
}

int main( int argc, char* const argv[] )
{
    //If the TEST macro is defined to be true,
    //runCatchTests will be called and immediately
    //return causing the program to terminate. Change TEST
    //to false in the macro def at the top of this file
    //to skip tests and run the rest of your code.
    if (TEST)
    {
        return runCatchTests(argc, argv);
    }

    //start working on other parts of your project here.
    return 0;
}

Once you’ve added that code, rebuild your project (Build menu| Rebuild All) then execute your project.  Your tests should run as normal.

Let’s Review Pointers

Pointers cause a lot of heartburn among students.  Hopefully this post will address some or all of the things you may be struggling with in the world of pointers.

For pointers to make sense, particularly the parts of them that are important for this class, you need to remember a few fundamental pieces of information:

  • Each location where data can be stored has an address.
    • Analogy: Every post office box in Hughes Trigg has an individual address.  If each didn’t, then the workers wouldn’t know what mail goes in which box.
    • Note that even pointers have addresses.  So, if I have [Read more…]