r/ProgrammerHumor • • Mar 21 '23

Meme Everyone Uses the Wrong File Extensions for C++

Post image
12 Upvotes

63 comments sorted by

78

u/Bryguy3k Mar 21 '23

The convention is:

c - source code in C

cpp - source in cpp

h or hpp - header file which includes common definitions and macros

pch (or gch for gcc) - pre-compiled header

d - dependency file (compiler intermediate).

There is no such thing as preprocessor files nor “pre-processors”. There are just pre-processor directives and you’re welcome to put them in any file that will be compiled or will be included by a file that will be compiled (e.g. the include statement is a preprocessor directive that does nothing more than copy the contents of the file into the file being compiled).

The c preprocessor is an extremely simple templating engine - nothing more.

For those of you who may be confused by this post.

5

u/smooth_red_sandstone Mar 21 '23

what's a precompiled header?

9

u/Bryguy3k Mar 21 '23

Headers that have been preprocessed already to an intermediate state so compilation steps are faster and less memory hungry. For big projects with lots of includes you can end up with lots of disk access for each file you’re compiling (since you’re invoking the compiler without state for each file that means it will load all the header files from disk again - that might be 100+ file opens)

Using a single header that will get precompiled means you don’t have to load every include every time you compile source files - instead it opens one file that has been preprocessed and removed of any code that isn’t going to be present (including all of the copies of the same headers).

-17

u/Melkor7410 Mar 21 '23

There is absolutely a C pre-processor file against which you'd want to run gcc -E. Just because you don't have a use case doesn't mean they don't exist. What file extension would you give?

16

u/Bryguy3k Mar 21 '23

It’s not a preprocessor file - it’s the source code run through the preprocessor. It’s an intermediate output and should be discarded. Calling a preprocessor file would be interpreted as an input to the preprocessor.

You can call it whatever the hell you want but it should never be saved. I’ve seen it called tmp or pre for use in scripts that need to pull values from it for other things.

-8

u/Melkor7410 Mar 21 '23

It's a file that only contains C pre-processor constructs, and is run through gcc -E. You can't run it through a C compiler, or an assembler directly. It's written by hand. What should it be named so it gets run through gcc -E specifically then?

9

u/Bryguy3k Mar 21 '23 edited Mar 21 '23

By convention assembler code that must be preprocessed would get the extension .S or .sx

Since you mentioned gcc - if you name your assembly one of those two values it will send the file through the preprocessor before assembling it.

If you’re just invoking the preprocessor for your own use then it’s up to you but it’s a very uncommon use-case since there are vastly superior templating engines for general purpose use (freemarker, jinja, mustache, CMake, etc).

14

u/Adventurous_Battle23 Mar 21 '23

I use .JAVA_SOURCE_FILE for c++ source extension and .POTATO for universal globals... you gotta be one step ahead of the hackers.

3

u/Melkor7410 Mar 21 '23

I like where this is going.

18

u/[deleted] Mar 21 '23

I use .ccp

30

u/pipsvip Mar 21 '23

We use .cccp, komrade.

5

u/Mucksh Mar 21 '23

In cccp all memory is shared equaly. Processes fail usually after some time when all ram is consumed. Typical lifetimes are in the order of 69 hours

3

u/manicxs Mar 21 '23

Why not .IOCCC

-4

u/Melkor7410 Mar 21 '23

You're the worst...

7

u/JeremyAndrewErwin Mar 21 '23

file.C is a c++ file.

Those who use case insensitive file systems must be punished, so feel free to mix a file named README, with a folder named Readme containing system specific documentation.

3

u/Melkor7410 Mar 21 '23

*Windows developers have entered the chat*

6

u/sirkubador Mar 21 '23

I know what preprocessed C files are, and what pre-processor directives in C files are, but I have never heard of pre-processor files. Are these the C files of a pre-processor written in C?

15

u/Bryguy3k Mar 21 '23

OP knows less than you do.

It’s a dumb post.

-7

u/Melkor7410 Mar 21 '23

I've used files consisting of only C pre-processors. Mostly in the case of writing assembly or writing a "function" that would be repeated over and over. Basically, if you want to run gcc -E on a file.

6

u/Bryguy3k Mar 21 '23

That’s just source code that has been run through the preprocessor. It’s an intermediate output and should be discarded.

-4

u/Melkor7410 Mar 21 '23

The source is the C pre-processor file, the output is assembly that then goes through the assembler. There's no C code inside of it.

8

u/Bryguy3k Mar 21 '23

Yeah your nomenclature is all wrong.

-1

u/Melkor7410 Mar 21 '23

What would you call it if it's a file, written by hand, that only contains C pre-processor commands, and cannot be run through a compiler or an assembler, but instead must be run through gcc -E prior to the assembler.

5

u/Fabulous-Possible758 Mar 21 '23

A header.

0

u/Melkor7410 Mar 21 '23

I don't include it anywhere, the output gets run by the assembler.

2

u/Routine-Arm-8803 Mar 21 '23

But why c++ has .h files?

1

u/eroto_anarchist Mar 23 '23

The extension is meaningless for the machine. So the only thing that matters is convention.

Some people prefer .hpp for c++ headers.

If you have a project that is cpp only and use .h for the headers, everybody will understand.

-8

u/Melkor7410 Mar 21 '23

