Solved

Steps in compiling a c++ program in LINUX

Posted on 2000-02-23
4
187 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
ID: 2553124
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
ID: 2560127
#include <iostream>
#include <string.h>
void main()
{
      cout <<"hello world";

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

Expert Comment

by:Remote040300
ID: 2681322
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
ID: 2790388
Thankyou and good answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

861 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

25 Experts available now in Live!

Get 1:1 Help Now