I am using GCC C++ 11 in CodeBlocks IDE. I have been trying to reuse classes that I wrote by putting them into a header file but it doesn't work. I have looked into many books but none has detailed information on making C++ code reusable.
-
This technically doesn't have to do with anything with Java aside from a Java concept, and Java code is not an expected answer, so I've gone ahead and removed that tag. – Compass Oct 29 '14 at 16:34
-
3Why is making a header file "more complicated"? Typing `.h` instead of `.java` when saving it? or what? – The Paramagnetic Croissant Oct 29 '14 at 16:34
-
1No, there's nothing simpler than headers (although there are people working on a "modules" proposal for a future C++). If you have any specific problems defining classes in headers, and can't find the answer in your C++ book, then please ask specific questions about it. – Mike Seymour Oct 29 '14 at 16:35
-
You have to put class definition with functions and members in header, and function code in source file. Then share the header file to reuse the class. That's just the way it works. – Neil Kirk Oct 29 '14 at 16:36
-
2`I have looked into many books but none has detailed information on making C++ code reusable` I think you need to look at more books: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – EdChum Oct 29 '14 at 16:37
-
Thanks Compass. But what I am asking is can you suggest a good book that has details on code re-usability in c++. – Harshit Kaushik Oct 29 '14 at 16:38
-
You need to be more specific about what you mean by code re-usability. – Neil Kirk Oct 29 '14 at 16:39
-
2Asking for resources is off-topic for Stack Overflow. – Compass Oct 29 '14 at 16:40
-
I am just a novice to Stack Overflow.Learning its ways. Your remarks are appreciated Compass. No offence! – Harshit Kaushik Oct 29 '14 at 16:44
-
@NeilKirk We can write a class definition in a package and use it in another program. But I think we can't do the same in C++. You only write prototypes in header file. – Harshit Kaushik Oct 29 '14 at 16:46
-
You can share the source files in the new project? – Neil Kirk Oct 29 '14 at 16:50
-
1If you want to use classes or functions in a .cpp file other than the one they are defined in you need to include a header file with their **declaration** and link against either the object file or library with their **definition**. – sjdowling Oct 29 '14 at 16:53
2 Answers
There are a couple concepts that C++ uses that I think you're missing:
- The difference between a declaration and a definition
#include
statements- Linking against other libraries
In general, one reuses code in C++ by Declaring a function in a header file (.h), Defining it in a source file (.cpp). The header file ("foo.h") is then included in both the source file (foo.cpp) and any other file you want to use something declared in it using and preprocessor include directive #include "foo.h"
. Finally if the source file in which the functions declared in the header file are defined is not a part of the source for the library or executable that you're compiling, you will need to link against the library in which it was built.
Here's a simple example that assumes that you don't need to link against an external library and that all files are in the same directory:
foo.h:
The class Foo is declared along with a member function foobar and a private member variable barM. In this file we're telling the world that there is a class named Foo, it's constructor and destructor are public, it has a member function named fooBar that returns an integer and it also has a private member variable named barM.
class Foo
{
public:
Foo();
~Foo();
int fooBar();
private:
int barM;
};
foo.cpp
The individual member functions for our class Foo are defined, we implement the things we declared in the header file. Notice the include statement at the top
#include "foo.h"
Foo::Foo()
{
barM = 10;
}
Foo::~Foo()
{
}
int Foo::fooBar()
{
return barM;
}
main.cpp We use our class in a different file, again the header file is included at the top.
#include <stdio.h>
#include "foo.h"
int main(int argc, char *argv[])
{
Foo flub;
std::cout << "flub's fooBar is: " << flub.fooBar() << std::endl();
return 0;
}
The expected output from this would be:
flub's fooBar is 10
.
As a general note, I haven't compiled this code, but it should be enough to give you a basic example of the ideas of declarations, definitions, and include statements.
Seeing as you're coming from Java, I'm actually betting that you got all of that already, the hard part is actually using code from a different c++ library, which is akin to Java packages. Setting this up requires exporting the symbols you desired to use in a different library. The way to do this is compiler specific, but is generally accomplished by defining a Macro in a different header file and then using that macro in the declaration of the item you'd like to export. For gcc, see reference GNU Reference Manual.
To extend the above example you create another header file: fooLibExport.h
#if BUILDING_LIBFOO && HAVE_VISIBILITY
#define LIBFOO_DLL_EXPORTED __attribute__((__visibility__("default")))
#elif BUILDING_LIBFOO && defined _MSC_VER
#define LIBFOO_DLL_EXPORTED __declspec(dllexport)
#elif defined _MSC_VER
#define LIBFOO_DLL_EXPORTED __declspec(dllimport)
#else
#define LIBFOO_DLL_EXPORTED
#endif
foo.h would then be changed to:
#include "fooLibExport.h"
class LIBFOO_DLL_EXPORTED Foo
{
public:
Foo();
~Foo();
int fooBar();
private:
int barM;
};
Finally you'll need to link against the library that Foo was built into. Again this is compiler specific. At this point we're through the setting up of header files for exporting symbols from a C++ library so that functions defined in one library can be used in another. I'm going assume that you can follow the reference material for setting up the GCC compiler for the rest of the process. I've tried to bold the key words that should help refine your searches.
One final note about #include
statements, the actual argument isn't just the filename, its the path, relative or absolute, to the file in question. So if the header file isn't in the same file as the file you're trying to include it in, you'll need to use the appropriate path to the file.

- 302
- 1
- 6
Code re-usability casts its net wide in C++ terminology. Please be specific what do you mean by it.C and C++ programming language features usually considered to be relevant to code reuse could be :-
functions, defined types, macros, composition, generics, overloaded functions and operators, and polymorphism.
EDITED IN RESPONSE TO COMMENT:-
Then you have to use header files for putting all declarations which you can use in any file just by including this.

- 10,994
- 1
- 18
- 36
-
Making a personal standard library of classes that I can reuse in another program. – Harshit Kaushik Oct 29 '14 at 16:50