LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-18-2005, 04:11 PM   #1
JinBaba
LQ Newbie
 
Registered: Jul 2005
Posts: 2

Rep: Reputation: 0
Question Compiling C++ Header Files ??


hi Folks!..
I want to ask how to compile c++ header files using g++ compiler i have written the following code for the class
Code:
#include <iostream>
using namespace std;

class Rectangle
{
private:
   int w;
   int h;
public:
   Rectangle()
   {
   w=h=0;
   }
void setDim(int w, int h)
{
   this.w = w;
   this.h = h;
}

void show()
{
   cout<<"The Width is : "<<w<<"\nHeight is : "<<h;
}
   
};
Saved it with name "Rectangle.h" when i try to compile this file with g++ using command
[root@SERVER Code]# g++ Rectangle.h

Following msg is displayed.
g++: compilation of header file requested.
But it is not compiled.And when i try to include it so i may use this class in a program. like..
The code for main() function is like this......stored in file testMain.cpp
Code:
#include <iostream>
#include <Rectangle.h>

using namespace std;

int main()
{
   Rectangle r1;
   r1.setDim(10,20);
   r1.show();
   return 0;
}
when i compile this code with g++ with the following command...

[root@SERVER Code]# g++ testMain.cpp -o testMain

I get the following Errors

testMain.cpp:2:23: Rectangle.h: No such file or directory
testMain.cpp: In function `int main()':
testMain.cpp:8: `Rectangle' undeclared (first use this function)
testMain.cpp:8: (Each undeclared identifier is reported only once for each
function it appears in.)
testMain.cpp:8: parse error before `;' token
testMain.cpp:9: `r1' undeclared (first use this function)

Plz Tell me how to overcome these errors.... and more specificallly how to compile c++ header files.

Regards, JinBaba
 
Old 07-18-2005, 04:13 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
You do not compile header files. your program just doesn't seem to be picking it up. The very first error is your problem, all the others follow from that.

try changing the include to

#include "Rectangle.h"

and make sure there in the same directory. ( angle brackets are for system headers ).

Last edited by leonscape; 07-18-2005 at 04:14 PM.
 
Old 07-20-2005, 03:21 AM   #3
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
Hi there,
I would like to mention that <> usually denotes a absolute path to the header file for the compiler (at least from what I recall) You could try it without <>, like so.

#include "Rectangle.h"

That could solve it., so long as the source you are compiling is in the same DIR as the header file.
 
Old 07-20-2005, 03:23 AM   #4
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
Oh crizap. I should have read the earlier post I guess

My bad
 
  


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
problem compiling c++ header in slack 10.1 tommyj27 Slackware 2 10-28-2005 05:10 PM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
Compiling a header file with gcc Squall Linux - Software 2 04-07-2004 05:16 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM
compiling header files with anjuta santiagom5 Linux - Software 7 01-30-2004 12:59 AM

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

All times are GMT -5. The time now is 11:26 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration