Commit 9c7f8e14 authored by Vicente Mataix Ferrándiz's avatar Vicente Mataix Ferrándiz Committed by GILLES Sebastien
Browse files

Adding direct compilation 1

parent 0dd0ff24
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# [Getting started in C++](./) - [C++ in a real environment](/notebooks/6-InRealEnvironment/0-main.ipynb) - [File structure in a C++ program](/notebooks/6-InRealEnvironment/2-FileStructure.ipynb) # [Getting started in C++](./) - [C++ in a real environment](/notebooks/6-InRealEnvironment/0-main.ipynb) - [File structure in a C++ program](/notebooks/6-InRealEnvironment/2-FileStructure.ipynb)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1> <h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#Library-and-program" data-toc-modified-id="Library-and-program-1">Library and program</a></span><ul class="toc-item"><li><span><a href="#Static-and-shared-libraries" data-toc-modified-id="Static-and-shared-libraries-1.1">Static and shared libraries</a></span></li></ul></li><li><span><a href="#Source-file" data-toc-modified-id="Source-file-2">Source file</a></span><ul class="toc-item"><li><span><a href="#Compilation-of-Hello-world!" data-toc-modified-id="Compilation-of-Hello-world!-2.1">Compilation of <em>Hello world!</em></a></span></li><li><span><a href="#Source-files-extensions" data-toc-modified-id="Source-files-extensions-2.2">Source files extensions</a></span></li><li><span><a href="#Expanding-our-hello-program-with-two-source-files:-one-for-main,-one-for-the-function" data-toc-modified-id="Expanding-our-hello-program-with-two-source-files:-one-for-main,-one-for-the-function-2.3">Expanding our hello program with two source files: one for main, one for the function</a></span></li></ul></li><li><span><a href="#Header-file" data-toc-modified-id="Header-file-3">Header file</a></span><ul class="toc-item"><li><span><a href="#Header-location" data-toc-modified-id="Header-location-3.1">Header location</a></span></li><li><span><a href="#&quot;&quot;--or-<>?" data-toc-modified-id="&quot;&quot;--or-<>?-3.2"><code>""</code> or <code>&lt;&gt;</code>?</a></span></li><li><span><a href="#Header-guards" data-toc-modified-id="Header-guards-3.3">Header guards</a></span></li><li><span><a href="#Header-files-extensions" data-toc-modified-id="Header-files-extensions-3.4">Header files extensions</a></span><ul class="toc-item"><li><span><a href="#My-personal-convention" data-toc-modified-id="My-personal-convention-3.4.1">My personal convention</a></span></li></ul></li></ul></li><li><span><a href="#Why-a-build-system:-very-basic-CMake-demonstration" data-toc-modified-id="Why-a-build-system:-very-basic-CMake-demonstration-4">Why a build system: very basic CMake demonstration</a></span></li><li><span><a href="#Where-should-the-headers-be-included?" data-toc-modified-id="Where-should-the-headers-be-included?-5">Where should the headers be included?</a></span></li><li><span><a href="#Forward-declaration" data-toc-modified-id="Forward-declaration-6">Forward declaration</a></span></li></ul></div> <div class="toc"><ul class="toc-item"><li><span><a href="#Library-and-program" data-toc-modified-id="Library-and-program-1">Library and program</a></span><ul class="toc-item"><li><span><a href="#Static-and-shared-libraries" data-toc-modified-id="Static-and-shared-libraries-1.1">Static and shared libraries</a></span></li></ul></li><li><span><a href="#Source-file" data-toc-modified-id="Source-file-2">Source file</a></span><ul class="toc-item"><li><span><a href="#Compilation-of-Hello-world!" data-toc-modified-id="Compilation-of-Hello-world!-2.1">Compilation of <em>Hello world!</em></a></span></li><li><span><a href="#Source-files-extensions" data-toc-modified-id="Source-files-extensions-2.2">Source files extensions</a></span></li><li><span><a href="#Expanding-our-hello-program-with-two-source-files:-one-for-main,-one-for-the-function" data-toc-modified-id="Expanding-our-hello-program-with-two-source-files:-one-for-main,-one-for-the-function-2.3">Expanding our hello program with two source files: one for main, one for the function</a></span></li></ul></li><li><span><a href="#Header-file" data-toc-modified-id="Header-file-3">Header file</a></span><ul class="toc-item"><li><span><a href="#Header-location" data-toc-modified-id="Header-location-3.1">Header location</a></span></li><li><span><a href="#&quot;&quot;--or-<>?" data-toc-modified-id="&quot;&quot;--or-<>?-3.2"><code>""</code> or <code>&lt;&gt;</code>?</a></span></li><li><span><a href="#Header-guards" data-toc-modified-id="Header-guards-3.3">Header guards</a></span></li><li><span><a href="#Header-files-extensions" data-toc-modified-id="Header-files-extensions-3.4">Header files extensions</a></span><ul class="toc-item"><li><span><a href="#My-personal-convention" data-toc-modified-id="My-personal-convention-3.4.1">My personal convention</a></span></li></ul></li></ul></li><li><span><a href="#Why-a-build-system:-very-basic-CMake-demonstration" data-toc-modified-id="Why-a-build-system:-very-basic-CMake-demonstration-4">Why a build system: very basic CMake demonstration</a></span></li><li><span><a href="#Where-should-the-headers-be-included?" data-toc-modified-id="Where-should-the-headers-be-included?-5">Where should the headers be included?</a></span></li><li><span><a href="#Forward-declaration" data-toc-modified-id="Forward-declaration-6">Forward declaration</a></span></li></ul></div>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Library and program ## Library and program
Contrary to for instance Python or Ruby, C++ is not a scripting language: it is intended to build either an **executable** or **library**. Contrary to for instance Python or Ruby, C++ is not a scripting language: it is intended to build either an **executable** or **library**.
To summarize: To summarize:
* An **executable** runs the content of the [`main() function`](http://localhost:8888/notebooks/1-ProceduralProgramming/4-Functions.ipynb#A-very-special-function:-main). There should be exactly one such function in all the compiled files; the file with this `main` must be compiled. * An **executable** runs the content of the [`main() function`](http://localhost:8888/notebooks/1-ProceduralProgramming/4-Functions.ipynb#A-very-special-function:-main). There should be exactly one such function in all the compiled files; the file with this `main` must be compiled.
* A **library** is a collection of functions, classes and so on that might be used in a program. A library may be **header-only**: in this case it is just an ensemble of header files with no file compiled. In this case all the definitions must be either **inline** or **template**. * A **library** is a collection of functions, classes and so on that might be used in a program. A library may be **header-only**: in this case it is just an ensemble of header files with no file compiled. In this case all the definitions must be either **inline** or **template**.
### Static and shared libraries ### Static and shared libraries
A (non header) library may be constructed as one of the following type: A (non header) library may be constructed as one of the following type:
* A **static** library, usually with a **.a** extension, is actually included directly into any executable that requires it. The advantage is that you just need the bare executable to run your code: the library is no longer required at runtime. The inconvenient is that the storage space may balloon up rather quickly: each executable will contain the whole library! * A **static** library, usually with a **.a** extension, is actually included directly into any executable that requires it. The advantage is that you just need the bare executable to run your code: the library is no longer required at runtime. The inconvenient is that the storage space may balloon up rather quickly: each executable will contain the whole library!
* A **shared** library, which extension may vary wildly from one OS to another (**.dylib**, **.so**, **.dll**, etc...), is on the other hand required at runtime by the executable that was built with it. The advantage is that executables are thus much smaller. They are often described on the Web as the way to go; my personal experience with them is however less rosy as each OS handles them differently (noticeably the way to indicate in which location the dynamic libraries should be looked at differ rather wildly...) * A **shared** library, which extension may vary wildly from one OS to another (**.dylib**, **.so**, **.dll**, etc...), is on the other hand required at runtime by the executable that was built with it. The advantage is that executables are thus much smaller. They are often described on the Web as the way to go; my personal experience with them is however less rosy as each OS handles them differently (noticeably the way to indicate in which location the dynamic libraries should be looked at differ rather wildly...)
The best if possible is to enable generation of your library in either type... but it requires a bit of work with your build system. The best if possible is to enable generation of your library in either type... but it requires a bit of work with your build system.
## Source file ## Source file
Contrary to most of more modern languages, C++ relies upon two very specific kind of files, each of which with their own extension schemes. We will introduce first the source file, with which basic programs might be achieved, and then show why header files are also needed. Contrary to most of more modern languages, C++ relies upon two very specific kind of files, each of which with their own extension schemes. We will introduce first the source file, with which basic programs might be achieved, and then show why header files are also needed.
### Compilation of _Hello world!_ ### Compilation of _Hello world!_
A source file is a type of file intended to be **compiled**. A source file is a type of file intended to be **compiled**.
Let's consider the seminal _Hello world_ in a dedicated source file named _hello.cpp_ (all the examples here are made available in `2c-Demo` directory): Let's consider the seminal _Hello world_ in a dedicated source file named _hello.cpp_ (all the examples here are made available in `2c-Demo` directory):
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File hello.cpp - I put "Code" as cell type in Jupyter to get nice colors but it's not intended // File hello.cpp - I put "Code" as cell type in Jupyter to get nice colors but it's not intended
// to be executed in the cell! // to be executed in the cell!
#include <iostream> #include <iostream>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::cout << "Hello world!" << std::endl; std::cout << "Hello world!" << std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
To compile it on a Unix system, you will need to type in your terminal a line that looks like (with at least [GNU compiler for C++](https://en.wikipedia.org/wiki/GNU_Compiler_Collection) and [clang++](https://en.wikipedia.org/wiki/Clang)): To compile it on a Unix system, you will need to type in your terminal a line that looks like (with at least [GNU compiler for C++](https://en.wikipedia.org/wiki/GNU_Compiler_Collection) and [clang++](https://en.wikipedia.org/wiki/Clang)):
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// In a terminal // In a terminal
g++ -std=c++17 hello.cpp -o hello g++ -std=c++17 hello.cpp -o hello
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
where: where:
- `g++` is the name of the compiler. You may provide clang++ if you wish. - `g++` is the name of the compiler. You may provide clang++ if you wish.
- `-std=c++17` tells to use this version of the standard. If not specified the compilers tend to assume C++ 11 but may issue warnings if some features introduced with this standard are used. - `-std=c++17` tells to use this version of the standard. If not specified the compilers tend to assume C++ 11 but may issue warnings if some features introduced with this standard are used.
- `hello.cpp` is the name of the source file. - `hello.cpp` is the name of the source file.
- `hello` is the name of the executable produced. If the `-o hello` is omitted, the executable is arbitrarily named `a.out`, exactly as in C. - `hello` is the name of the executable produced. If the `-o hello` is omitted, the executable is arbitrarily named `a.out`, exactly as in C.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The executable may then be used with: The executable may then be used with:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// In a terminal // In a terminal
./hello ./hello
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The `./` is there to specify the executable is to be looked at in current path; it may be omitted if `.` is present in the system `PATH` environment variable. The `./` is there to specify the executable is to be looked at in current path; it may be omitted if `.` is present in the system `PATH` environment variable.
Please notice the name of the file with the `main()` function and the name of the executable are completely custom; you have no requirement on the names of files and executable. Please notice the name of the file with the `main()` function and the name of the executable are completely custom; you have no requirement on the names of files and executable.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
If your current machine has the compilers installed it is possible to execute these compilation commands instead of opening the terminal use the ! symbol as follows:
%% Cell type:code id: tags:
``` C++17
!g++ -std=c++17 ./2c-Demo/1-HelloWorld/hello.cpp -o hello
```
%% Cell type:code id: tags:
``` C++17
!./hello
```
%%%% Output: stream
Hello world!
%% Cell type:markdown id: tags:
### Source files extensions ### Source files extensions
The plural is not a mistake: unfortunately, contrary to many languages, there is no universal convention upon the extensions to use for C++ files. There are widely spread conventions, but a library may choose not to follow them. The plural is not a mistake: unfortunately, contrary to many languages, there is no universal convention upon the extensions to use for C++ files. There are widely spread conventions, but a library may choose not to follow them.
Editors and IDE know the most common ones and usually provide a way to add your own spin so that they may provide language recognition and all that goes with it (colored syntax, completion helper and so on). Editors and IDE know the most common ones and usually provide a way to add your own spin so that they may provide language recognition and all that goes with it (colored syntax, completion helper and so on).
The most common extensions are **.cpp**, **.cc**, **.C** and more seldom **.cxx**. The most common extensions are **.cpp**, **.cc**, **.C** and more seldom **.cxx**.
My advice would be to choose one and stick to it; the only one I warn against is **.C** because some operating systems (such as macOS) are case-insensitive by default and **.c** is a more common convention for C programs. My advice would be to choose one and stick to it; the only one I warn against is **.C** because some operating systems (such as macOS) are case-insensitive by default and **.c** is a more common convention for C programs.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Expanding our hello program with two source files: one for main, one for the function ### Expanding our hello program with two source files: one for main, one for the function
This code is not very subtle: everything is in the same file, so we are in a very simplistic case in which only one file is compiled, and there are no need to find ways to specify how several files relate to each other. This code is not very subtle: everything is in the same file, so we are in a very simplistic case in which only one file is compiled, and there are no need to find ways to specify how several files relate to each other.
You may imagine working in a single file is not an very common option: it hinders reusability, and it would be cumbersome to navigate in a file with thousands or more lines or code (if you're really curious to an extreme case have a look at the amalgamation ([2.28 Mo zip here](https://www.sqlite.org/2020/sqlite-amalgamation-3310100.zip)) of sqlite code, in which all the code is put in a same source file...) You may imagine working in a single file is not an very common option: it hinders reusability, and it would be cumbersome to navigate in a file with thousands or more lines or code (if you're really curious to an extreme case have a look at the amalgamation ([2.28 Mo zip here](https://www.sqlite.org/2020/sqlite-amalgamation-3310100.zip)) of sqlite code, in which all the code is put in a same source file...)
We want know to separate the main() and the actual content of the code: We want know to separate the main() and the actual content of the code:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File hello.cpp - no main inside // File hello.cpp - no main inside
#include <iostream> #include <iostream>
void hello() void hello()
{ {
std::cout << "Hello world!" << std::endl; std::cout << "Hello world!" << std::endl;
} }
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File main.cpp // File main.cpp
#include <cstdlib> // for EXIT_SUCCESS #include <cstdlib> // for EXIT_SUCCESS
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
hello(); hello();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
This brute force method is not working: a line on a terminal like: This brute force method is not working: a line on a terminal like:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// In a terminal // In a terminal
clang++ -std=c++17 hello.cpp main.cpp -o hello clang++ -std=c++17 hello.cpp main.cpp -o hello
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
would yield something like: would yield something like:
````verbatim ````verbatim
main.cpp:5:5: error: use of undeclared identifier 'hello' main.cpp:5:5: error: use of undeclared identifier 'hello'
hello(); hello();
^ ^
1 error generated. 1 error generated.
```` ````
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Header file ## Header file
The issue above is that we need to inform the compiler when it attemps to compile `main.cpp` that `hello()` function is something that exists. We need to **declare** it in a dedicated **header file** and **include** this file in each source file that needs it: The issue above is that we need to inform the compiler when it attemps to compile `main.cpp` that `hello()` function is something that exists. We need to **declare** it in a dedicated **header file** and **include** this file in each source file that needs it:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File hello.hpp // File hello.hpp
void hello(); void hello();
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File main.cpp // File main.cpp
#include <cstdlib> // for EXIT_SUCCESS #include <cstdlib> // for EXIT_SUCCESS
#include "hello.hpp" #include "hello.hpp"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
hello(); hello();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File hello.cpp - no main inside // File hello.cpp - no main inside
#include <iostream> #include <iostream>
#include "hello.hpp" #include "hello.hpp"
void hello() void hello()
{ {
std::cout << "Hello world!" << std::endl; std::cout << "Hello world!" << std::endl;
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
With this few changes, the command line: With this few changes, the command line:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// In a terminal // In a terminal
clang++ -std=c++17 hello.cpp main.cpp -o hello clang++ -std=c++17 hello.cpp main.cpp -o hello
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
works as expected and creates a valid `hello` executable (also note the header file is not required explicitly in this build command) works as expected and creates a valid `hello` executable (also note the header file is not required explicitly in this build command)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Header location ### Header location
In the example above `hello.hpp` was found because it was in the current folder. Let's suppose now we want to put include files in a directory named `incl`; to make it work we have actually two ways: In the example above `hello.hpp` was found because it was in the current folder. Let's suppose now we want to put include files in a directory named `incl`; to make it work we have actually two ways:
* Either modifying the path in the source file. We would get * Either modifying the path in the source file. We would get
````#include "incl/hello.hpp"```` in both hello.cpp and main.cpp. ````#include "incl/hello.hpp"```` in both hello.cpp and main.cpp.
* Or by giving to the command line the `-I` instruction to indicate which path to look for: * Or by giving to the command line the `-I` instruction to indicate which path to look for:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// In a terminal // In a terminal
clang++ -std=c++17 -Iincl hello.cpp main.cpp -o hello clang++ -std=c++17 -Iincl hello.cpp main.cpp -o hello
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
As many `-I` as you wish may be provided on the command line; I would recommend not providing too many as it increases the risk of an ambiguity if two header files at different path are named likewise: As many `-I` as you wish may be provided on the command line; I would recommend not providing too many as it increases the risk of an ambiguity if two header files at different path are named likewise:
````verbatim ````verbatim
incl/foo.hpp incl/foo.hpp
bar/incl/foo.hpp bar/incl/foo.hpp
```` ````
and and
```` ````
clang++ -Iincl -Ibar/incl main.cpp clang++ -Iincl -Ibar/incl main.cpp
```` ````
leads to an ambiguity if there is `#include "foo.hpp"` in the `main.cpp`... leads to an ambiguity if there is `#include "foo.hpp"` in the `main.cpp`...
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### `""` or `<>`? ### `""` or `<>`?
You may have noticed I sometimes used `<>` and sometimes `""` to specify the path for the include. You may have noticed I sometimes used `<>` and sometimes `""` to specify the path for the include.
The details don't matter that much in most cases, but it is better to: The details don't matter that much in most cases, but it is better to:
* Use `<>` only for the system libraries, typically STL or C headers should be this form. * Use `<>` only for the system libraries, typically STL or C headers should be this form.
* Use `""` for your headers or for third-party libraries installed in specific locations. * Use `""` for your headers or for third-party libraries installed in specific locations.
If you want a bit more details: If you want a bit more details:
* `""` will look first in the current directory, and then in the header files directories. * `""` will look first in the current directory, and then in the header files directories.
* `<>` will look only in the header files directories. * `<>` will look only in the header files directories.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Header guards ### Header guards
During compilation, the `#include` command is actually replaced by the content of the file which path is provided here. We therefore may quickly include twice the same content: During compilation, the `#include` command is actually replaced by the content of the file which path is provided here. We therefore may quickly include twice the same content:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File foo.hpp // File foo.hpp
class Foo class Foo
{ }; { };
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File main.cpp // File main.cpp
#include <cstdlib> #include <cstdlib>
#include "foo.hpp" #include "foo.hpp"
#include "foo.hpp" // Oops... #include "foo.hpp" // Oops...
int main() int main()
{ {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
doesn't compile: the translation unit provides two declarations of class Foo! doesn't compile: the translation unit provides two declarations of class Foo!
This might seem a simple enough mistake to fix it, but in a project with few header files that might be intricated it becomes quickly too much a hassle: This might seem a simple enough mistake to fix it, but in a project with few header files that might be intricated it becomes quickly too much a hassle:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File foo.hpp // File foo.hpp
class Foo class Foo
{ }; { };
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File bar.hpp // File bar.hpp
#include "foo.hpp" #include "foo.hpp"
struct Bar struct Bar
{ {
Foo foo_; Foo foo_;
}; };
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` C++17
// File main.cpp // File main.cpp
#include <cstdlib> #include <cstdlib>
#include "foo.hpp" #include "foo.hpp"
#include "bar.hpp" // Compilation error: "foo.hpp" is sneakily included here as well! #include "bar.hpp" // Compilation error: "foo.hpp" is sneakily included here as well!
int main()