this post was submitted on 03 Apr 2024
29 points (100.0% liked)

C++

1763 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
 

I've been trying to learn a system language because it would enable me to access a whole new world of possibility for games, tools, and potential projects. My main problem when learning the language are:

  • can I write modern C++ code using the newer standards and still compile with libraries from older standards?
  • how do I even organize a C++ project? Look at the linked project, the CMakeList.txt is so hard to understand, the syntax looks so hard to write.
  • how do I install dependencies? You're going to laugh at me, but I always used languages with package managers and I looked again at the linked project, and they write a whole CMakeList.txt to import ImGui (GUI library I wanna try) but if you compare the structure of the files, it's different from the ones on the repository of ImGui.

As you see there are a lot of problems and it pains me to not be able to solve them because Rust is so unfun to use and work with! Do you think I should try C++, carry one with it?

Thanks, hector.

top 22 comments
sorted by: hot top controversial new old
[–] [email protected] 7 points 7 months ago* (last edited 7 months ago)

C++ is a great language. However, the standard library is small and the systems for package management are vastly inferior to what you find with other popular languages.

C++ applications regularly use largely organization internal code bases with bits and pieces of the standard library integrated into them.

That's so as to say, with C++ code tends to be more freestanding/independent but it also means that the time from no code to "working" for non-toy projects is way higher than it otherwise should be (e.g. if you could just trivially pull in a few good libraries).

can I write modern C++ code using the newer standards and still compile with libraries from older standards?

The answer as to "can you take C++11 code and mix it with C++20 code" (as an example) is a more complicated one. I suggest this excellent answer from Jonathan Wakely which is relevant to the GNU/GCC C++ implementation https://stackoverflow.com/a/49119902

how do I even organize a C++ project?

This is something I struggled with when I first came to C++ land and the answer is basically "however the heck you want."

Every C++ project takes a different approach. C++ build systems are closer to bash script anarchy than some carefully crafted standard practice.

If you go into C++ build systems expecting well established, consistent, conventions... You're going to be disappointed and frustrated.

The C++ ecosystem in general provides options more than answers. This is in contrast to something like Ruby on Rails where convention over configuration is the mantra.

Do you think I should try C++, carry one with it?

If you're interested, absolutely! You do need to temper your expectations with C++ though. It's a great language once your code base has some momentum, prior to that point though you're going to have to be scrappy.

I'd recommend trying to either expand on an existing project or working within an established ecosystem that does have some guiding principles. Qt, CryEngine, Godot, Boost, CMake, and Meson are all terms I would Google and consider looking at to varying degrees. There's also cool stuff to look at here: https://en.cppreference.com/w/cpp/links/libs

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago) (1 children)

(1) usually
(2) maybe this, pretty debatable
(3) also pretty flexible

with great power comes great responsibility

[–] [email protected] 3 points 7 months ago (1 children)

Wow so those are real legitimate concerns before starting a project… this is hard lol

[–] [email protected] 5 points 7 months ago* (last edited 7 months ago) (2 children)

C++ is great with many things, but a consistent build system is Definitely not one of them. Additionally to note, when doing CMake stuff, it's generally best to stick to the modern way of doing things (targets, rather than a slew of global properties); it ends up being a lot easier to develop as projects grow and other dependencies are added.

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago) (1 children)

Additionally to note, when doing CMake stuff, it’s generally best to stick to the modern way of doing things

I'd add that the so called Modern CMake is well over a decade old. There is no excuse for people to choose to be miserable.

Personally I prefer calling "Modern CMake" as simply CMake, and the old way of using CMake as "you're doing it very wrong, invest 5 minutes going through a tutorial."

[–] [email protected] 4 points 7 months ago

I don't disagree at all there, however there's an absolute glut of websites showing the bad way of doing things. For a person brand new to C++ though, I find it generally important to make a distinction, so one doesn't pick up the first thing seen and learn bad habits.

[–] [email protected] 1 points 7 months ago (1 children)

I think I’m gonna use Bazel anyways, the basic stuff and configuration I need is there. It’s super easy to say what part of your code depends on which.

You can use globs to compile every files and be aware of every head according to a regular expression… yeah I really like it.

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago)

I think I’m gonna use Bazel anyways

