Wednesday, 28 March 2018

Chapter 8 // Drill 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Drill 1



1. Create three files: my.h, my.cpp, and use.cpp. The header file my.h contains:

extern int foo;
void print_foo();
void print(int);

The source code file, my.cpp #includes my.h and std_lib_facilities.h, defines print_foo() to print the value of foo using cout, and print(int i) to print the value of i using cout.

The source code file use.cpp #includes my.h, defines main() to set the value of foo to 7 and print it using print_foo(), and to print the value of 99 using print(). Note that use.cpp does not #include std_lib_facilities.h as it doesn't directly use any those facilities.

Get these files compiled and run. On Windows, you need to have both use.cpp and my.cpp in a project and use { char cc; cin >> cc} in use.cpp to be able to see your output. Hint: You need to #include <iostream> to use cin.


//my.h
#pragma once
//my.h

//declarations
extern int foo;
void print_foo();
void print(int);

void keepWindowOpen();

//my.cpp

//my.cpp

#include "my.h"
#include "std_lib_facilities.h"

int foo;

//print the value of foo
void print_foo()
{
 cout << foo << endl;
}

//print a given integer
void print(int i)
{
 cout << i << endl;
}

//function to keep the window open
void keepWindowOpen()
{
 char c;
 cout << "\nPress any key to quit: ";
 cin >> c;
}


//use.cpp

//use.cpp

#include "my.h"

int main()
{
 //assign 7 to foo
 foo = 7;

 //print what's in foo
 print_foo();

 //send an integer to be printed to screen
 print(99);

 //keep window open until any key press
 keepWindowOpen();
}


A pretty simple exercise which showcases how to split a project up into separate files. Seeing as how you can't use keep_window_open() from std_lib_facilities I created a function in my.cpp which does the same thing and then declared it in my.h so it could be used in main(). This way I didn't have to #include <iostream> in use.cpp as it was already declared in my.cpp from the other header file.

7 comments:

  1. Replies
    1. haha not quite, but thanks. Glad you find these posts useful!

      Delete
  2. i have to include my.cpp also in use.cpp otherwise it gives error - undefined reference to print_foo(). !!!

    ReplyDelete
    Replies
    1. Directly including cpp files is not considered good practice in c++ although it does work. The only thing I can think of that would cause that error is not defining print_foo() in my.cpp. What was your actual error in the build log?

      Delete
  3. This was the actual error-> - undefined reference to print_foo() . actually how does my.h connects to my.cpp?
    There are no references of my.cpp in my.h. i am using codeblocks. Right now going to start chapter 9, and your blog is very helpful in it. Thanks

    ReplyDelete
    Replies
    1. Ah that sounds like the actual function body was not in my.cpp. Here is a github link to the project:
      https://github.com/l-paz91/principles-practice/tree/master/Chapter%208/Drill%201
      It's much clearer than above. I used to have code formatting on the blog but it made it extremely slow to load.

      If you have the definition of print_foo() in my.cpp it could be an issue with codeblocks not realising that my.cpp is a part of the project. In Visual Studio you just add a file but I'm not sure how it works in other IDE's.

      my.h doesn't connect to my.cpp in any way, the file could be name otherFile.cpp. my.cpp just needs to know about my.h by including it at the top.

      Delete
  4. Hey there! Thank you so much for this information. It pointed me in the right direction. Here are my two cents (this is how i resolved it).

    OS: Ubuntu 20
    IDE: VS Code
    Compiler: G++
    Diretory:
    Drill
    Drill/headers
    Drill/headers/my.h
    Drill/my.cpp
    Drill/use.cpp

    Command that i use to inform the compiler WHERE is my header files:

    g++ -I/headers use.cpp my.cpp -o useDrill

    Hope it helps somebody.

    Have a great week!

    ReplyDelete