Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-25-2010, 01:36 AM   #1
Registered: Mar 2009
Posts: 39

Rep: Reputation: 15
how to compile c++ class in linux

hi all ,
i am new in c++ progreming .
i have written lot of classes and main class of c++ in one file and compile successfully but now i have written all classes in different and main class in different file like this.

my one class is "rect.h"

#include <iostream>
using namespace std;

class CRectangle {
int x, y;
void set_values (int,int);

\\this is another "rect.cpp"
#include <iostream>
using namespace std;
#include "rect.h"
void CRectangle::set_values (int a, int b) {
x = a;
y = b;

//this is main class "rect_main.cpp"
#include <iostream>
using namespace std;
#include "rect.h"
int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;

now how to compile and run these classes
Old 05-25-2010, 01:42 AM   #2
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
First of all, you don't need the lines

#include <iostream>
using namespace std;

in either of your class files (rect.cpp and rect.h), because you're not using them in there. Also, you haven't declared the function area() in your class, so your program isn't going to compile.

Secondly, you need to use g++ to compile. You need to pass the main file as well as the class implementation file:

g++ rect_main.cpp rect.cpp -o rect_main

The -o option here sets the name of the executable file. If you don't use it, the executable will be called a.out. Edit: to run the executable, you simply do ./rect_main (the '.' is necessary here, because the current directory (for which '.' is shorthand) is not likely to be in your PATH).

Last edited by Nylex; 05-25-2010 at 01:46 AM.


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
Start jar file from java without being in class path at compile time nazzouri Linux - Newbie 0 02-04-2010 10:47 AM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM
QT class compile error. noteventime Programming 0 11-16-2005 11:06 AM
Compile to .class file AMMullan Programming 1 03-31-2004 04:59 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:52 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