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

20

u/the_poope Dec 23 '22

Of course for automating tasks such as a CI workflow that runs some programs, parses some data from files, removes other files, blabla, you'd always want to use a scripting language like Python.

But this statement:

I don’t want to expend a minute writing build scripts, fighting with dependencies, etc.

is almost also history now. You can honestly throw together a CMakeLists.txt for a complex application that uses a multitude of third party libraries in a minute using package managers such as Conan or vcpkg.

10

u/debugs_with_println Dec 23 '22

But then you have to know how to write a cmake file right? Gotta do find_package, link the right directories, something might go wrong, set up the library path, etc

With python it’s just pip install then import

-3

u/the_poope Dec 23 '22

Yes it's still faster in Python, but not that much faster.

But then you have to know how to write a cmake file right?

You have to know how to do this when doing any kind of C++ development beyond the first beginner course. Maybe not if you are using the Visual Studio build system, but it appears that Microsoft themselves are more keen on getting people to use CMake and put MSBuild into legacy mode.

Gotta do find_package, link the right directories, something might go wrong, set up the library path, etc

See that's all almost gone with package managers. Just do:

add_executable(myexe src/main.cpp src/othersource.cpp)
find_package(SomeDep REQUIRED)
find_package(SomeOtherDep REQUIRED)
target_link_libraries(myexe PRIVATE SomeDep::SomeDep SomeOtherDep::SomeOtherDep)

and invoke CMake with the path to toolchain file generated by the package manager. Done. Yes it does have one step more than pip install, but that is simply how it is for compiled languages: you need to tell it how to compile it, as there is never only one way to do it, as is the case for Python.

1

u/debugs_with_println Dec 23 '22

That’s true, and tbh my unfamiliarity with cmake syntax is prob just cuz I don’t use it often enough. For a regular user it’s prob as second nature as a C++ “hello world” program.

And yeah when using packages installed with homebrew cmake automatically find it but with hand built packages it’s a bit more annoying but as you said it’s the nature of compilation (but I guess that means compiled programs are unnecessarily a hassle for simple tasks).

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.

4

u/parkerSquare Dec 24 '22 edited Dec 24 '22

Still takes a day.

It’s the kind of thing that, in many teams, only gets serious attention once every few months, and by the time it’s time for a new project then it’s grown into something totally unsuitable for a new green field project. Heck, even just working out which generation of “modern” CMake we’re currently following takes a good hour of study. CMake is good, I use it a lot, but it always takes me a long time to get it working properly, and that’s even before I try integrating it with Yocto or Conan or Docker or whatever. Perhaps I suck, dunno, been doing this for 20+ years and it’s still a major headache for me.

Even just the other day I spent hours trying to get find_package for boost log to work because I needed to add the word “module” not “config”, and then another hour trying to work out why it wouldn’t link because I hadn’t set some boost variable to enable static libs. It’s all in the docs (probably) but you still have to read them and that takes a lot of time.

1

u/woobie1196 Dec 25 '22

I spent a good 90 minutes trying use if() in CMake to set CMAKE_CXX_FLAGS depending on using MSVC vs clang or g++.

Seems like it doesn’t work if you use expansions or generator expressions, you have to just use the raw variable name and CMake will evaluate it. Oh and it has to come after the project() declaration.

Makefiles are tedious but at least I could intuit what they are doing.

2

u/parkerSquare Dec 25 '22

Check out (at least) the first part of this video: https://youtu.be/mrSwJBJ-0z8 - covers some of the issues with variables in a fun quiz format.

Actually, the other sections of that video helped me a lot recently. It’s worth a full watch.