4

I am a bit confused by the naming scheme of c++ projects. I have seen people naming the extension of their c++ project either .cc or .cpp. and I also know that the Googles style guide suggests .cc, but with no good reason. Which one should I use?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
procoder36
  • 169
  • 1
  • 6
  • This is a user preference. I use `.cxx` – drescherjm Dec 14 '21 at 13:23
  • 5
    There is no "better". Google's style guide is not given by God or something. Just use whatever suits your style. I personally use `.cpp`. – freakish Dec 14 '21 at 13:24
  • Both are widely used. Either one is fine. – John Kugelman Dec 14 '21 at 13:24
  • Some people/projects use CXX as X is "angled" form +, from C++. It's a matter of convention... and maybe the tools one uses if they require specific file extensions. – alagner Dec 14 '21 at 13:25
  • 1
    I vote for reopen. There are many extension accepted and some that has different meanings. A quick google search hasn't told me which GCC, MSVC, or Clang accepts, and what are the meanings. – Guillaume Racicot Dec 14 '21 at 13:25
  • 1
    I use `.cpp` for source files, `.h` for header files, and `.hh` for header-header files. All the teams I have worked on in the last 30 years have used those extensions. I have seen other projects that have used `.C`, `.cxx`, `.cc`, `.c++`. Use whatever convention your team uses. Or for your own projects, use whichever you prefer. – Eljay Dec 14 '21 at 13:26
  • 1
    @GuillaumeRacicot that's because each of these compilers doesn't care about file extensions, they accept all. I suppose there might be a difference with some Ide, but definitely not a compiler. – freakish Dec 14 '21 at 13:26
  • @freakish not true at all. Specific extension can completely change behaviour and preprocessing, and even more so with modules – Guillaume Racicot Dec 14 '21 at 13:27
  • @GuillaumeRacicot compiler extensions can do pretty much anything they want. Is op asking about compiler extensions? Which ones? – freakish Dec 14 '21 at 13:29
  • 2
    gcc takes a look on extensions! if you try `gcc y.c` with key word `class` inside it will fail, if you run with `gcc y.cpp` it compiles. As long you not manually add the selected language to compile on command line, the extension makes a difference! – Klaus Dec 14 '21 at 13:30
  • 1
    From the list @Eljay gave, the one that should be avoided is `.C`. On case-insensitive OS like (by default) Windows or macOS, it can be a pain to change extension from `.c` to `.C` or vice-versa. – prapin Dec 14 '21 at 13:32
  • @freakish the answer would be that there are no difference between those two so there is no better one. That is objective fact so it fit in a stackoverflow answer. But I would also say that some extension might change how the compiler interpret the file, and list some of those. I would also briefly talk about modules. It completely fits for stack overflow, and I would maybe edit the question to remove the "which one is better". – Guillaume Racicot Dec 14 '21 at 13:32
  • 1
    I concur with prapin. I wasn't judging the merits or problems of using `.C`, just stating that I've seen projects use that convention. – Eljay Dec 14 '21 at 13:33
  • 1
    @Klaus actually `g++` is the C++ compiler. – freakish Dec 14 '21 at 13:33
  • It's largely down to opinion. I use `hpp` and `cpp` for C++ headers and source files. Boost (www.boost.org) do too. Some folk like to use `tpp` for template headers but I don't really understand what a "template header" is. – Bathsheba Dec 14 '21 at 13:33
  • @freakish Actually gcc is the entry point for all as it means "gnu compiler collection" which means gcc can be called for everything if you like! – Klaus Dec 14 '21 at 13:34
  • 3
    Another reason why to reopen this question: The title is extremely searchable, with words that are what most beginners would search when wondering what extension to use. – Guillaume Racicot Dec 14 '21 at 13:39
  • If you like to get a list of what `gcc` can compile take a look at the man page. There you will see a list of expected file name extensions. https://linux.die.net/man/1/gcc – Klaus Dec 14 '21 at 13:41
  • 1
    @Klaus Just like I could read all the man pages for `tar` and know exactly what args to pass, but I search on google and hit the stackoverflow answer that explains how to do what I need. – Guillaume Racicot Dec 14 '21 at 13:49
  • @CoryKramer then it should be marked properly as duplicate, and I would add an answer there (sadly I can't). Only closed is not enough and not constructive. – Guillaume Racicot Dec 14 '21 at 14:06
  • Given the other question is so similar, I cannot ignore that this question is a duplicate. However, the linked question is also closed, I could only edit the answers there and not post any new ones. I did my best to add the missing information. There. – Guillaume Racicot Dec 14 '21 at 14:23

0 Answers0