Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-18-2005, 05:11 PM   #1
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
#include <iostream>
using namespace std;

class Rectangle
   int w;
   int h;
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
#include <iostream>
#include <Rectangle.h>

using namespace std;

int main()
   Rectangle r1;
   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, 05:13 PM   #2
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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 05:14 PM.
Old 07-20-2005, 04:21 AM   #3
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, 04:23 AM   #4
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


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 06:10 PM
Including header files and source files for classes Feenix Programming 8 09-28-2005 11:53 AM
Compiling a header file with gcc Squall Linux - Software 2 04-07-2004 06:16 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 03:24 AM
compiling header files with anjuta santiagom5 Linux - Software 7 01-30-2004 01:59 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 AM.

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