A CMake project template for LeetCode.
Features
- CMake project
- Local playground
- Unit test and benchmark
- Cross platform
LeetCodeCMake
├── CMakeLists.txt # Project wide setup
├── src
│ ├── CMakeLists.txt # Target collection
│ ├── include
│ │ ├── CMakeLists.txt # Library collection
│ │ ├── catch.h # Catch2 header file
│ │ └── leetcode.h # Leetcode header file
│ └── L0001 # Problem 1
│ ├── L0001.h # Solution file
│ ├── L0001.cpp # Playground main
│ └── TestL0001.cpp # Test main
├── cmake # CMake modules
│ ├── Catch.cmake
│ └── CatchAddTests.cmake
└── build # Build directory
To run the example project, you need a complier with C++17 support.
Build and test the project in command line.
$ cmake -B build
$ cmake --build build --target TestL0001 L0001
$ cd build
$ ctest -R L0001 # run tests in problem L0001
Test project D:/code/learn_cmake/build
Start 1: L0001
1/1 Test #1: L0001 ............................ Passed 0.02 sec
100% tests passed, 0 tests failed out of 1
# run all tests and benchmark in problem L001
$ ./src/Debug/TestL0001 # Windows, multi-configure build
$ ./src/TestL0001 # Linux, single-configure build
-------------------------------------------------------------------------------
L0001_Benchmark
-------------------------------------------------------------------------------
/src/L0001/TestL0001.cpp(32)
...............................................................................
benchmark name samples iterations estimated
mean low mean high mean
std dev low std dev high std dev
-------------------------------------------------------------------------------
Brute Force 100 1 190.132 ms
706.525 us 585.886 us 874.196 us
718.779 us 563.925 us 971.882 us
Hash Map 100 1 20.7629 ms
187.329 us 170.959 us 206.033 us
88.9449 us 75.6476 us 107.436 us
Hash Map2 100 1 860.129 ms
9.20266 ms 9.10087 ms 9.3058 ms
523.041 us 467.378 us 615.805 us
===============================================================================
All tests passed (9 assertions in 2 test cases)
# run playground for problem L0001
$ ./src/Debug/L0001 # Windows, multi-configure build
$ ./src/L0001 # Linux, single-configure build
[2, 7, 11, 15]
9
[0, 1]
You can also open this directory using VSCode with CMake Tools extension, or open this directory using VS2019+ to run the target more easily.
- Problem directory:
src/LXXX
- Problem header:
src/LXXX.h
, includeleetcode.h
- [Optional] Playground source:
src/LXXX.cpp
, includeLXXX.h
- [Optional] Test source:
src/TestLXXX.cpp
, includeLXXX.h
andcatch.h
- Rebuild
Read TestL0001.cpp
and TestL0002.cpp
to learn hwo to write basic catch test cases. Refer to catch tutorial and benchmark for more complicated usage.
Some widely used leetcode structs and functions are inlucded in src/include/leetcode.h
- ListNode/TreeNode
- trimLeftTrailingSpaces/trimRightTrailingSpaces
- stringToInteger/stringToString/boolToString
- stringToIntegerVector/integerVectorToString
- stringToListNode/listNodeToString
- stringToTreeNode
Used STL: iostream
/sstream
/cstdlib
/cassert
/string
/vector
/queue
Some compromises for convenience but not best practices:
-
Use
file(GLOB ...)
to collect targets: CMake is not automatically re-run when removing or adding files. Even though theCONFIGURE_DEPENDS
flag can fix it, there is still a cost to perform the check on every rebuild. -
Use
#define CATCH_CONFIG_MAIN
in every test file, which will repeated compiling thecatch.hpp
for each test. However, we just want a runnable test for each problem, so we don't need a super test program. Avoid building all targets at once. -
Implement all methods in
LXXXX.hpp
to keep the project simple.
-
How to debug the playground with vscode?
Install the
vscode-cmake-tools
extension and configure thelaunch.json
according to https://vector-of-bool.github.io/docs/vscode-cmake-tools/debugging.html.
This is an example for gdb. You can create a default templatelaunch.json
on your platform and make sure thatprogram
,externalConsole
andenviroment
are set correctly. -
How to change the C++ standard?
Change the line
set(CMAKE_CXX_STANDARD 20)
in the top levelCMakeLists.txt
.