Upload
venkatesh-gunti
View
245
Download
0
Embed Size (px)
Citation preview
7/28/2019 Unix Camp Compiling c
1/33
1
C/C++
Compiling@ UM/MCSR
Last modified: September 3, 2008
7/28/2019 Unix Camp Compiling c
2/33
2
Outline
Surfing www.mcsr.olemiss.edu website
Logging into the system viassh
Brief History of C/C++ languages
Basic Structure and Syntax ofC/C++ Programs A quick glance on PICO editor
A detailed look on compilers and compilingcommands
How to run a compiledfile Application of C/C++ Compiling
http://www.mcsr.olemiss.edu/http://www.mcsr.olemiss.edu/7/28/2019 Unix Camp Compiling c
3/33
3
Logging into the systemusing ssh
Logging into the system from Windows: 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 : UserNameClick Connect.
Logging into the system from Unix: Start the Terminal:
Finder Utilities Terminal Type the following command:
ssh UserName@HostNameEnter 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 theSoftware Tab, followed by the Secure Shell link.
If you are a Unix, Linux, or MAC user, ssh will come with theoperating system
http://www.mcsr.olemiss.edu/http://www.mcsr.olemiss.edu/7/28/2019 Unix Camp Compiling c
4/33
4
A Brief History of Clanguage
In the early 1970s, Dennis Ritchie of BellLaboratories was engaged in a project to developnew operating system. C programming languagewas then developed.
In the early 1980's, also at Bell Laboratories,another C++ language was created. This newlanguage was developed by Bjarne Stroustrupand was called C++ which was designed withOOP (Object Oriented Programming) featuresadded to C without significantly changing the Ccomponent.
7/28/2019 Unix Camp Compiling c
5/33
5
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
7/28/2019 Unix Camp Compiling c
6/33
6
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
Comments are set between /* and */
7/28/2019 Unix Camp Compiling c
7/33
7
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
The C pre-processor replaces this directivewith the contents of the stdio.h header filefrom the standard C library.
7/28/2019 Unix Camp Compiling c
8/33
8
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
Every C program must have one mainfunction.
7/28/2019 Unix Camp Compiling c
9/33
9
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
Each variable must be explicitly defined asa specific type.
7/28/2019 Unix Camp Compiling c
10/33
10
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
The stdio library defines theprintf() function for creating output.
7/28/2019 Unix Camp Compiling c
11/33
11
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
The stdio library defines theprintf() function for creating output.
\n is the newline character
7/28/2019 Unix Camp Compiling c
12/33
12
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
The stdio library defines thescanf() function for capturing input.
7/28/2019 Unix Camp Compiling c
13/33
13
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
%d tells scanf() to interpret theinput as a decimal value
7/28/2019 Unix Camp Compiling c
14/33
14
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
The = operator is used forassignment.
The * operator is used formultiplication.
7/28/2019 Unix Camp Compiling c
15/33
15
A Simple C Program/* Take a number multiply it by 10 and display it */
#include
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
23The number multiplied by 10 equals 230
%d tells printf() to treat the value ofthe result variable as a decimal nbr.
7/28/2019 Unix Camp Compiling c
16/33
16
Simple C++ Program/* Take a number multiply it by 10 and display it */
#include
int main(){
int number, result;std::cout
7/28/2019 Unix Camp Compiling c
17/33
17
Simple C++ Program/* Take a number multiply it by 10 and display it */
#include
int main(){
int number, result;std::cout
7/28/2019 Unix Camp Compiling c
18/33
18
Simple C++ Program/* Take a number multiply it by 10 and display it */
#include using namespace std;
int main()
{int number, result;cout
7/28/2019 Unix Camp Compiling c
19/33
19
The 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
7/28/2019 Unix Camp Compiling c
20/33
20
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 beprocessed, compiled and assembled
.h or preprocessor (header) files
7/28/2019 Unix Camp Compiling c
21/33
21
How to run compiled files
The compiling commands create anexecutable file known as a.out unlessspecified otherwise.
To execute your program, type./a.out and press Enter.
7/28/2019 Unix Camp Compiling c
22/33
22
Compilation Details
object.h
object.cpp
main.cpp
object.s
main.s
object.o
main.o
Output
Source code Assembly Machine Code
7/28/2019 Unix Camp Compiling c
23/33
23
A detailed look into Compilersand 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 2800sweetgum
Portland Group, GNU, and MPICH
Compilers on Beowulf Cluster mimosaGNU C Compiler and SUN STUDIO 8
C/C++ Compilers on willow
7/28/2019 Unix Camp Compiling c
24/33
24
Loading the appropriate IntelCompiler Module
Several versions/builds of Intel compilers are available on redwood. Tocompile, you must first pick which compiler version module you want toload, then load it. Before you can use the module command, you mustsource 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 theycorrespond 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
7/28/2019 Unix Camp Compiling c
25/33
25
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 syntaxis the same regardless of whether yoursource file is C or C++.
E i 1 I t l C il
7/28/2019 Unix Camp Compiling c
26/33
26
Exercise 1: Intel Compiler onRedwood
1. If you have an account on redwood, login to it.2. Copy the two example source files from /usr/local/examples/c to your
working directory:A. cd 1 (if using a common class account, cd to your numbered subdirectory)B. cp /usr/local/examples/c/hello.c ./hello.cC. cp /usr/local/examples/c/addtwo.cpp ./addtwo.cpp
3. Source the appropriate modules environment script for your shell:C. . /usr/share/modules/init/bash
4. Use module avail to see which modules are available
5. Load one of the 10.X modulesA. module load c101B. module list
6. Compile/execute the hello.c and addtwo.cppA. icc hello.cB. ./a.outC. icc addtwo.cpp
D. ./a.out7. Clear all loaded modules
A. module listB. module clearC. module list
7/28/2019 Unix Camp Compiling c
27/33
27
Sweetgum 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
7/28/2019 Unix Camp Compiling c
28/33
28
GNU C Compiler and SUN STUDIO8 C/C++ Compilers on willow
gcc file1.ccommand is used tocompile and link a C program onwillow
g++ file1.ccommand is used tocompile and link a C++ program onwillow
7/28/2019 Unix Camp Compiling c
29/33
29
Willow & Common CompilerFlags
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 executablefile called a.out. To execute the C/C++ program, enter:./a.out.
E i 2 C il C/C++
7/28/2019 Unix Camp Compiling c
30/33
30
Exercise 2: Compile C/C++ onwillow
1. Log in to willow using the account: student
2. Change to your numbered working directory: cd 1
3. Compile/execute hello.c using GNU C compiler gcc hello.c
./a.out
4. Compile/execute simpleB.cpp using Suns C++ CC simpleB.cpp
./a.out5. Try to compile hello.c using Suns C compiler
cc hello.c
7/28/2019 Unix Camp Compiling c
31/33
31
Example C/C++ Flagscc
-c Compile or assemble the source files, but do notlink.
-S Stop after the stage of compilation proper
-E Stop after the preprocessing stage
-o newFilename Name executable somethingbesides a.out
-V Show the compiler version (SUN)-v Show the compiler version (GNU)
7/28/2019 Unix Camp Compiling c
32/33
32
Exercise 3: Compiler Options
1. Compile/execute hello.c using GNU C compiler,and name the executable file helloc.exe
gcc hello.c o helloc.exe
./helloc.exe2. Determine what version of the GNU compilers
are installed gcc -v
g++ -v3. Determine version of installed Suns compiler
CC V
/ptmp/studio8/SUNWspro/bin/cc -V
Frequently Asked ?s on C/C++
7/28/2019 Unix Camp Compiling c
33/33
33
Frequently Asked ?s on C/C++Willow
1. How can I compile one or more C/C++ sourcefiles into object files without yet linking into anexecutable program?
2. How can I ensure the compiler will find aC/C++ header file referenced by my program?
3. How can I ensure the compiler will find a pre-compiled module referenced by my programbut residing in a system- or user-defined
archive library?4. 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?pagename=cwillow.inc