C++ HelloFriend Guide
Elementary Tutorial for Writing and Running c++. Follows the following steps to run a basic C++ program:
1. create a folder to store your source code, for example:
mkdir -p ~/source/cpp/HelloFriend/src
2. change the current directory to the working folder, for example:
cd ~/source/cpp/HelloFriend
3. create the source code by the following command:
nano src/HelloFriend.cpp
4. In the nano environment, type the source code:
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(int argc, char* argv[]){
string name;
while(true){
cout<<"What is your name (type 'quit' to quit) ? ";
getline(cin,name);
if(name.compare("quit")==0) break;
cout<<"Hello, "<<name<<"!"<<endl;
}
return 0;
}
5. press ctrl-x to leave and save the source code
6. Create the Makefile.am to instruct makefile program to look for sub-folders
nano Makefile.am
7. The source code for the Makefile.am should be:
SUBDIRS=src
8. press ctrl-x to save and exit the nano editor
9. Create the sub-folder Makefile.am, e.g.
nano src/Makefile.am
[/source}
10. Update the sub-folder Makefile.am as followings:
bin_PROGRAMS=HelloFriend
HelloFriend_SOURCES=HelloFriend.cpp
11. Press Ctrl-X to Save and Exit the nano editor.
12. Generate a template config file by invoking the autoscan command:
autoscan
13. A file called configure.scan has been generate for you, change the name to configure.ac and edit it by the following command:
mv configure.scan configure.ac
nano configure.ac
14. Change line 5 of the code into (replace your email accordingly):
AC_INIT([HelloFriend],[1.0],[cloudgen@examplet.org])
15. Add a line below line 5 and type the following:
AM_INIT_AUTOMAKE([foreign])
16. Then your configure.ac should look like:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([HelloFriend],[1.0],[cloudgen@examplet.org])
AM_INIT_AUTOMAKE([foreign])
AC_CONFIG_SRCDIR([src/HelloFriend.cpp])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CXX
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
# Checks for library functions.
AC_CONFIG_FILES([Makefile
src/Makefile])
AC_OUTPUT
17. Prepare the makefile.in and config.h.in by the following command:
autoheader
aclocal
autoconf
automake --add-missing
18. Configure your compiler by the following command:
./configure
19. Compile runnable code by the following command:
make
20. Install the program to your system by the following command,
make install
21. Run the installed program by,
HelloFriend
22. As you run the program, you’ve been asked for your name repeatedly:
What is your name (type 'quit' to quit) ? Peter Pan
Hello, Peter Pan!
What is your name (type 'quit' to quit) ? quit
Read Full Post »