Holidays and Hobbies
  • Home
  • Brasil
  • Rome
  • Paris
  • Fabio
    • Data Science Blog
    • Music
    • Rubiks Cube
    • Astronomy
    • Art
    • Unicycle
    • English Pope
    • Flying
    • If I Ruled The World
    • Fusion
  • Local
  • Tenby
  • Keswick
  • Mark
  • Store
  • Contact
  • cplusplus

Using headers .h and implementation files

8/28/2018

 
A typical object oriented program will have:
  1.     myfirst.h - the header include file
  2.     myfirstlib.cpp  - the implementation, compiled to myfirstlib.o
  3.     myfirst - the compiled (and linked program)
Example:

// This will be #include "myfirst.h"
class myfirstClass {
public:
    int a_number;
    void my_func01();
};

// This would be in the myfirstlib.o library, the implementation
void myfirstClass::my_func01() {
    std::cout << "This is in my_func01" << std::endl;
}

// And this of course the main body
#include <iostream>
int main() {
    myfirstClass mine;
    mine.my_func01();

}



The Makefile when you have headers and libraries

So the Makefile would need entries for this like so:

bin/running :    bin/myfirst.o    bin/myfirstlib.o
    $(CC) -o bin/myfirst  bin/myfirst.o bin/myfirstlib.o

bin/myfirstlib.o : src/myfirstlib.cpp  include/myfirst.h
    $(CC) -c -o bin/myfirstlib.o   src/myfirstlib.cpp
bin/myfirst.o : src/myfirst.cpp include/myfirst.h
    $(CC) -c -o bin/myfirst.o src/myfirst.cpp





Makefile

8/27/2018

 
We want to get organised on the command line. We want two directories in our toplevel LearningCpp directory:
    bin  src
the bin directory has the binaries, the src directory the source files.
In that same directory we want a Makefile.

So to recap, the Makefile is the rules to compile our source programs into binary files.
With this Makefile we can type:
    make  # which will compile any programs which need compiling - their source code has changed
    make clean # delete the binary programs
    make myfirstprog # target particular programs to build
In the Makefile we have heavy use of variables so we can have long term flexibility e.g.
CC = c++  # This sets the variable CC to the compiler program c++ and we use it as $(CC)

Now there are lots of fancy aspects to Makefiles so you have to look them up here e.g.
PATHSOURCES = $(wildcard src/*.cpp) #  will expand src/*.cpp so provide a full list of all source files

Our Makefile

# Declaration of variables
# the compiler I have on the MAC which is called clang++ but is effectibely
# g++ (Gnu c++)
CC = c++
FUSSY = -Wall

# Been finding this was needed for code examples so left it in
CC_FLAGS =  -Wc++11-extensions

# if we are using maths functions which is common, so left it in
LD_FLAGS = -lm

# keep my .ccp sources files in one place
SRCDIR = src

# put the binaries in another place
BINDIR = bin

# might as well compile all with -g debug option
DEBUG=-g

PATHSOURCES = $(wildcard src/*.cpp)

#substitue src/ for ""  from $(PATHSOURCES)
SOURCES = $(subst src/,,$(PATHSOURCES))

#$(var:pattern=replacement)
PATHOBJECTS1 = $(subst src,bin,$(PATHSOURCES))
PATHOBJECTS = $(subst .cpp,,$(PATHOBJECTS1))
OBJECTS= $(SOURCES:.cpp=)


# default is to make every object "make"
all::   $(PATHOBJECTS).  

# just to see what $(OBEJECTS) is set to "make objects"
show-objects:
                echo $(PATHOBJECTS)

# just to see what $(SOURCES) is set to "make sources"
show-sources:
                echo $(PATHSOURCES)

#The rule to make a bin/%
$(BINDIR)/%: $(SRCDIR)/%.cpp
        $(CC) $(FUSSY) $(DEBUG) $(CC_FLAGS) $<  -o $@ $(LD_FLAGS)

# "make clean" delete all your binaries
clean:
        rm -f  $(PATHOBJECTS)

%: $(SRCDIR)/%.cpp
        $(CC) $(FUSSY) $(DEBUG) $(CC_FLAGS) $<  -o $(BINDIR)/$@ $(LD_FLAGS)


With <ompose><b> now using Eclipse, that effectively runs "make all" and so if you have edited a program then this correctly detects the change. (You will need to hit the save icon first.)
Picture
Or, if you are working on the command line (the terminal shell) you can use vim to edit your source program and then type make to compile the program. Two ways of doing the same thing.

Using Eclipse as an IDE for C++

8/27/2018

 

What  Eclipse is for


  • Eclipse can format  our code
  • it will verify  syntax
  • we will use it to  build or compile our C++ application
  • we are to create a run configuration (how to compile and run) so we can run our prograns
  • we are to create debug configurations (how to debug)  so we can debug

Some Preferences:
One thing we must do is set the path to the debugger in preferences  to the full path so /usr/local/bin/gdb and not gdb.

Creating an Eclipse Project

We want to get used to the idea of using both the Terminal shell and Makefiles as well as Eclipse. So we are going to create an Eclipse Project based on the existing Code (and Makefile). Use the Wizard in the manner below:
Picture
Import your code and Makefile
Picture

Create a source file using vim or using Eclipse:

Picture
Picture

Compiling/Building your program with Eclipse

To compile with Eclipse we  build. After a sucessful buid we  are ready to Run our prrogram.
Picture

Run the program / Execute the program

Picture


To compile we build which will refer to our Makefile.

C++ On a Mac

8/26/2018

 

The Mac Environment

So we are using macOS 10.13.6 (High Sierra).
We tried Xcode and found it was so complicated and too set up for iphone/ipad/apple apps as to get in the way. We have switched to Eclipse.
So, our envirnoment is:
  • Compiler : c++ that comes with the macOS (clang++)
  • IDE: Eclipse (photon)
  • Debugger: gdb
  • Java 8 JDK (is required for Eclipse)

We are also going to have a second environment using the Terminal shell (bash) and use a Makefile. The idea being we can get use the command line and IDE.


Notes on Gnu Debugger (gdb) on Higher Sierra

So we had to  install  gdb 8.01 using brew.

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/c3128a5c335bd2fa75ffba9d721e9910134e4644/Formula/gdb.rb

Essentially gdb needs signing or else the debugger won't work.
We followed these  helpful pages:
  • https://danisfermi.github.io/tech/2018/01/16/setting-up-gdb.html
  • https://www.thomasvitale.com/how-to-setup-gdb-and-eclipse-to-debug-c-files-on-macos-sierra/


Our First Progam


#include <iostream>
using namespace std;
int main(){
  cout << "Hello" << endl;
}

# From your Terminal
cd
mkdir LearningCpp
cd LearningCpp
vim myfirstprog.cpp
i
:wq


So we are using the editor vim in bash (terminal shell). You might want to look at a UNIX command line cheet sheet to brush up.

And we compile by typing:
 make myfirstprog
Which runs:
c++  myfirstprog.cpp -o myfirstprog

See make , but with no file called Makefile present to take detailed instructions, make  looks for a relevant source files and compiles it.

First Look at gdb


We have to compile like this  with "-g" so :
    cc -g myfirstprog.cpp -o myfirstprog
and so we create our first Makefile

DEBUG="-g"
myfirstprog:     myfirsrprog.cpp
        c++    $(DEBUG).   -o    $<.   -o   $@


Notes:
  • $< is the dependency "myfirstprog.cpp"
  • $@ is the output of the compilation

    Author

    A look at C++ From scratch.

    RSS Feed

    Archives

    August 2018

    Categories

    All

    RSS Feed