Solved

Steps in compiling a c++ program in LINUX

Posted on 2000-02-23
4
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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