Here are some problems with compiling and running a quite straightforward C plus plus program (Gnu C++ compile gcc)
wizard:cpp graham$ g++ morethan.cpp
morethan.cpp:9:18: error: ir.inc: No such file or directory
morethan.cpp: In function •[98]int main() •[99]:
morethan.cpp:27: error: •[98]intread •[99] was not declared in this scope
What happened? An "include" file was missing ... it couldn't be found (first error), nor could symbols that were defined in it (second error)
Solution: Copy the include file into the current directory (have you only downloaded part of an application?)
wizard:cpp graham$ g++ morethan.cpp
Undefined symbols:
"intread(char const*)", referenced from:
_main in ccOOqa9r.o
_main in ccOOqa9r.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
What happened? The compile worked, but then you failed to link the compiler output to all the other components needed to make up an application.
Solution: Add the name of the other files that make up the application onto the compile / load command
OR
Solution: Specify just compile (-c option) not compile and load (default)
wizard:cpp graham$ g++ morethan.cpp ir.cpp
wizard:cpp graham$
What happened? It worked ... "no news is good news" BUT where it the output file (the program)
Solution: It's in a file called a.out - that's the [very curious] default
wizard:cpp graham$ a.out
-bash: a.out: command not found
What happened? Your PATH doesn't include the current directory, so when you tried to run a.out it didn't look in the right place for it
Solution: Add current directory to PATH (but be aware of security issues)
wizard:cpp graham$ ./a.out
Enter year of first book 2006
Enter year of second book 2004
First book is newer
Age difference is 2
first book: 2006
second book: 2004
What happened? It worked!
wizard:cpp graham$ g++ -o moreisless morethan.cpp ir.cpp
wizard:cpp graham$
What happened? Compile, link / load and the output file is named this time
wizard:cpp graham$ ./moreisless
Enter year of first book 2010
Enter year of second book 2011
First book is older
Age difference is 1
first book: 2010
second book: 2011
What happened? It worked!
The source code used in this example can be found in our
Introduction to C++ resource bundle (written 2010-11-15, updated 2010-11-16)
Associated topics are indexed under
C051 - C and C based languages - C++ - General [3587] C++ Courses - do I get official certification at the end of my Well House course? - (2012-01-20)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[3155] Rake - a build system using code written in Ruby - (2011-02-03)
[3129] Extra courses - C and C++ - (2011-01-12)
[3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
[2851] Further C++ material - view new or old - (2010-07-04)
[2763] Our C and C++ training course are on Open Source platforms - (2010-05-13)
[2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
[2536] All the Cs ... and Java too - (2009-12-13)
[2504] Learning to program in ... - (2009-11-15)
[2370] C++, Python, and other training - do we use an IDE - (2009-08-21)
C231 - C and C based languages - Introduction to C++ [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3053] Make - automating the commands for building and installing - (2010-11-16)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[2169] When should I use OO techniques? - (2009-05-11)
[2004] Variable Scope in C++ - (2009-01-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[336] Targetted Advertising - (2005-06-05)
[318] Choosing a theme - (2005-05-20)
[317] Programming languages - a comparison - (2005-05-20)
Some other Articles
C++ - a complete example with polymorphism, and how to split it into project filesZyliana Kyrei CoxLonghope HotelGetting your C++ program to runPositively reforming the system - could it be done?Views of Melksham - November 2010Computer Graphics is fun - even if Java Applets are Old HatString handling - from first steps to practical examplesWhat is a universal superclass? Java / Perl / Python / Other OO languages