LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   NOOBIE C++ Question (https://www.linuxquestions.org/questions/linux-newbie-8/noobie-c-question-830222/)

donaldfarkas 09-03-2010 05:51 PM

NOOBIE C++ Question
 
Code:

#include <iostream>


int main()
{

        int number1;
            int number2;
        int sum;
       
        std::cout << "Enter first integer: ";
        std::cin >> number1;
               
        std::cout << "Enter second integer: ";
        std::cin >> number2;

        sum = number1 + number2;

        std::cout << "Sum is " << sum << std::end1;
       
        return 0;

}

im going by the book and im having some issues here it wont compile successfully the error is:
Code:

[Donald@localhost ~]$ cd Desktop/C++
[Donald@localhost C++]$ make addition
make: *** No rule to make target `addition'.  Stop.


AlucardZero 09-03-2010 05:53 PM

Do you even have a Makefile created in ~/C++ ?

donaldfarkas 09-03-2010 06:02 PM

no it did not create a executable

alan99 09-03-2010 06:31 PM

A makefile is a kind of a template. It tells gcc how to build a application. It tells gcc what kind of command line options to use, what kind of objects to compile, and other things such as where to look for headers files and shared libraries. If you don't want to bother to learn 'make' you can do as many do, install an IDE such as Eclipse. That is what I did.

donaldfarkas 09-03-2010 06:38 PM

a make file? i didnt use it for my previous program to compile why do i need it now? they are just console programs

alan99 09-03-2010 06:55 PM

You don't really have to have one (it makes it easier if you have a lot of objects that are being compiled and linked into the final program) if you are just compiling one source file, you can use g++ on the comand line if you use the right options.

alan99 09-03-2010 06:59 PM

By the way, I compiled your program and it had an error in it. I'll let you figure out what it is when you get it to compile. That the 'fun' part of learning to program.

AlucardZero 09-03-2010 07:09 PM

So, why are you running make?

donaldfarkas 09-03-2010 08:12 PM

Quote:

Originally Posted by alan99 (Post 4087461)
By the way, I compiled your program and it had an error in it. I'll let you figure out what it is when you get it to compile. That the 'fun' part of learning to program.

yea ifound it to endl it was a 1 instead of the L but still did not fix my problem i think something is wrong with my compiler

crosstalk 09-03-2010 08:25 PM

When you run "make", it tries to read a file (called a makefile, usually named "Makefile"). This file contains information on how to compile a program.

Since you have no Makefile, make does not know how to compile the program. You could either do it manually, with g++, or you can write a Makefile (there's also cmake, autotools, and several other pieces of software I don't know how to use.)

"g++" is the C++ compiler. You can invoke it to compile this program with:
Code:

g++ addition.cpp -o addition
This will produce this program as an executable.

A makefile contains targets. For example, to compile a program named "main" from the file "main.cpp", you could add as a rule (make is picky on tabs vs. spaces):
Code:

main: main.cpp
<tab>g++ main.cpp -o main

Most makefiles start (i.e. it must be first) with the target "all", as a convention:
Code:

all: main
and include a "clean" target:
Code:

clean:
<tab>rm -f main

You could compile by hand, with g++, or you could read up on Makefiles. I hope this has enough information for you to write a makefile for this program.

And, as alan99 said, there is an error in the program. You'll figure it out.

Post any issues you have if you try to write a Makefile for this project.

EDIT: you found the error with "end1" vs "endl"

donaldfarkas 09-03-2010 10:07 PM

Quote:

Originally Posted by crosstalk (Post 4087506)
When you run "make", it tries to read a file (called a makefile, usually named "Makefile"). This file contains information on how to compile a program.

Since you have no Makefile, make does not know how to compile the program. You could either do it manually, with g++, or you can write a Makefile (there's also cmake, autotools, and several other pieces of software I don't know how to use.)

"g++" is the C++ compiler. You can invoke it to compile this program with:
Code:

g++ addition.cpp -o addition
This will produce this program as an executable.

A makefile contains targets. For example, to compile a program named "main" from the file "main.cpp", you could add as a rule (make is picky on tabs vs. spaces):
Code:

main: main.cpp
<tab>g++ main.cpp -o main

Most makefiles start (i.e. it must be first) with the target "all", as a convention:
Code:

all: main
and include a "clean" target:
Code:

clean:
<tab>rm -f main

You could compile by hand, with g++, or you could read up on Makefiles. I hope this has enough information for you to write a makefile for this program.

And, as alan99 said, there is an error in the program. You'll figure it out.

Post any issues you have if you try to write a Makefile for this project.

EDIT: you found the error with "end1" vs "endl"


Thank you so much for your time for writing all this up!!! So your saying if i want to by-pass these problems i can just use
Code:

g++ addition.cpp -o addition
while my programs are quite simple. and when i get into more complex programs then i might have to use a makefile.

Kenny_Strawn 09-03-2010 10:11 PM

If you need to compile the C++ file, just use g++ if it's one file, like so:

Code:

$ g++ -Wall -Wextra -o addition addition.cpp

crosstalk 09-03-2010 10:16 PM

Quote:

Originally Posted by donaldfarkas (Post 4087566)
So your saying if i want to by-pass these problems i can just use
Code:

g++ addition.cpp -o addition
while my programs are quite simple. and when i get into more complex programs then i might have to use a makefile.

That is correct, although you (almost) never have to use a makefile (make is just used for convenience.) Just remember -- many programs have hundreds of source files, and running "g++" (and any other pieces of software, such as a linker) on all of them would be quite tiresome without make.

Glad I could help. Good luck learning C++.

MTK358 09-04-2010 06:48 AM

Also, make compiles only the files that changed, saving time for big multi-file projects.

donaldfarkas 09-04-2010 08:28 AM

[Donald@localhost ~]$ cd Desktop/C++
[Donald@localhost C++]$ ls
addition.cpp printingLineofText
[Donald@localhost C++]$ g++ addition.cpp -o addition
g++: addition.cpp: No such file or directory
g++: no input files
[Donald@localhost C++]$ ^C
[Donald@localhost C++]$

so above paste shows that i have navigated to the right directory and showing that the file is thereand then i try to compile and shows no such file


All times are GMT -5. The time now is 10:29 PM.