Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the file extension naming convention for c++20 modules?

I have seen .ixx, .cxx, .cpp and .cppm used with c++20 modules.

  • What are the different file extension naming conventions for c++20 modules?
  • For which kind of unit (interface or implementation) is which extension usually used?
like image 281
Kischy Avatar asked Nov 16 '25 20:11

Kischy


1 Answers

I would like to give a more detailed answer, because it may be a good idea to try to avoid future inconsistencies in projects owing to early misunderstandings of a newborn feature.

The fact that GCC uses the name cxx-modules to designate its C++20 module management makes some people think the .cxx extension is the GCC counterpart of Microsoft .ixx. I see a potential future mess with that because when everybody understands what are interface and implementation module units, some developers would feel natural something like choosing .ixx for interfaces, .cxx for implementations, and .cpp for other files that may use modules but are neither interfaces nor implementations (like main.cpp). On the other hand, some other developers have already shown their intention to use .cxx for interfaces and something different (more usual) for implementations or non-module files. This will bring us to situations where different projects give a contradictory meaning of the .cxx extension (interface-specific vs implementation-specific).

Of course, everybody uses the extension (s)he wants for anything, but let's describe at least the complete philosophy of each compiler/development kit in order to give a hint about consistent naming patterns.

  1. Microsoft reserves the .ixx extension for interface module units in its development tools, and uses .cpp for implementation units in its documentation (but I don't know if it would also accept .cc or .cxx instead of .cpp). The compiler also provides the /interface argument to allow declaring a module interface file having any arbitrary extension.

  2. In CLANG, there are four possible extensions for interface module units, which are: .cppm, .cxxm, .c++m and .ccm. They are the respective counterparts for .cpp, .cxx, .c++ and .cc that you should use for implementation module units and non-module units. (Think about it before rushing on .ccm claiming "oh that little cute three-letter extension is perfect for my modules!" 😉)

  3. In GCC there is no special extension for interface module units, so the proper way to distinguish interfaces and implementations is to use the same file extension but to apply a different suffix (for example : gear_interface.cc and gear_implementation.cc). Or alternatively, you may use a new extension specific for interfaces thanks to the -x c++ argument, like .ixx, .ccm, or either the famous .mpp that seems to be more and more in use, despite not officially recognized in any worldwide tool (yet). But please, try not to reserve a historical C++ extension for this purpose; in ten years you would be happy not to have to switch between projects having contradictory file namings. (I distinguish "different" and "contradictory")

Now better informed, feel free to choose/craft your favourite solution.

like image 69
Morgana Avatar answered Nov 19 '25 11:11

Morgana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!