Thanks in part to fierce competition over the past decade, C++ has seen a huge ramp in language features and batteries included library support. Build tools have also improved so much that starting a new project could almost be described as user-friendly. Most projects are standardizing around CMake which, with a style ironically similar to C++, can be quite powerful when restricted to a pleasant subset of the overall language. This is a quick guide to starting your own C++ programming project. Use a few of these concepts and you’ll be off to a great start.
$ # CMake has a great "out-of-source" build convention. This keeps source $ # files isolated from generated files and binaries. $ mkdir build && cd build $ # First use CMake to generate the build system. This step is only required $ # when making a change to CMakeLists.txt or another CMake script. $ cmake .. -- The C compiler identification is GNU 5.4.0 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/brian/codecast/002/build $ # Then use the build system to build the app. $ make Scanning dependencies of target codecast002 [ 50%] Building CXX object CMakeFiles/codecast002.dir/codecast002_main.cc.o [100%] Linking CXX executable codecast002 [100%] Built target codecast002 $ # Finally run all tests, rinse and repeat. $ ./codecast002 Hello World!
My C++ projects always start with two extremely useful libraries: gflags and glog. Both are open source libraries from Google that have seen heavy production use inside and outside the company. Gflags provides a quick and easy way to integrate command line flags into your application, while glog delivers flexible, high performance logging with built-in assertions and stack traces. These libraries are under active development on GitHub and very responsive to issues and pull requests.
To add the libraries to our project we’ll use the popular CMake package manager Hunter. The jury is still out on which package manager will become the de-facto choice for C++, but Hunter is a great choice today. It’s written in native CMake script, so there are no additional dependencies (like Python, node.js or another scripting language). All Hunter requires is one CMake module and a few lines in
CMakeLists.txt to enable it.
$ # As you can guess, our app doesn't respond to command line flags yet. $ ./codecast002 --version Hello World! $ ./codecast002 --help Hello World! $ cd .. $ mkdir cmake && cd cmake $ # Fetch the latest Hunter CMake module from GitHub. You can cheeck this in $ # with the rest of your source files. $ wget https://github.com/hunter-packages/gate/raw/master/cmake/HunterGate.cmake --2017-07-15 20:29:44-- https://github.com/hunter-packages/gate/raw/master/cmake/HunterGate.cmake Resolving github.com (github.com)... 18.104.22.168, 22.214.171.124 Connecting to github.com (github.com)|126.96.36.199|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://raw.githubusercontent.com/hunter-packages/gate/master/cmake/HunterGate.cmake [following] --2017-07-15 20:29:45-- https://raw.githubusercontent.com/hunter-packages/gate/master/cmake/HunterGate.cmake Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 188.8.131.52, 184.108.40.206, 220.127.116.11, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|18.104.22.168|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 16499 (16K) [text/plain] Saving to: ‘HunterGate.cmake’ HunterGate.cmake 100%[=========================================>] 16.11K --.-KB/s in 0.02s 2017-07-15 20:29:45 (926 KB/s) - ‘HunterGate.cmake’ saved [16499/16499]
So I realized after the fact that this episode continued coding until the very end! As I tune the video + source code + blog post combo I’ll try to include more long form content. Until then, please check out my GitHub repo for full source code. If you enjoyed watching please leave a comment, like this video or subscribe to my YouTube channel for future codecasts. Thanks for watching!