Intro_to_C.ppt

advertisement
C/C++
Compiling
@ UM/MCSR
1
Logging into the system
using ssh
•
Logging into the system from Windows:
–
–
•
Logging into the system from Unix:
–
–
•
•
Start the secure shell client:
Start->Programs->SSH Secure Shell->Secure Shell Client
Connect to willow:
From the secure shell window, click Quick Connect.
Then, from the Connect to Remote Host pop-up window, enter:
Hostname
: HostName
User Name
: UserName
Click Connect.
Start the Terminal:
Finder Utilities  Terminal
Type the following command:
ssh UserName@HostName
Enter your password
If you are a windows user and you want to download ssh:
Go to MCSR Web at www.mcsr.olemiss.edu and click on the
Software Tab, followed by the Secure Shell link.
If you are a Unix, Linux, or MAC user, ssh will come with the
operating system
2
A Brief History of C
language
• In the early 1970s, Dennis Ritchie of Bell
Laboratories was engaged in a project to develop
new operating system. C programming language
was then developed.
• In the early 1980's, also at Bell Laboratories,
another C++ language was created. This new
language was developed by Bjarne Stroustrup
and was called C++ which was designed with
OOP (Object Oriented Programming) features
added to C without significantly changing the C
component.
3
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
main()
{
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
4
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
Comments are set between /* and */
main()
{
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
5
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
The C pre-processor replaces this directive
with the contents of the stdio.h header file
from the standard C library.
main()
{
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
6
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
Every C program must have one main
function.
main()
{
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
7
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
main()
Each variable must be explicitly defined as
{
a specific type.
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
8
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
main()
{
The stdio library defines the
int number, result;
printf("Type in a number \n"); printf() function for creating output.
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
9
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
main()
{
The stdio library defines the
int number, result;
printf("Type in a number \n"); printf() function for creating output.
scanf("%d", &number);
\n is the newline character
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
10
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
The stdio library defines the
main()
scanf() function for capturing input.
{
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
11
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
main()
{
int number, result;
printf("Type in a number \n"); %d tells scanf() to interpret the
input as a decimal value
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
12
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
The = operator is used for
main()
assignment.
{
int number, result;
printf("Type in a number \n"); The * operator is used for
scanf("%d", &number);
multiplication.
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
13
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
main()
{
int number, result;
printf("Type in a number \n");
scanf("%d", &number);
result = number *10;
printf("The number multiplied by 10 equals %d\n", result);
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
%d tells printf() to treat the value of
the result variable as a decimal nbr.
14
Simple C++ Program
/* Take a number multiply it by 10 and display it */
#include <iostream>
C++ pre-processor directives include
different versions of the standard
library packages.
int main()
{
int number, result;
std::cout<<"Type in a number “<< std::endl;
std::cin>>number;
result = number *10;
std::cout<<"The number multiplied by 10 equals “<<result;
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
15
Simple C++ Program
/* Take a number multiply it by 10 and display it */
#include <iostream>
std is an object which you can send
messages to—messages such as:
cout, cin, & endl.
int main()
{
int number, result;
std::cout <<"Type in a number “<< std::endl;
std::cin >>number;
result = number *10;
std::cout<<"The number multiplied by 10 equals “<<result;
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
16
Simple C++ Program
/* Take a number multiply it by 10 and display it */
#include <iostream>
using namespace std;
You can use an object’s namespace,
to keep from having to specify the
name of the object each time you
send it a message.
int main()
{
int number, result;
cout <<"Type in a number “ << endl;
cin>>number;
result = number *10;
cout<<"The number multiplied by 10 equals “ <<result;
}
Sample Program Output
Type in a number
23
The number multiplied by 10 equals 230
17
C/C++ source files suffixes
• .cpp, .cc, .c suffixes are used for C++
programs that are to be preprocessed,
compiled and assembled
• .c for C programs that are to be
processed, compiled and assembled
• .h or preprocessor (header) files
18
How to run compiled files
• The compiling commands create an
executable file known as a.out unless
specified otherwise.
• To execute your program, type
./a.out and press Enter.
19
Compilation Details
Source code
Assembly
Machine Code
object.cpp
object.s
object.o
object.h
Output
main.s
main.o
main.cpp
20
A detailed look into Compilers
and Compiling commands
• C/C++ Compilers at UM/MCSR:
– Intel C++ Compiler on redwood
– Portland Group, GNU, and MPICH
Compilers on Beowulf Cluster mimosa
– GNU C Compiler and SUN STUDIO 8
C/C++ Compilers on willow
21
Loading the appropriate Intel
Compiler Module
•
Several versions/builds of Intel compilers are available on redwood. To
compile, you must first pick which compiler version module you want to
load, then load it. Before you can use the module command, you must
source the correct setup file for your shell.
– . /usr/share/modules/init/sh (if using ssh) (There should be a space between .
and /usr)
•
Then you use the module command:
•
For example, to load the latest 10.1 version of the C Compilers:
•
These are the names of the modules and the compiler versions they
correspond to:
–
–
–
–
module
module
module
module
list (to see if any other versions of compiler modules are loaded)
purge (to unload any other versions of compiler modules)
list (to verify that other versions were successfully unloaded)
avail (to see what versions of compiler modules are available to load)
– module load c101
– module list
–
–
–
–
–
–
intel-compilers.7.1.037 for c 7.1
intel-compilers.8.0.042 for c 8.0
intel-compilers.8.0.046 for c 8.0
intel-compilers.9.0.027 for c 9.0
intel-compilers.9.1.046 for c 9.1
intel-compilers.cc.10.1.017 for c 10.1
22
Intel C++ Compiler on redwood
• Intel C/C++ Compilers(7.1, 8.0, 9.0, 9.1
& 10.0)
– Before using the C/C++ Compiler on redwood,
you must first load the appropriate Intel
compiler module.
– Then, to compile:
• icc example.c if using the 8.0 or later
compiler
• ecc example.c if using the 7.1 compiler.
• With Intel compilers, the invocation syntax
is the same regardless of whether your
source file is C or C++.
23
Mimosa
• Mimosa: PGI CDK 7.2 Compilers
– To compile with the C/C++ compilers, enter:
• /usr/local/apps/pgi-7.2/linux86/7.2/bin/pgCC example.c
24
GNU C Compiler and SUN STUDIO
8 C/C++ Compilers on willow
• gcc file1.c command is used to
compile and link a C program on
willow
• g++ file1.c command is used to
compile and link a C++ program on
willow
25
Willow & Common Compiler
Flags
• Sun Studio C/C++ Compilers, Version 5.5:
– To compile with C/C++, enter:
• cc example.c (C)
• CC example.c (C++)
– Compilers located in /ptmp/studio8/SUNWspro/bin
• GNU C/C++ Compilers, Version 3.3.2
– To compile with C/C++, enter:
• gcc example.c (C)
• g++ example.c (C++)
– Compilers located in /usr/local/bin
• Use which to see which compiler version is being found.
– which cc
– which CC
• If there are no compilation errors this creates an executable
file called a.out. To execute the C/C++ program, enter:
./a.out.
26
Exercise 1: Compile C/C++ on
willow
1.
2.
Log in to willow using the account: student
Change to your numbered working directory:
–
3.
Compile/execute hello.c using GNU C compiler
–
–
4.
gcc hello.c
./a.out
Compile/execute simpleB.cpp using Sun’s C++
–
–
5.
cd 1
CC simpleB.cpp
./a.out
Try to compile hello.c using Sun’s C compiler
–
cc hello.c
27
Example C/C++ Flags
cc <flag> <filename.c>
-c Compile or assemble the source files, but do not
link.
-S Stop after the stage of compilation proper
-E Stop after the preprocessing stage
-o newFilename Name executable something
besides a.out
-V Show the compiler version (SUN)
-v Show the compiler version (GNU)
28
Exercise 2: Compiler Options
1.
Compile/execute hello.c using GNU C compiler,
and name the executable file helloc.exe
–
–
2.
Determine what version of the GNU compilers
are installed
–
–
3.
gcc hello.c –o helloc.exe
./helloc.exe
gcc -v
g++ -v
Determine version of installed Sun’s compiler
–
CC –V
–
cc -V
29
Download