Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Steps in compiling a c++ program in LINUX

Posted on 2000-02-23
4
Medium Priority
?
201 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 100 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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