Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-28-2005, 07:59 AM
|
#1
|
LQ Newbie
Registered: Feb 2005
Posts: 4
Rep:
|
Including header files and source files for classes
Hello everyone, i believe this is my first post on this site, i looked through previous posts but i couldn't find anything explain this to me...
i created a project with 5 files:
main.cpp
class1.h
class1.cpp
class2.h
class2.cpp
in every file but main.cpp i wrapped it with:
#ifndef SOME_DEF_OR_OTHER
#define SOME_DEF_OR_OTHER
...code....
#endif
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...
thankyou for your time.
|
|
|
09-28-2005, 09:45 AM
|
#2
|
Member
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332
Rep:
|
Header files (*.h) contain declarations, like
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?
|
|
|
09-28-2005, 09:55 AM
|
#3
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
Quote:
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?
|
|
|
09-28-2005, 10:39 AM
|
#4
|
LQ Newbie
Registered: Feb 2005
Posts: 4
Original Poster
Rep:
|
i really appreciate your help, heres a quick example i have put together so you might be able to spot my mistake:
//main.cpp
#include <iostream>
#include "test.h"
int main()
{
CLAS clas;
clas.sety(4);
std::cout << clas.gety() << endl;
return 0;
}
//test.h
#ifndef __CLAS_H__
#define __CLAS_H__
class CLAS
{
protected:
int y;
public:
int gety();
bool sety(int newy);
};
#endif
//test.cpp
#ifndef __CLAS__
#define __CLAS__
#include "test.h"
int CLAS::gety()
{
return y;
}
bool CLAS::sety(int newy)
{
y = newy;
return true;
}
#endif
test.cpp compiles ok, except a no main error
when i try to compile main.cpp i get:
[Linker error] undefined reference to `CLAS::sety(int)'
[Linker error] undefined reference to `CLAS::gety()'
thankyou again for your time.
|
|
|
09-28-2005, 11:29 AM
|
#5
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
Ok, I'm not sure what compiler your using here, but this is how I built this on Mac OSX (works just the same in Linux).
g++ -Wall -c -o test.o test.cpp
g++ -Wall -o main main.cpp test.o
Output:
Code:
johnshaw@Quaqmire-OSX ~/test/testdir $ ./main
4
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.
Last edited by jtshaw; 09-28-2005 at 11:34 AM.
|
|
|
09-28-2005, 11:35 AM
|
#6
|
LQ Newbie
Registered: Feb 2005
Posts: 4
Original Poster
Rep:
|
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.
Thankyou very much for your time.
|
|
|
09-28-2005, 11:36 AM
|
#7
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
Not a problem, glad you got your stuff working.
|
|
|
09-28-2005, 11:44 AM
|
#8
|
Member
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332
Rep:
|
Quote:
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.
Last edited by addy86; 09-28-2005 at 11:45 AM.
|
|
|
09-28-2005, 11:53 AM
|
#9
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
I guess I should expand reason 2 or add a reason 5...
5. Lack of runtime support, base libraries, other misc. low level system things.
The point is, if your doing it in application level programming your probably doing something wrong.
|
|
|
All times are GMT -5. The time now is 10:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|