C++ Logo

sg15

Advanced search

Re: [isocpp-ext] Can we expect that all C++ source files can have the same suffix?

From: Ben Boeckel <ben.boeckel_at_[hidden]>
Date: Wed, 13 Apr 2022 17:44:20 -0400
On Wed, Apr 13, 2022 at 22:58:13 +0200, Nicolai Josuttis via Ext wrote:
> What I teach about modules is compelling. Programmers like and want to
> use it.
> However, they ask how they should organize module files in practice.

<puts CMake hat on>

> So far I cannot recommend a specific suffix (and I might never be able
> to do that).
> However there is one important question that IMO the standard should answer:
> *Do we **/need /**different suffixes?*

Need? No. Even with different suffixes, a scanner is still necessary to
know what order files need to be compiled in.

> Is this still true with modules?
> That is: Can we expect that identifying a file as C++ file is enough to
> be able to (pre) compile it as C++ file?

I don't believe so. Ordering is important, so you might know what flags
to pass, but you still need to know to compile `provides_x.cpp` before
`imports_x.cpp`. I don't think the filename is the place to store such
information, so you're left with scanning or luck IMO.

> Current compilers give different answers (AFAIK):
>
> - *gcc *says the same suffix is possible. There is not special option
> for modules.
> I can still have my own suffixes and use -xc++ though.

There is `-x c++-header` also when compiling header units (to get a BMI
file). I believe Clang has some flags for its older module system that
are relevant, but I haven't dealt with it that much (because I haven't a
scanner for it yet).

> - *VC++* currently requires different suffixes or different command-line
> arguments.
> Identifying a file as C++ file is not enough.
> For example
> - This is not enough: /Tp mymod.cppm
> - You need: /interface /Tp mymod.cppm

You also need `-internalPartition` to be able to tell:

```c++
// Compile with `-internalPartition`
module X:part; // non-exported module partition

int f();
```

apart from:

```c++
// Compile without `-internalPartition`
module X:part; // without the flag, this is now an implementation unit

int f() { return 0; }
```

There is no syntactic difference; it is purely metadata. AFAIK, this is
not supported with GCC (one just uses `module X;` in the implementation
unit regardless of the partition).

> In any case it would help a lot to clarify:
> Can all C++ source files expect that treating them the same way works fine?

In CMake, the plan is that C++ code is C++ code. The variety of `.cppm`,
`.ixx`, and other extensions have just extended what CMake considers as
C++ code out of the box. But CMake also plans on scanning all C++
sources to figure out what is going on anyways, so extensions aren't
really that useful (in my personal opinion as a build systems guy).

FWIW, CMake also allows someone to say that `.frob` is a C++ file by
either amending the extension globbing variable or explicitly setting
the source file's `LANGUAGE` property to `CXX`.

> If not, we obviously need different suffixes. But then we should clearly
> say so (without necessarily saying which suffix it is).

Suffixes can help with flags, but not with ordering.

On Wed, Apr 13, 2022 at 23:10:42 +0200, Nico Josuttis via Ext wrote:
> I should add that the fact that we need
> module;
> at the beginning of the global module fragment was only introduced to
> let a file identify itself as module file. If we would require
> different suffixes, that would not have been necessary.
>
> But correct me if I am wrong.

Not all files use that (e.g., I've yet to write `module;` in any example
I've used at least).

--Ben

Received on 2022-04-13 21:43:54