Getting your C++ program to run
Archive - Originally posted on "The Horse's Mouth" - 2010-11-15 21:23:57 - Graham EllisHere 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 ‘int main()’:
morethan.cpp:27: error: ‘intread’ 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