Skip to content
Snippets Groups Projects
Commit 61aa39e4 authored by Thibaut DE SAIVRE's avatar Thibaut DE SAIVRE
Browse files

finish c++ readme

parent 0bc997eb
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,27 @@ ...@@ -3,6 +3,27 @@
[![Language Server: Clangd](https://img.shields.io/badge/Language%20Server-Clangd-blue.svg)](https://clangd.llvm.org/) [![Language Server: Clangd](https://img.shields.io/badge/Language%20Server-Clangd-blue.svg)](https://clangd.llvm.org/)
[![Code Formatter: ClangFormat](https://img.shields.io/badge/Code%20Formatter-ClangFormat-blue.svg)](https://clang.llvm.org/docs/ClangFormat.html) [![Code Formatter: ClangFormat](https://img.shields.io/badge/Code%20Formatter-ClangFormat-blue.svg)](https://clang.llvm.org/docs/ClangFormat.html)
**WARNING** : this setup only works for Linux and Windows-WSL.
## Setup and Run
In order to setup your C++ project and run it, you must use the following commands :
Create the build output directory (the first time)
```bash
mkdir build
cd build
```
Compile your code
```bash
cmake .. # Re-run everytime you add or remove .hpp & .cpp files, or when you change CMakeLIsts.txt
make # Compile your code
./<executable_name> # Run your compiled program
```
## VSCode Project Settings ## VSCode Project Settings
In order to ensure that everyone uses the same linting and formatting settings, In order to ensure that everyone uses the same linting and formatting settings,
...@@ -28,12 +49,11 @@ The [Clangd](https://clangd.llvm.org/) language server is a better language serv ...@@ -28,12 +49,11 @@ The [Clangd](https://clangd.llvm.org/) language server is a better language serv
## Environment and reproducibility ## Environment and reproducibility
TODO: linux As c++ does not have a package manager, you must install dependencies by hand. There are 3 ways to install dependencies :
In order to make it easier for other developers to contribute to your project, - Install using Linux packages : some dependencies are available as Linux packages.
and to enable deploying your application easily, you must indicate which python version and dependencies you are using. - Install manually to `/usr/local/include` (headers), `/usr/local/lib` (libraries) and `/usr/local/bin` (executables). If you put the relevant dependency folders and executable there, your C++ compiler will find them without any issue.
- Install manually to a random location : you will need to modify your `CMakeLists.txt` in order to tell the compiler where to find the dependencies (more complicated).
You typically indicate the python version in your [`pyproject.toml`](./pyproject.toml) or (`README.md`)(./README.md) file.
### Git ### Git
...@@ -42,52 +62,24 @@ Some common gitignores for a C++ project : ...@@ -42,52 +62,24 @@ Some common gitignores for a C++ project :
- `build/` : the build output folder. - `build/` : the build output folder.
- `.cache/` : clangd cache folder. - `.cache/` : clangd cache folder.
## Python Clean Code ## C++ Clean Code
TODO : code rules
Some often-overlooked python clean code rules : Some often-overlooked C++ clean code rules :
### Docstrings ### Docstrings
Always write docstrings in order to describe the purpose of your functions and classes. Always write docstrings in order to describe the purpose of your functions and classes.
```python ```cpp
def function(): /** This function does something */
"""This function does something.""" void function() {
pass }
``` ```
### Type hinting ### Header files
When you can (always), use type hints to indicate the types of the parameters of your functions, their return types, etc... When your project gets bigger, you must split your code into header (`.hpp`) and source (`.cpp`) files.
You sometimes need to use the `typing` module to indicate more complex types. The header files contain class and function header declarations, and source files contain the implementations.
```python - ALWAYS add `#pragma once` at the very top of each of your header files.
from typing import List - ALWAYS include header files in your code, NEVER include source files.
def add_to_list(my_list: List[int], element: int) -> List[int]:
my_list.append(element)
return my_list
```
Since python 3.10, you can use the `|` operator to indicate that a parameter can be of multiple types, and use `list` and `dict` directly as type hints.
```python
def add_to_list(my_list: list[int | float], element: int | float) -> list[int | float]:
my_list.append(element)
return my_list
```
### Use modules
When your code gets big, you should split it into multiple files.
Use the following test in order to run module-specific code only when you run your module directly via `python module.py`, and not when you import it from another file.
```python
def module_function():
pass
if __name__ == "__main__":
module_function()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment