Solved

Steps in compiling a c++ program in LINUX

Posted on 2000-02-23
4
186 Views
Last Modified: 2010-04-22
My Context:
I have learned ANSI C++ in the Codewarrior IDE on windows.
I run a Mandrake LINUX but am thinking of going to Slackware
(its been mentioned as a good learning source)

My Question:
How do I get from a source/header files to an executable?

(how do I run the compiler, is it always present
 Is there a directory I need to be in, where will my files end up
 Is there an IDE for LINUX, and anything else I need to code . . . )

0
Comment
Question by:rhik
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mattrope earned 50 total points
Comment Utility
Linux generally comes with g++, which is the Gnu C++ compiler.  To compile a program with just a couple of files, you could do this:

g++ -o desired_final_executable_name file1.C file2.C . . .

For large programs, it is better to compile the .C files separately into object (.o) files and then link the object files together.  This can be done as follows:

g++ -c file1.C          (creates file1.o)
g++ -c file2.C          (creates file2.o)

g++ -o final_executable_name file1.o file2.o . . .


Obviously typing each of these commands each time you want to compile and test your program would be very tedious, so it is possible to create a file called a "Makefile" which contains all the dependency information for your project and specifies exactly what files are needed to build the final executable.  The syntax of makefiles is too complex to post here, but you can find lots of info on the Internet.  Basically having a makefile allows you to type "make" and your computer will figure out which source files have changed, only recompile the ones necessary, and build the final executable.

Hope this is what you were looking for...
0
 

Author Comment

by:rhik
Comment Utility
#include <iostream>
#include <string.h>
void main()
{
      cout <<"hello world";

}
 
the above  gives me an error.
" Parse error before '}' "
0
 

Expert Comment

by:Remote040300
Comment Utility
The standard library is defined in a namespace called standard. So, your code should read either:

#include <iostream>
#include <string.h>
void main()
{
std::cout <<"hello world";
}

or (better)

#include <iostream>
#include <string>
using namespace std;

void main()
{
cout <<"hello world";
}

(Stroustrup, C++ Programming Language, Special Edition, pg. 46,47)

I don't see why you should include string or string.h in the above.

Note that it's supposed to be:
int main()
but I don't think this will cause your compiler to report an error. Still, stick to int and return a value to the system (zero if everything goes fine).
0
 

Author Comment

by:rhik
Comment Utility
Thankyou and good answer.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now