Hacker News new | past | comments | ask | show | jobs | submit login
Modern C++ for C Programmers (ds9a.nl)
104 points by ausjke on April 30, 2019 | hide | past | favorite | 21 comments



I really like this list as it seems to cover the core aspects of what makes C++ a good language. But the one issue I have with resource such as these, is the lack of covering how to build real work projects. The list itself seems really good and after giving the topics a light skim, I really want to dive right into it and read everything.

But the biggest thing that I struggled with (and still have no idea about) when learning C++ was trying to learn how to build real projects. This includes things like, how do you use an external library? How do you set up a build system? Which build system to use in the first place? The last time I was trying to learn C++ (about two years ago I think?) I couldn't find answers to these questions, which was pretty discouraging as I thought C++ was a really useful language to learn. I wish this article covered some of these topics for beginners looking to build something more than a simple program.


>This includes things like, how do you use an external library?

Are you using Linux or Windows? If Windows & MS Visual Studio, Yan Cherinokov (programmer at EA) made some helpful videos about how to create libraries and link them:

https://www.youtube.com/user/TheChernoProject/search?query=l...

I recommend his C++ tutorials over other ones because he often deliberately does the wrong thing to show you what the error messages look like. This teaches you how to troubleshoot your own C++ projects when you encounter the same errors.

Lots of tutorials only show the correct way but it's also important to demonstrate the common ways that beginners do it incorrectly. This teaches people how to dissect the error codes.


CMake is the modern build tool everyone seems to love, but regular old GNU/BSD Makefile is fine too. As for a package manager it seems like people are converging on Conan or vcpkg (I have no real metrics, just basing that statement on the posts I see on Reddit over at /r/cpp).


If you're on a Linux system, learn how to use Makefiles for building since they're generally useful for lots of things. Cmake is becoming more common and does some good things, but I find it very hard to use/debug (and you might find yourself debugging the Makefiles it generates anyway). It helps if you already know how to compile/link C programs...

How to incorporate external libraries depends on if they're header-only, shared libraries, or static libraries (do people still use static libraries?). pkg-config is your friend!


Most embedded systems do not support dynamic linking.

Plus unless you are doing plugins, dynamic linking is an attack vector.


Fair enough! Although the tooling for embedded systems is usually some crazy black art! (I only have experience with mbed and energia systems).


This is the main issue that prevents me from learning C++. None of this seems to be documented or explained anywhere. I can find fantastic books on how to write C++ but this entire topic isn't addressed anywhere even though it tends to be a big source of time-consuming errors/issues for beginners. I've wanted to use a certain library, spend close to a week trying to figure out how to make it accessible to my project and then give up in frustration.


Depends pretty much on the platform.

On Apple, Windows, embedded platforms and game consoles, it is pretty much "File->New Project" on the respective OS IDEs.

Then you just vendor the libraries on some company wide repository and use the IDE respective features to link to them.

Then most CI/CD systems understand the respective project files.

Naturally you can also go old style Makefiles, CMake or whatever is the build tool of the day.

It depends on the company's approach to their development stack.


If you already know C, then the knowledge of how to use a library and build system should carry over.


Not a bad list.


the code snippets used have c++17 and c++20 features, my clion only supports up to c++14 with very limited c++17 support, not to mention c++20.

clang++ does have good c++17 support but g++ only supports full set of c++14


Which g++ are you talking about? g++-8 defaults to C++17. g++-9 supports many C++20 features.


using default g++7 on ubuntu 18.04, but you're correct that 8/9 has c++17/c++2a support, i need upgrade my toolchain now


If you are not already using C++, learn Rust instead.


Coincidentally, the author already has a response for you: https://ds9a.nl/articles/posts/cpp-rust-go/


This post makes the mistake of assuming that anybody in industry writes secure C or C++ code.


- Rust doesn't have to keep 30+ years of backwards compatibility. You can learn it.

- C++ has hundreds of dialects to pick from. You'll spend time bikeshedding with coding standards, you'll spend time in meetings trying to get people to agree to a standard, you'll spend time trying to enforce the standard...

- Rust makes reasonable decisions by default, and mutable/unsafe code is opt-in.


Why learn Rust over C++?


Well, my answer is this: Coming from the perspective on a Pythonista, C++ seems tedious and error-prone. Rust, on the other hand, is merely tedious -- which is progress of a sort, one has to admit.


Coincidentally, I personally started learning Rust as C++ was a lot more difficult to learn by myself. The Rust book[1] is a great resource as it makes learning pretty complex topics fairly easy to approach.

Personally the thing that really sold me over was the "Building a guessing game" section, as it showed how to use the build system to pull in external libraries. I found these aspects specifically difficult to learn when learning C++. Plus, after learning Rust, I was able to go back to C++ and better understand it. Still have no clue about build systems though.

[1]: https://doc.rust-lang.org/book/


And be treated badly by the Rust community for not agreeing with their political opinions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: