how do you compile c++ in linux?

Page 1 of 1

4 Replies - 1136 Views - Last Post: 11 September 2010 - 08:36 AM

#1 beefstew   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-August 10

how do you compile c++ in linux?

Posted 07 September 2010 - 11:09 PM

Hi everyone this is my first post here and I'm in need of some clarification.

I'm currently learning how to code in c++ and taking a course on it in school.

My problem/question is how do you transfer code so it compiles correctly from a windows machine to a linux machine.
My assignment in finished and compiles correctly on my computer using "microsoft visual c++ 2010 express", problem is my teacher has a linux machine and he tests the code on that machine. When I tried compiling my code on a linux machine I ended up with an error log that made no sense to me. My guess is that the linux machine doesn't have access to all the libraries I used, if so how do I fix this so I can turn my assignment in?

here is my code

#include "stdafx.h"
#include <iostream> 
using namespace std;
double ask_p (double);
void display (double,double,double);
void f_month (double, double);
void n_month (double,double,double);

int main()
{
	const double r = 0.22;
	double b = 5000.00;
	double P = ask_p(P);
	display(r,b,P);
	f_month (r,B)/>;
	n_month (r,b,P);
	system("pause");

	return 0;
}

double ask_p(double P)
{
	cout << "Enter your monthly payment: ";
	cin >> P;
	cout << endl;
	return P;
}

void display (double r, double b, double P)
{
	cout << "r = " << r << endl;
	cout << "b = " << b << endl;
	cout << "P = " << P << endl;
}

void f_month (double r, double B)/>
{
	double fm = (r/12)*b;
	cout << "1 " << fm << " " << b << endl;
}

void n_month (double r, double b, double P)
{
	double fm = (r/12)*b;
	double h = (r/12)*b;
	int c = 2;
	double paid = (b-P+fm);
	double total = fm;
	double n_month = (r/12)*(b-P+fm);
	while (paid > 0){
		cout << c << " " << n_month << " " << paid << endl;
		fm = n_month;
		b = paid;
		n_month = (r/12)*(b-P+fm);
		paid = (b-P+fm);
		total = total + n_month;
		c++;
	}
	total = total + h;
	cout << total << endl;
} 


Is This A Good Question/Topic? 0
  • +

Replies To: how do you compile c++ in linux?

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: how do you compile c++ in linux?

Posted 07 September 2010 - 11:48 PM

** Moved to Computer Support & Operating Systems **

I would recommend issuing man gcc or man g++ at the command line to see what's installed on your specific system, but you can also check out these resources :

http://www.faqs.org/.../GCC-HOWTO.html
http://gcc.gnu.org/

Your compiler is going to fail unless you are using this file ---> #include "stdafx.h"
& if your instructor isn't going to assist you with a missing include header that is specific to the Microsoft Visual suite you are using, then you may want to see about getting your money back for the class. Just my 2¢
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how do you compile c++ in linux?

Posted 08 September 2010 - 12:19 AM

You can create an empty C++ project that won't use precompiled headers or the "stdafx.h" file.

I'm using VS 2008, not 2010, so I hope these instructions are the same:

  • You want to create a new project.
  • from the start page click "Create: Project..."
  • In the project types pane select "Visual C++" and "Win32"
  • In the right pane select "Win32 Console Application"
  • Enter a name for your project in the Name field below the panes.
  • and then click "OK"
  • The Win32 Application Wizard should appear
  • Click "Next"
  • Under the Additional Options line click "Empty project"
  • Click finish.


At this point you have an empty project:

  • Click "Project"on the start page tool bar
  • Then select "Add New Item..."
  • In the left pane select "Code" and in the right pane select "C++ File (.cpp)"
  • Enter the name you want to use for the source file in the Name: textbox below
  • Then click "Add"


Now you can paste, or start writing :), your source code into the empty source file being displayed.

Once you are ready to send your work to your instructor it will be only standard C++, without VS2010 'enhancements' :)

This post has been edited by n8wxs: 08 September 2010 - 12:20 AM

Was This Post Helpful? 2
  • +
  • -

#4 kamboj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-August 10

Re: how do you compile c++ in linux?

Posted 08 September 2010 - 04:45 AM

C++ program can be compiled and run in Linux using g++ or gcc command.
When you invoke GCC (GNU C Compiler), it normally does preprocessing, compilation, assembly and linking for you. For example to

compile hello.cpp program , type the following command:

$ g++ hello.cc

C++ program can be compiled and run in Linux using g++ or gcc command.
When you invoke GCC (GNU C Compiler), it normally does preprocessing, compilation, assembly and linking for you. For example to compile hello.cpp program , type the following command:

$ g++ hello.cc
Was This Post Helpful? 0
  • +
  • -

#5 beefstew   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-August 10

Re: how do you compile c++ in linux?

Posted 11 September 2010 - 08:36 AM

sorry I didn't reply to you guys, I figured it out thanks to your helpful posts. I also rewrote the program in c instead of c++.

here's the code

 #include <stdlib.h>
#include <stdio.h>


double ask_p(double);
void display(double, double, double);
void f_month(double, double);
void n_month(double, double, double);

int main()
{
    const double r = 0.22;
    double b = 5000.00;
    double P = ask_p(P);   
    display(r, b, P);
    f_month(r, B)/>;   
    n_month(r, b, P);

    return 0;
}

double ask_p(double P)
{
    printf("Enter your monthly payment");
    scanf("%lf", &P);
   
    return P;   
}

void display(double r, double b, double P)
{
    printf(" r = %lf\n b = %lf\n p = %lf \n", r, b, P);
}

void f_month(double r, double B)/>
{
    double fm = (r / 12) * b;
    printf(" 1 %lf %lf\n", fm, B)/>;
}

void n_month(double r, double b, double P)
{
    double fm = (r / 12) * b;
    double h = (r /12) * b;
    int c = 2;
    double paid = (b - P + fm);
    double total = fm;
    printf("double total = %lf\n", fm);
    double n_month = (r / 12) * (b - P + fm);
    while (paid > 0) {
        printf("%d %lf %lf\n", c, n_month, paid);
        fm = n_month;
        b = paid;
        n_month = (r/12) * (b - P + fm);
        paid = (b - P + fm);   
       
        printf("total = %lf + %lf\n", total, fm);
        total = total + n_month;
        c++;   
    }   
    total = total + h;
    printf("total=%lf + %lf\n", total, h);
} 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1