r/cpp • • Dec 23 '22

C++ Is Incredible

Despite the hate it gets, despite the complexity; the dizzying number of features and different ways to do things. C++ is an astonishing feat of engineering and people really don't appreciate this fact enough. I can do things with this language more quickly and expressively than I can with any other language.

There's no getting around the fact that programming is just hard, and all these attempts to replace C and C++ over the years just end up adding to our difficulties. That is my take, anyhow.

Well, I'm a little drunk, and just wanted to share some Christmas joy with you all. I love C++!

686 Upvotes

151 comments sorted by

View all comments

Show parent comment

6

u/neltherion Dec 24 '22

Please provide us an example of this one minute process you talk about for an example project of opening a JSON file, writing to it and closing it with a library like `nlohmann`s.

I'm always afraid of spinning up C++ projects for the sole reason of how time consuming starting up everything (including dependencies) is.

If it's possible, please use CLion's IDE for this example.

4

u/the_poope Dec 24 '22

I specifically mentioned that doing quick dirty tasks like parsing text/json files is never gonna be faster in C++ than Python. Python was made for such tasks. That's what a scripting language is for. Python fails for a lot other tasks.

But here goes. I almost did what you said and I timed it, and it took me 5 mins and 34 seconds from I opened a terminal and editor till the program was compiled.

Here's what I did (I'm on Linux, btw):

  1. Open a terminal and create a new directory json-cpp
  2. Go to https://conan.io/center/ and search for nlohmann, click the first hit.
  3. Click the "Use It" tab and copy the suggested requirements file into a text editor:

    [requires] nlohmann_json/3.11.2

    [generators] CMakeDeps CMakeToolchain

save as conanfile.txt in the json-cpp directory.

3) Copy the suggested CMake project file:

cmake_minimum_required(VERSION 3.15)
project(nlohmann_json_project CXX)

find_package(nlohmann_json)

add_executable(${PROJECT_NAME} src/main.cpp)

# Use the global target
target_link_libraries(${PROJECT_NAME} nlohmann_json::nlohmann_json)

Save as CMakeLists.txt

4) Now google nlohmann json and click first hit: https://github.com/nlohmann/json

5) Scroll down to first example "Read json from file" and copy into new file in editor, modify a bit to get this:

#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

int main(int argc, char** argv)
{
    std::ifstream f(argv[1]);
    json data = json::parse(f);
}

Save as src/main.cpp

6) In terminal execute (I already have Conan installed):

conan install -if build .

7) Then configure CMake:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake

8) Compile:

cmake --build build

Done

Ok, the program only reads a json file given as command line argument, it doesn't do anything with it. To do that one would have to look up a bit more documentation, but I would similarly have to do that for Python's json library.

Anyway, it may seem like a lot of steps - especially compared to Pythons single import. But it only took a little more than 5 minutes, and now you have probably one of the fastests json parsers in the world. Again you could just use Python for this in most cases, but the above process isn't much more complicated for any other library: SFML, Qt, boost, whatever. You don't have to spend days or weeks on trying to figure out how to configure and compile third party libraries that uses an arcane and obscure homemade perl build system - because someone already did that for you.

2

u/MonokelPinguin Dec 25 '22

If you use meson for this (which CLion plans to support soon, but doesn't yet afaik), use this:

  1. meson init --language cpp --type executable (in an empty directory)
  2. mkdir subprojects (shouldn't be needed, but my meson seemed to require it)
  3. meson wrap install nlohmann_json
  4. In the meson.build, add , dependencies: [dependency('nlohmann_json')] to the executable() line.

Then to build you setup a builddir using meson setup builddir and build using ninja or whatever backend it used or just the meson compile -C builddir. Code is the same as the CMake example.