As I know, we can write our c++ library in .h file,.C file and .c &.h file?
can someone describe difference between these 3 library method?
First, a C++ library should no be in a .c file. That indicates C language. C++ source modules should generally have .cpp extension.Then, common to both C and C++ language, a header file should have .h extension to denote that it is a header. Header files are used to define the APIs in your library; i.e., the names of functions and their parameters. The actual bodies of the various functions in your library should be in .cpp files. The API defined in the header file should match the function definitions in the .cpp files.
Related
Posting a basic question about using C style .c and .h class in a C++ application.
I have a library which is meant for C but based on the documentation i can also use for C++.
Should i need to rename the two files as .cpp and .hpp before i start including them in my project ?
I tried to refer existing thread but it talks about other way crom cpp to c.
How to convert C++ Code to C
No you don't. The .h extension is shared.
The implementation file extension depends on the compiler/IDE. For example, MSVS will compile .c files as C source code, and .cpp files as C++. That means you'll have to use
extern "C"
in the header if you use the C functionality in the C++ part of the project.
AFAIK you can compile .c files with g++ as C++, so the extension change is not necessary. Or you can compile them with gcc and use extern "C" again.
I was just wondering what the difference between .cpp and .h files is? What would I use a header file (.h) for and what would I use a cpp file for?
In general, and it really could be a lot less general:
.h (header) files are for declarations of things that are used many times, and are #included in other files
.cpp (implementation) files are for everything else, and are almost never #included
Technically, there is no difference. C++ allows you to put your code in any file, with any format, and it should work.
By convention, you put your declarations (basically, that which makes up your API) in the .h files, and are referred to as "headers". The .cpp files are for the actual "guts" of your code - the implementation details.
Normally, you have the header files included with #include by other files in your project (and other projects, if you're making a library), so the compiler can get the interface required to compile. The implementation, in the .cpp files, is typically implemented so there is one .cpp file "filling in" the implementation per .h file.
By convention, .h files is something that you #include. CPP files are something you add to your project for compiling into separate object file, and then passing to the linker.
The .h file is called the header file. You usually put your interface there (the stuff you want to be public). The cpp file is where you actually implement your interface.
First, both are text files that contain code for the C++ compiler or pre-processor. As far as the system is concerned there is no difference.
By convention different file name extensions are used to indicate the content of files. In C programs you tend to see .h and .c files while in C++ .hpp and .cpp serve the same purposes.
The first group, .h and .hpp files, called header files, contains mostly non-executing code such as definitions of constants and function prototypes. They are added to programs via #include directive and used not only by the program or library in question but by other programs or libraries that will make use of them, declaring interface points and contracts defining values. They are also used to set metadata that may change when compiling for different operating systems.
The second group, .c and .cpp files, contain the executing parts of the code for the library or program.
Correct me if I'm wrong but,
When you #include something, it more-or-less inserts the entire included file into the one with the include command; that is, when I include, say "macros.h" in "genericTools.cpp", the entire contents of "macros.h" is placed in "genericTools.cpp" at that point. This is why you need to use things like "#pragma once" or other protections, to prevent including the same file twice.
Of note, templated code needs to be entirely in the file you're going to be including elsewhere. (I'm unsure of this - can template specializations be ommited from the included files, and linked like a normal function?)
The .cpp that is the implementation file is our actual program or code.
When we need to use different inbuilt functions in our code, we must include the header file that is .h files.
These .h files contains the actual code of the inbuilt functions that we use hence we can simply call the respective functions.
Therefore, while we compile our code we can see more number of lines compiled than what we have actually coded because not only our code is compiled but along with that the (code of the) functions (that are included in .h files) are also compiled.
I am working on a small game using C++, and I used Eclipse CDT's class generator. It created a .h file with the class definitions and a .cpp file that included body-less methods for said class.
So if I followed the template, I'd have a .cpp file filled with the methods declarations, and a .cpp file with method bodies. However, I can't include a .cpp file within another.
So what is the convention in C++ with classes and include files? What I did was fill in the method bodies right under the class declaration in the .h file, and deleted the .cpp file.
You don't have to include the .cpp file. Including the .h file is all it takes. .h means header, ie, all it should have is function / object definitions. The actual implementations go in the .cpp file of the same name. The linker will deal with straightening it out for you.
The header file contains declarations (also known as prototype). Inclusion of the header lets the program know "I declare something that looks like this exists".
The user of headers saves us the effort of declaring methods all over the place in our code files - we just do it once, then import the file.
The .c/.cpp/.cc file includes the definition - which tells the program what the function does.
You do not have to "include" .c files because that's what the compiler does - it compiles all your .c files into machine code.
One more thing you can do is while creating a header file is to use the preprocessor directive
ifdef and endif. This will prevent your header file being included multiple times.
This is a standard practice which I use whenever I create a new header file.
I'm not quite sure I understand. The header files defines what the class is and can do, and you include that into any source files that need to use the class.
The source file implements how the class does its action.
However, you can include a .cpp into another (you can include anything into anything), but you don't need to.
Because I've made .cpp files and then transferred them into .h files, the only difference I can find is that you can't #include .cpp files. Is there any difference that I am missing?
The C++ build system (compiler) knows no difference, so it's all one of conventions.
The convention is that .h files are declarations, and .cpp files are definitions.
That's why .h files are #included -- we include the declarations.
The .cpp file is the compilation unit: it's the real source code file that will be compiled (in C++).
The .h (header) files are files that will be virtually copied/pasted in the .cpp files where the #include precompiler instruction appears. Once the headers code is inserted in the .cpp code, the compilation of the .cpp can start.
.h files, or header files, are used to list the publicly accessible instance variables and methods in the class declaration. .cpp files, or implementation files, are used to actually implement those methods and use those instance variables.
The reason they are separate is because .h files aren't compiled into binary code while .cpp files are. Take a library, for example. Say you are the author and you don't want it to be open source. So you distribute the compiled binary library and the header files to your customers. That allows them to easily see all the information about your library's classes they can use without being able to see how you implemented those methods. They are more for the people using your code rather than the compiler. As was said before: it's the convention.
A header (.h, .hpp, ...) file contains
Class definitions ( class X { ... }; )
Inline function definitions ( inline int get_cpus() { ... } )
Function declarations ( void help(); )
Object declarations ( extern int debug_enabled; )
A source file (.c, .cpp, .cxx) contains
Function definitions ( void help() { ... } or void X::f() { ... } )
Object definitions ( int debug_enabled = 1; )
However, the convention that headers are named with a .h suffix and source files are named with a .cpp suffix is not really required. One can always tell a good compiler how to treat some file, irrespective of its file-name suffix ( -x <file-type> for gcc. Like -x c++ ).
Source files will contain definitions that must be present only once in the whole program. So if you include a source file somewhere and then link the result of compilation of that file and then the one of the source file itself together, then of course you will get linker errors, because you have those definitions now appear twice: Once in the included source file, and then in the file that included it. That's why you had problems with including the .cpp file.
I know the difference between a declaration and a definition.
Whereas:
A CPP file includes the definitions from any header which it includes (because CPP and header file together become a single 'translation unit')
A header file might be included by more than one CPP file
The linker typically won't like anything defined in more than one CPP file
Therefore any definitions in a header file should be inline or static. Header files also contain declarations which are used by more than one CPP file.
Definitions that are neither static nor inline are placed in CPP files. Also, any declarations that are only needed within one CPP file are often placed within that CPP file itself, nstead of in any (sharable) header file.
A good rule of thumb is ".h files should have declarations [potentially] used by multiple source files, but no code that gets run."
By convention, .h files are included by other files, and never compiled directly by themselves. .cpp files are - again, by convention - the roots of the compilation process; they include .h files directly or indirectly, but generally not .cpp files.
Others have already offered good explanations, but I thought I should clarify the differences between the various extensions:
Source Files for C: .c
Header Files for C: .h
Source Files for C++: .cpp
Header Files for C++: .hpp
Of course, as it has already been pointed out, these are just conventions. The compiler doesn't actually pay any attention to them - it's purely for the benefit of the coder.
In the C++ Boost libraries, why is there a ".ipp" extension on some header files?
It seems like they are header files included by the ".hpp" file of the same name.
Is this convention common outside of Boost?
What is the justification for having a special file type?
Explanation from one of the template gurus:
If you want to split up your template sources into interface and
implementation (there are lots of good reasons to do that, including
controlling instantiation), you can't very well use the same name
(foo.hpp) twice, and foo.cpp wouldn't be appropriate for either one.
foo.ipp clearly delineates the file as an implementation file intended to
be #included in foo.hpp.
I believe "ipp" stands from "implementation" file. i.e, they hold actually code (for inline functions & templates) rather than just declaration (which are in the header --.H or .HPP -- files)