Difference in including the .cpp file and .h file (with the same content in cpp)?

前端 未结 3 575
心在旅途
心在旅途 2021-01-20 01:58

I\'ve recently started learning cpp from basics and was very much confused with the folowing:

Lets say I have a header( test.h which contains only decla

相关标签:
3条回答
  • 2021-01-20 02:09

    It changes nothing. It's just a convention whether you use a *.h or *.cpp or *.asdasd suffix, as long as it doesn't get compiled by itself.

    Some projects use the .hxx extension for header files and .cc for source file.

    Please, for the good of fellow programmers you'll work with, stick to common conventions and don't put header code in .cpp files.

    0 讨论(0)
  • 2021-01-20 02:10

    #include just does a copy-n-paste of the file you include into the current file. What the file is named doesn't matter one bit - you can name it "foo.exe" if you like; as long as it contains valid source-code in the context where it is included all is well (but please don't use unconventional names, you'll just confuse people).

    0 讨论(0)
  • 2021-01-20 02:21

    what difference it makes?

    The extension of a header file has no effect on anything. You could have just as well named the file test.mpg, .test or just test (changing the include directive obviously), and it would have worked just as well. The extension is for the benefit of the programmer, not the toolchain.

    However, it is a bad idea to name it anything other than .h, .hpp or whatever is your convention. If you name it .mpg, people will think that it is a video, and not realising that it is a header file, try to play it in a media player. If you name it .cpp, people will think that it is a source file and may attempt to compile it or maybe add definitions into it.

    Including a file with the preprocessor is technically just copying contents of one file into another. Nothing more and nothing less. Everything else about them is just convention.

    In makefile, when specifying source file, Can I give my source files with any extension(.fsfs, .xxx) rather than .cpp extension

    Technically yes, however compilers usually use the source file extension to detect the language which they will fail to do in this case, so you would have to specify it explicitly.

    0 讨论(0)
提交回复
热议问题