Including header files and source files for classes
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
although it was a different define for each file...
and in both class1.cpp and class2.cpp i have:
#include class1/2.h
what is confusing me is to be able to create objects from these classes in main.cpp i had to:
#include "class1.cpp"
but
#include "class2.h"
if i change these lines of code at all, i either get linker errors complaining of multiple definitions or compile errors telling me that they can't find the object i'm trying to construct...
what is also confusing me, is that nowhere in my code did i write:
#include "class2.cpp"
yet the code inside that file is still used and compiled...
is there someone or somewhere i can get the information on what is actually going on behind the scenes here? Any clarification would be greatly appreciated...
class a { public int do_it(); ... }
or
void some_func( int d );
but never definitions like
int a::do_it() { do_something(); }
or
void some_func( int d ) { do_something_else(); }
or
char some_variable;
(it's a different story with inline functions, but that's not important now). Further you never include source files (*.cpp) (except for some very rare cases), only header files.
It seems you forgot some declarations in class1.h, and instead wrote them in class1.cpp. Is that the case?
Further you never include source files (*.cpp) (except for some very rare cases), only header files. [/B]
If you find yourself having to include cpp files then typically one of the following is true:
1. Your missing a declaration in your header file.
2. Your doing some obscure embedded programming with a poor compiler.
3. Your not linking your program properly.
4. You have a major design problem.
I guess the question I would have its.. are the errors your seeing linker errors or compiler errors?
This would also work:
g++ -Wall -o main main.cpp test.cpp
In my first method I build a non-linked object file (the -c option does this) that contains the binary code for the test.cpp class. This binary must be included in the 2nd compile for linking purposes. The 2nd method gets rid of the middle step... but is less ideal if you are building a large project with lots and lots of files.
EDIT:
BTW, to compile that example I did have to change endl to std::endl in the cout statement. It would have been equally correct to just set the namespace to standard by putting in a "use namespace std;" line in main.cpp.
ok, i think you've spotted my mistake... i didn't realise you had to point the compiler to test.o, hence my confusion of how the code in test.cpp was being accessed...
I'm using KDevelop to compile and possibly it's project capabilities are interfering and confusing me, so i'll switch to gcc to compile in future.
Originally posted by jtshaw If you find yourself having to include cpp files then typically one of the following is true:
1. Your missing a declaration in your header file.
2. Your doing some obscure embedded programming with a poor compiler.
3. Your not linking your program properly.
4. You have a major design problem.
I have seen this only once, in glibc, but there it is used very, very often. Entering
grep -r '#include.*\.c[">]' glibc-2.3.5 | wc -l
gives me 1607 lines. I don't know why it is done there, certainly not for point 1., 2., or 3., and probably not for 4.; it seems that sometimes there are good reasons to do so.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.