r/programming • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
210 Upvotes

105 comments sorted by

View all comments

27

u/[deleted] Jan 28 '19 edited Jan 28 '19

Provide a “manifest” or “mapping” file that describes the filepath to an MIU based on the module name. This file will need to be user-provided, or we are back in the scanning problem.

Why can't this be <implementation-defined> just like the mapping from header file inicludes to files?

Honestly, what you propose won't fly because (1) you are turning C++ compilers into C++ compilers + build systems, and (2) because getting 150 countries and >100 committee members to agree on formats for these manifest files, file system structure, etc. is pretty much impossible.

Also, many languages have solved this problem, and none of them had to turn their compilers into build systems.

One such language is Rust. C++ modules solve the same problem as Rust crates. The Rust language provides an import syntax for crates (extern crate foo), and the user provides the mapping from that to files manually by passing CLI arguments to the compiler - the dependency must be compiled before, so this happens-before relation exists there too.

Nobody provides these CLI arguments manually. Instead, the build tool (cargo) parses a TOML manifest file that specifies dependencies, builds a dependency graph, fetches them from the internet, the file system, etc., compiles them, and calls the compiler with the right arguments.

Another build tool could parse a YAML manifest, or parse all source files scanning for imports, and generate the manifest file automatically (this would be incredibly hard to get right for Rust, and probably impossibly hard for C++ though), but still reuse the same compiler by calling it with the right arguments.

Most importantly, neither the compiler nor the language have to know anything about this.

In fact, as you have noticed, happens-before requires you to compile dependencies before hand. That is, one needs to pass the modules that each module imports to the compiler "somehow". That's serving the same function as import module. So in Rust2018, you don't even need to import crates. Crates passed to the compiler via CLI are put in the root namespace, and that's it, you can just use them without extern crate. If you forget to pass one to the compiler, you just get an error because the path cannot be resolved.

0

u/[deleted] Jan 28 '19

[deleted]

11

u/[deleted] Jan 28 '19 edited Jan 28 '19

It's hard to tell what point you are trying to make. But if you are trying to state that Rust approach is hard for regular programmers, I strongly disagree.

Pretty much no C++ programmer actually knows exactly how dependencies are added to their project. The reason for this is that most of it happens implicitly. Yes, Makefiles, CMake files, etc. express some of the dependency information, but a lot of the information is passed implicitly to the compiler via environment variables (e.g. for the system header files, system library files, etc.), some of them also rely on conventions (e.g. #include <> vs #include "", etc.), the order of paths in these variables is relevant, etc.

Things magically appear to work for C++ until they don't, e.g., wrong library version / path being "magically" picked, wrong standard library picked (e.g. using clang+libc++ on Linux for one project when most of your other projects use gcc+libstdc++), etc. Debugging these problems typically require getting the compiler into a verbose mode for some TU which dumps all include paths, library paths, etc. being considered, their order, which one got picked and why, etc. Most devs just fickle with their environment till things work, they are probably even unaware that tools to debug these issues exist. I've seen C++ devs waste days trying to fix linking / include issues, and weeks trying to fix build system issues.

There are some Rust features that are too implicit for newbies to understand, like Rust2015 crate paths, but dependency resolution / the build system is not really one of them. Rust projects have an explicit Cargo.toml listing their dependencies and that's pretty much it. Is the wrong version of a library being picked? Open the Cargo.toml, change the library version/path/git-branch/..., done. Newbies don't, in general, know anything about the dependency-resolution algorithm used, but because it is all explicitly listed in one place, they can manually change things if the algorithm produces an output that they don't like.

8

u/pakoito Jan 28 '19 edited Jan 28 '19

Sarcasm doesn't transmit well on internet posts, that's why I tried to put it as a strawman quote.

2

u/[deleted] Jan 28 '19

Ah sorry, I read too much into it.