Title: C/C Compiling @ UM/MCSR
1C/C Compiling_at_ UM/MCSR
Last modified September 3, 2008
2Outline
- Surfing www.mcsr.olemiss.edu website
- Logging into the system via ssh
- Brief History of C/C languages
- Basic Structure and Syntax of C/C Programs
- A quick glance on PICO editor
- A detailed look on compilers and compiling
commands - How to run a compiled file
- Application of C/C Compiling
3Logging into the system using ssh
- Logging into the system from Windows
- Start the secure shell client Start-gtPrograms-gtS
SH Secure Shell-gtSecure 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
HostNameUser Name UserName - Click Connect.
- Logging into the system from Unix
- Start the Terminal
- Finder? Utilities ? Terminal
- Type the following command
- ssh UserName_at_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
4A 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.
5A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
6A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
Comments are set between / and /
7A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
The C pre-processor replaces this directivewith
the contents of the stdio.h header file from the
standard C library.
8A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
Every C program must have one main function.
9A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
Each variable must be explicitly defined as a
specific type.
10A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
The stdio library defines the printf() function
for creating output.
11A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
The stdio library defines the printf() function
for creating output.
\n is the newline character
12A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
The stdio library defines the scanf() function
for capturing input.
13A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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 scanf() to interpret the input as a
decimal value
14A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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
-
The operator is used for assignment.
The operator is used for multiplication.
15A Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltstdio.hgt
- 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.
16Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltiostreamgt
- int main()
-
- int number, result
- stdcoutltlt"Type in a number ltlt stdendl
- stdcingtgtnumber
- result number 10
- stdcoutltlt"The number multiplied by 10 equals
ltltresult -
- Sample Program Output
- Type in a number
- 23
- The number multiplied by 10 equals 230
C pre-processor directives include different
versions of the standard library packages.
17Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltiostreamgt
- int main()
-
- int number, result
- stdcout ltlt"Type in a number ltlt stdendl
- stdcin gtgtnumber
- result number 10
- stdcoutltlt"The number multiplied by 10 equals
ltltresult -
- Sample Program Output
- Type in a number
- 23
- The number multiplied by 10 equals 230
std is an object which you can send messages
tomessages such as cout, cin, endl.
18Simple C Program
- / Take a number multiply it by 10 and display it
/ - include ltiostreamgt
- using namespace std
- int main()
-
- int number, result
- cout ltlt"Type in a number ltlt endl
- cingtgtnumber
- result number 10
- coutltlt"The number multiplied by 10 equals
ltltresult -
- Sample Program Output
- Type in a number
- 23
- The number multiplied by 10 equals 230
You can use an objects namespace, to keep from
having to specify the name of the object each
time you send it a message.
19The PICO Editor
- General Command
- Write editor contents to a file Ctrl o
- Save the file and exit pico Ctrl x
- Spell Check Ctrl t
- Justify the text Ctrl j
- Moving around in your file
- Move one character to the right Ctrl f or right
arrow key - Move one character to the left Ctrl b or left
arrow key - Move up one line Ctrl p or up arrow key
- Move down one line Ctrl n or down arrow key
20C/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
21How 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.
22Compilation Details
Source code
Assembly
Machine Code
object.cpp
object.s
object.o
object.h
Output
main.s
main.o
main.cpp
23A detailed look into Compilers and Compiling
commands
- C/C Compilers at UM/MCSR
- Intel C Compiler on redwood
- MIPS C, MIPSpro C, and MIPSpro C version 7.4
compilers on Origin 2800 sweetgum - Portland Group, GNU, and MPICH Compilers on
Beowulf Cluster mimosa - GNU C Compiler and SUN STUDIO 8 C/C Compilers
on willow
24Loading 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 /opt) - Then you use the module command
- module list (to see if any other versions of
compiler modules are loaded) - module purge (to unload any other versions of
compiler modules) - module list (to verify that other versions were
successfully unloaded) - module avail (to see what versions of compiler
modules are available to load) - For example, to load the latest 10.1 version of
the C Compilers - module load c101
- module list
- These are the names of the modules and the
compiler versions they correspond to - 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
25Intel 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.
26Exercise 1 Intel Compiler on Redwood
- If you have an account on redwood, login to it.
- Copy the two example source files from
/usr/local/examples/c to your working directory - cd 1 (if using a common class account, cd to
your numbered subdirectory) - cp /usr/local/examples/c/hello.c ./hello.c
- cp /usr/local/examples/c/addtwo.cpp ./addtwo.cpp
- Source the appropriate modules environment script
for your shell - . /usr/share/modules/init/bash
- Use module avail to see which modules are
available - Load one of the 10.X modules
- module load c101
- module list
- Compile/execute the hello.c and addtwo.cpp
- icc hello.c
- ./a.out
- icc addtwo.cpp
- ./a.out
- Clear all loaded modules
- module list
27Sweetgum and Mimosa
- Sweetgum MIPSPro 7.4 Compilers, version 7.4
- To compile with cc/CC on sweetgum, enter
- CC example.c
- To find out more about compilers, enter
- man cc OR man CC
- 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
28GNU 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
29Willow 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.
30Exercise 2 Compile C/C on willow
- Log in to willow using the account student
- Change to your numbered working directory
- cd 1
- Compile/execute hello.c using GNU C compiler
- gcc hello.c
- ./a.out
- Compile/execute simpleB.cpp using Suns C
- CC simpleB.cpp
- ./a.out
- Try to compile hello.c using Suns C compiler
- cc hello.c
-
31Example C/C Flags
- cc ltflaggt ltfilename.cgt
- -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)
32Exercise 3 Compiler Options
- Compile/execute hello.c using GNU C compiler, and
name the executable file helloc.exe - gcc hello.c o helloc.exe
- ./helloc.exe
- Determine what version of the GNU compilers are
installed - gcc -v
- g -v
- Determine version of installed Suns compiler
- CC V
- /ptmp/studio8/SUNWspro/bin/cc -V
-
33Frequently Asked ?s on C/C Willow
- How can I compile one or more C/C source files
into object files without yet linking into an
executable program? - How can I ensure the compiler will find a C/C
header file referenced by my program? - How can I ensure the compiler will find a
pre-compiled module referenced by my program but
residing in a system- or user-defined archive
library? - How can I add my own module to a library archive
for others on the system to re-use? -
Answers here (or in Advanced C/C Compiling Unix
Camp) http//www.mcsr.olemiss.edu/appssubpage.php
?pagenamecwillow.inc