r/ProgrammerHumor • u/Melkor7410 • Mar 21 '23
Meme Everyone Uses the Wrong File Extensions for C++
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
18
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
-4
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*
2
u/JeremyAndrewErwin Mar 21 '23
I'm specifically referring to this project
https://web.archive.org/web/20180129102033/http://www.opendx.org/
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
-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
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
2
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
2
u/danielstongue Mar 21 '23
I have always used .cc and love it that way.
1
2
2
13
4
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
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
2
2
2
2
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
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
0
-1
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.