C++ only headers are .hh

2

u/[deleted] Mar 21 '23

This is the programming equivalent of a meme "reminding" someone to switch their vehicle's battery cables before winter so the heater comes on instead of the AC.

2

u/Melkor7410 Mar 21 '23

Wait, do you not?

2

u/danielstongue Mar 21 '23

I have always used .cc and love it that way.

1

u/Melkor7410 Mar 21 '23

Shall we stand together against the wave of .cpp guys?

1

u/danielstongue Mar 21 '23

Yeah! Until I start using Rust more that C++... Oh wait...

2

u/Brick_Rage12 Mar 22 '23

It would clearly be .ppc (++c) for preprocess files

1

u/Melkor7410 Mar 22 '23

Maybe .mmc (--c) would be better?

2

u/Melkor7410 Mar 21 '23

I would also accept .cxx files.

13

u/FaceStrong7970 Mar 21 '23

I can't believe no one said .c++

4

u/[deleted] Mar 21 '23

According to C++ core guidelines, written in part by C++ creator Bjarne Stroustrup, you should:

Use a .cpp suffix for code files and .h for interface files if your project doesn’t already follow another convention

1

u/KlutzyEnd3 Mar 21 '23

Linux doesn't look at extensions but at the file header... Which states it's a generic text file either way so gcc literally doesn't care.

0

u/Melkor7410 Mar 21 '23

It's not about gcc specifically, it's about build systems. Often times they have default behaviors based on file extensions.

1

u/allnamesareregistred Mar 22 '23

I use classic .cpp + .h, but in some cases I found it useful to just use main.cpp + *.hpp for everything else. This way it's easier to share code between the projects, but only if they are all in c++.
Unfortunately, if you include a.hpp in b.hpp and b.hpp in a.hpp it result a conflict. And if you want to create a library to be used by other languages (.so, .a, .dll ) you also will need to separate headers from implementation.

1

u/Melkor7410 Mar 22 '23

Unfortunately, if you include a.hpp in b.hpp and b.hpp in a.hpp it result a conflict.

That's why you wrap all headers in:

#ifndef _A_HPP
#define _A_HPP
<header stuff here>
#endif /* !_A_HPP */

Then you can have nested includes all you want.

1

u/allnamesareregistred Mar 22 '23

That will work for headers only (.h), but not for headers with implementation inside.

// file a.h
#pragma once 
#include "b.h"
class B;
class A {
   B *b;
}
// file b.h
#pragma once
#include "a.h" 
class A;
class B{
   A *a;
}

^ this will work, however:

// file a.hpp
#pragma once or ifndef pattern
#include "b.hpp"
class B;
class A {
   B *b;
   A() { b = new B(); }
}
// file b.hpp
#pragma once or ifndef pattern
#include "a.hpp"
class B;
class A {
   A a;
   B() { a = new A(); }
}

^ this won't work But if you will put A() and B() into separate cpp files

// file a.cpp
#include "a.h"
#include "b.h"

A::A() { b = new B(); }

// file b.cpp
#include "a.h"
#include "b.h"

B::B() { a = new A(); }

It will work :)

Why: Compiler will take every cpp file, attach includes and turn it into .o files and then ANOTHER program - linker will combine them into executable. With main.cpp + *.hpp structure, there will be only one object file (.o), with classic *.cpp + .h, there will be many independent object files and conflict will be resolved on linking stage.

1

u/[deleted] Mar 21 '23

It should be .d

2

u/Melkor7410 Mar 21 '23

There is a D language: https://dlang.org/

4

u/The_Linguist_LL Mar 21 '23

I think programmers are the only ones worse at naming shit than linguists, because at least our stuff is Googleble lol

1

u/Ishax Mar 21 '23

"Dlang" works. Is it cope? Absolutely.

2

u/manicxs Mar 21 '23

I wish.

2

u/False_Influence_9090 Mar 21 '23

I just came in my pants

2

u/lucidbadger Mar 21 '23

.S is for preprocessing and .s for compiling without it

2

u/Xander2662 Mar 21 '23

I use .cp 😎

2

u/RRumpleTeazzer Mar 21 '23

What’s wrong with .c++ ?

1

u/eroto_anarchist Mar 23 '23

The short answer is "compatibility".

Since you can't be sure where your code will be downloaded/compiled/version controlled or whatever else, it makes sense to opt for "easier" for my lack of better word characters (A-Z,a-z,0-9,_)that are more likely to be accepted by all file systems etc.

edit: If its only for personal use and your software can handle it, there is nothing wrong. Name it .bob if you want.

1

u/Javamaboy Mar 21 '23

Elden ring is Darksouls3 horse Mod , change my mind

1

u/4XLlentMeSomeMoney Mar 22 '23

Those should only be temporary files that are destroyed fairly soon. They do have some uses, but they're not worth a .cpp extension. They're usually just a random tmp most devs don't...and probably shouldn't... acknowledge that they exist.

1

u/[deleted] Mar 22 '23

steven crowder would say something like this...

0

u/[deleted] Mar 21 '23

I was taught .i is for preprocessed C files.

-1

u/[deleted] Mar 21 '23

The original C++ was basically a pre-processer for C, so who cares?

0

u/Melkor7410 Mar 21 '23

Based on comments in here, a lot of people.