C++
Learn how to solve CodeCrafters challenges in C++
Local development
To run the code for your challenge locally, you’ll need vcpkg & 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:
File structure
- The files for your solution are placed in the
src/
directory. src/Main.cpp
contains themain
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:
- Add a header file
Foo.hpp
and definefoo()
there. - Add a cpp file
Foo.cpp
and implementfoo()
there.
#pragma once
directive in src/Foo.hpp
ensures the header file is included only once during compilation.Adding dependencies
To add a third-party dependency such as the asio
to a C++ project, follow these steps:
Add the dependency to vcpkg.json
Run vcpkg add port asio
to add the dependency to the vcpkg.json
file.
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
.
Include the dependency in your code
You can now include the dependency in your code.
On your next push, the test runner will automatically install the dependency before compiling your code.
Was this page helpful?