> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codecrafters.io/llms.txt
> Use this file to discover all available pages before exploring further.

# C++

> Learn how to solve CodeCrafters challenges in C++

## Local development

To run the code for your challenge locally, you'll need [vcpkg](https://vcpkg.io/en/) & `make` installed.

You'll also need the `VCPKG_ROOT` environment variable set to the path where vcpkg is installed.

The script for your challenge (like `./your_sqlite3.sh`) will automatically compile your code using the rules defined in `CMakeLists.txt` before executing it. It'll
look something like this:

```sh theme={null}
#!/bin/sh

set -e
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
exec ./build/server "$@"
```

## File structure

* The files for your solution are placed in the `src/` directory.
* `src/Main.cpp` contains the `main` function, which is what the test runner executes.
* `CMakeLists.txt` contains the rules for compiling your program.

## Adding more files

You can add more files and directories to `src/`. The test runner will include them when compiling your code.

For example, if you want to create a function `foo()` in another file, you need to:

1. Add a header file `Foo.hpp` and define `foo()` there.
2. Add a cpp file `Foo.cpp` and implement `foo()` there.

<CodeGroup>
  ```cpp src/Foo.hpp theme={null}
  // Function definitions go here
  #pragma once

  void foo();
  ```

  ```cpp src/Foo.cpp theme={null}
  // Implementations of functions defined in src/Foo.hpp go here
  #include <iostream>

  void foo() {
      std::cout << "Hello from foo!";
  }
  ```

  ```cpp src/Main.cpp theme={null}
  #include "Foo.hpp"

  int main(int argc, char* argv[]) {
      foo(); // calls foo() defined in src/Foo.hpp
  }
  ```
</CodeGroup>

<Note>The `#pragma once` directive in `src/Foo.hpp` ensures the header file is included only once during compilation.</Note>

## Adding dependencies

To add a third-party dependency such as the `asio` to a C++ project, follow these steps:

<Steps>
  <Step title="Add the dependency to vcpkg.json">
    Run `vcpkg add port asio` to add the dependency to the `vcpkg.json` file.
  </Step>

  <Step title="Add the dependency to CMakeLists.txt">
    To be able to use the dependency in your project, you'll need to add it to `CMakeLists.txt`.

    ```CMake theme={null}
    find_package(asio CONFIG REQUIRED)
    target_link_libraries(server PRIVATE asio asio::asio)
    ```
  </Step>

  <Step title="Include the dependency in your code">
    You can now include the dependency in your code.

    ```cpp theme={null}
    #include <asio.hpp>
    ```

    On your next push, the test runner will automatically install the dependency before compiling your code.
  </Step>
</Steps>
