Unix Camp Compiling C
Unix Camp Compiling C
Outline
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
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
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.
4
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
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
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
The C pre-processor replaces this directive with the contents of the stdio.h header file from the standard C library.
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
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
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
10
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
11
A Simple C Program
/* Take a number multiply it by 10 and display it */
#include <stdio.h>
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
12
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
13
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
14
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.
15
#include <iostream>
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
C++ pre-processor directives include different versions of the standard library packages.
16
#include <iostream>
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
std is an object which you can send messages tomessages such as: cout, cin, & endl.
17
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
You can use an objects namespace, to keep from having to specify the name of the object each time you send it a message.
18
19
20
21
Compilation Details
Source code Assembly Machine Code
object.cpp
object.s
object.h main.s main.cpp
object.o
Output main.o
22
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
24
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++.
25
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 . /usr/share/modules/init/bash
3. 4. 5. 6.
Source the appropriate modules environment script for your shell: Use module avail to see which modules are available Load one of the 10.X modules Compile/execute the hello.c and addtwo.cpp
icc hello.c ./a.out icc addtwo.cpp ./a.out module list module clear module list module load c101 module list
A. B.
7.
A. B. C. D. A. B. C.
26
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
27
compile and link a C program on willow g++ file1.c command is used to compile and link a C++ program on willow
28
Compilers located in /ptmp/studio8/SUNWspro/bin GNU C/C++ Compilers, Version 3.3.2 To compile with C/C++, enter: Compilers located in /usr/local/bin
gcc example.c (C) g++ example.c (C++)
If there are no compilation errors this creates an executable file called a.out. To execute the C/C++ program, enter: ./a.out. 29
Compile/execute hello.c using GNU C compiler Compile/execute simpleB.cpp using Suns C++
5.
31
2.
3.
/ptmp/studio8/SUNWspro/bin/cc -V
32
4.