Week4
Week4
Scientific Computing
Autumn 2024
1
Creating a Program (Program
Development Environment)
3
Creating a Program
• Preprocess your c++ program file
.cpp / .cpp /
.cc / .cc /
.C .C
files files
• removes comments from your program,
• expands #include statements
4
Detour - Conditional Compilation
#if <constant-expression>
cout<<“CS601”; //This line is compiled only if
#endif <constant-expression> evaluates
to a value > 0 while preprocessing
#ifdef identifier
//This line is compiled only if identifier
cout<<“CS601”; is defined before the previous line is
#endif seen while preprocessing.
1. #ifdef identifier1
2. cout<<“Summer”
3. #elif identifier2
4. cout<<“Fall”;
5. #else
6. cout<<“Spring”;
7. #endif
//preprocessor checks if identifier1 is defined. if so,
line 2 is compiled. If not, checks if identifier2 is
defined. If identifier2 is defined, line 4 is compiled.
Otherwise, line 6 is compiled.
Nikhil Hegde 8
defined operator
Example:
#if defined(COMP)
cout<<“Spring”;
#endif
//same as if #ifdef COMP
.cpp / .cpp / .s
.cc / .cc / files
.C .C
files files
10
Creating a Program
• Translate your assembly code to machine code
.cpp / .cpp / .s .o
.cc / .cc / files files
.C .C
files files
11
Creating a Program
• Get machine code that is part of libraries*
.cpp / .cpp / .s .o
.cc / .cc / files files
.C .C
files files
* Depending upon how you get the library code, linker or loader may be involved.
12
Creating a Program
• Create executable
.cpp / .cpp / .s .o
.cc / .cc / files files
.C .C
files files
.cpp / .cpp / .s .o
.cc / .cc / files files
.C .C
files files
15
Creating a Program
• The steps just discussed are „compiled‟ way of
creating a program. E.g. C++
• Interpreted way: alternative scheme where
source code is „interpreted‟ / translated to
machine code piece by piece e.g. MATLAB
• Pros and Cons.
– Compiled code runs faster, takes longer to develop
– Interpreted code runs normally slower, often faster to
develop
16
Creating a Program
• For different parts of the program different
strategies may be applicable.
– Mix of compilation and interpreted – interoperability
• In the context of scientific software, the following
are of concern:
– Computational efficiency
– Cost of development cycle and maintainability
– Availability of high-performant tools / utilities
– Support for user-defined data types
17
Creating a Program - Executable
• a.out is a pattern of 0s and 1s laid out in memory
– sequence of machine instructions
• How do we execute the program?
– ./a.out <optional command line arguments>
18
Makefile or makefile
Nikhil Hegde 20
Makefile - Usage
Nikhil Hegde 21
Makefile - Benefits
Nikhil Hegde 22