LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-28-2005, 07:59 AM   #1
Feenix
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Rep: Reputation: 0
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.
 
Old 09-28-2005, 09:45 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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?
 
Old 09-28-2005, 09:55 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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?
 
Old 09-28-2005, 10:39 AM   #4
Feenix
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 09-28-2005, 11:29 AM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 09-28-2005, 11:35 AM   #6
Feenix
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 09-28-2005, 11:36 AM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Not a problem, glad you got your stuff working.
 
Old 09-28-2005, 11:44 AM   #8
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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.
 
Old 09-28-2005, 11:53 AM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc header files and cpp files? EchO Linux - Software 3 03-01-2005 02:14 AM
How to write Makefile in 2.6 for including header files in another dir ? ashbalu Programming 0 10-19-2004 04:45 PM
"Cannot find header source files" when trying to install Nvidia 6111 drivers naddad Linux - Newbie 3 08-31-2004 01:42 PM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 03:24 AM
including files with Kdevelop shyam_d_sundar Linux - Newbie 1 03-14-2004 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration