cat main.c #include "alib.c" #include "blib.c" int main(){ return a+b; } cat alib.c int a=100;

Slides:



Advertisements
Similar presentations
For(int i = 1; i
Advertisements

Test practice Multiplication. Multiplication 9x2.
CSE 303 Lecture 16 Multi-file (larger) programs
Procedural programming in Java
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
An Introduction to C Adam Gleitman – IAP 2014.
AB 11 22 33 44 55 66 77 88 99 10  20  19  18  17  16  15  14  13  12  11  21  22  23  24  25  26  27  28.
Trace-Based Automatic Parallelization in the Jikes RVM Borys Bradel University of Toronto.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
C Module System C and Data Structures Baojian Hua
1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
THE SCIENTIFIC METHOD. DEFINITION An organized way to solve a problem.
UNIT 13 Separate Compilation.
C Tutorial - Program Organization CS Introduction to Operating Systems.
AP STATISTICS LESSON 6.3 (DAY 1) GENERAL PROBABILITY RULES.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
CCSA 221 Programming in C CHAPTER 15 WORKING WITH LARGER PROGRAMS 1 ALHANOUF ALAMR.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
The Scientific Method. Scientific Method The scientific method is a systematic approach to problem solving. There are seven steps: 1.State the Problem.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Testing Programs with Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Find LCM Least Common Multiple of 3 and 5: List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18,... etc The multiples of 5 are.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Executable program (p1)
What is a compiler? Compiler Source code (e.g. C++) Target code
Introduction to Programming
Karel J Robot Chapter 6.
Factoring if/else code
CS1010 Programming Methodology
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Command-Line Arguments
CS1010 Programming Methodology
Recursion, Tail Recursion
Instructor: Ioannis A. Vetsikas
Solve: 1. 4<
INC 161 , CPE 100 Computer Programming
CSC 253 Lecture 7.
Linking.
More About Objects and Methods
Scope Rules and Storage Types
Welcome to It’s Anybody’s Guess.
Chapter 10: Method Overriding and method Overloading
Adapted from the slides of Prof
Method Overriding and method Overloading
General Probability Rules
Cat.
CSC 253 Lecture 15.
Executable program (p1)
Multiplication Facts 3 x Table.
Presentation transcript:

cat main.c #include "alib.c" #include "blib.c" int main(){ return a+b; } cat alib.c int a=100; cat blib.c int b=200; gcc main.c alib.c blib.c Guess what’s will happen in this attempt to compile

gcc main.c alib.c blib.c /tmp/ccNh9y1f.o:(.data+0x0): multiple definition of `a' /tmp/ccEcTiTd.o:(.data+0x0): first defined here /tmp/ccSETFVj.o:(.data+0x0): multiple definition of `b' /tmp/ccEcTiTd.o:(.data+0x4): first defined here collect2: ld returned 1 exit status Multiple definition occurs

gcc -E main.c alib.c blib.c # 1 "main.c" # 1 " " # 1 "main.c" # 1 "alib.c" 1 int a=100; # 2 "main.c" 2 # 1 "blib.c" 1 int b=200; # 3 "main.c" 2 With “gcc –E” command we can see why it happens. To be continued

int main(){ return a+b; } # 1 "alib.c" # 1 " " # 1 "alib.c" int a=100; # 1 "blib.c" # 1 " " # 1 "blib.c" int b=200; Continuing

gcc main.c gcc -E main.c # 1 "main.c" # 1 " " # 1 "main.c" # 1 "alib.c" 1 int a=100; # 2 "main.c" 2 # 1 "blib.c" 1 int b=200; # 3 "main.c" 2 int main(){ return a+b; } No problem if we do this

cat main.c #include "flib.h" #include "ilib.h" int main(){ float f; int i; f = getf(); i = geti(); return 0; } cat flib.c float x = 1.1; float getf(){ return x; } cat ilib.c int x = 2; int geti(){ return x; } cat flib.h float getf(); cat ilib.h int geti();22 What’s going wrong?

gcc main.c flib.c ilib.c /tmp/cc4K9Q3P.o:(.data+0x0): multiple definition of `x' /tmp/ccXHzNZD.o:(.data+0x0): first defined here collect2: ld returned 1 exit status We can use “static” to solve the problem

cat flib.c float getf(){ static float x = 1.1; return x; } cat ilib.c int geti(){ static int x=2; return x; }