Last time I checked, Bazel barely supported externa libraries and had zero support for shared libraries. Admittedly that's been a while, but those are pretty basic features to be missing.

IDE integration is also critical, and Bazel has nothing to show for in this regard.

Lastly, most of the benefits that Bazel advertises can be had with CMake by simply switching it's generator to something like Ninja, which is trivial and supports compiler cache tools such as ccache right out of the box.

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago) (1 children)

Rust is so unfun to use and work with

Out of curiosity, what makes it un-fun in your eyes? Or, I suppose, what is fun for you?

Fighting the borrow checker is rough at first, but things click after a while.

[–] [email protected] 4 points 7 months ago (1 children)

Not the OP but I've had the same experience. For me, it's slow to iterate in due to fightting the compiler. Languages like Go, Python, Java, C are all quicker to develop in, IMO.

[–] [email protected] 3 points 7 months ago (1 children)

Exactly, you’re right: it does not feel like you’re creating? You’re always fighting against the compiler and I prefer some debugging/seeking and keeping good practices that being surveilled and punished by the compiler.

[–] [email protected] 3 points 7 months ago (1 children)

I like what Rust tries to accomplish, but for me, the cost is too high. It's just too awkward in practice.

[–] [email protected] 2 points 7 months ago (1 children)

I solved practically all of my problems and I see why C++ is such a monument in software engineering it’s sooo cool versatile and fun

[–] [email protected] 2 points 7 months ago

C++ is a powerful language, it also definitely has its share of problems.

[–] [email protected] 3 points 7 months ago (1 children)

I have over a decade of professional C++ experience.

  • Most of my fellow developers are still wrapping their heads around C++11.
  • I've never used cmake and have no idea how, it's always been something custom or something generated by an IDE (usually VS, not vscode, I'm talking about the gigantic purple monster).
  • Download source or even binaries and check 'em in. I had never even heard of package management until I started working with other languages more.

Some of these situations are probably better outside of my industry (gamedev).

[–] [email protected] 4 points 7 months ago (1 children)

I’ve never used cmake and have no idea how,

That's odd. CMake is the de facto standard in C++. Even Visual Studio supports CMake, and perhaps the best IDE for C++, CLion, is basically built around CMake.

What exactly have you been doing in that decade of experience working with C++?

[–] [email protected] 1 points 7 months ago

Like I said, game development. For example, I know Unreal's build system pretty well and that's a bunch of batch files, C#, and nmake that gets generated automatically.

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago)

can I write modern C++ code using the newer standards and still compile with libraries from older standards?

Short answer: yes.

Longer version: It depends on what compiler suite you're using. In the end the main requirements are that a) include headers are valid C++ in any of the standards you're targeting (i.e., don't expect to build a C++11 project which includes language features introduced in C++20) b) it generates symbols that can be resolved and linked on all binaries.

This should be done as a last resort, though. The problems you might experience won't be trivial to troubleshoot or common.

how do I even organize a C++ project?

One of the main advantages of C++ is that it does not enforce any project tree layout. It doesn't even require a specific file type or file extension. You are free to follow what works best for you.

If you want a guideline, that can also be provided. See for example this link:

https://github.com/vector-of-bool/pitchfork

how do I install dependencies?

C++ does not enforce any dependency resolution system. To keep things simple but debatable, you can give Conan a try.

https://conan.io/

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago)

can I write modern C++ code using the newer standards and still compile with libraries from older standards?

Yes, but then your project ends up requiring that newer standard as minimum too (which can be fine if that's what you want).

how do I even organize a C++ project?

Well, one way that should be simple and scalable for beginners is leaving the root folder for project configuration/metadata/contribution guidelines/clang-format etc. and putting all source code inside src/.

If it's just an app, you can make smaller library targets in src/ in separate folders with meaningful names, like src/models/, src/settings/ etc. If it's a library, you typically put headers you intend to expose to your users in a src/include/ folder and use target_include_directories() to point to it, otherwise the same principle as apps applies.

It requires writing a few more, smaller CMakeLists, but it does mean you're never lost in your own code.

how do I install dependencies?

We added this recently to KDE's developer documentation, so at least for Linux it's a start: Installing build dependencies: Using build errors to find missing dependencies.