Program Libraries 1. What is a program library? A library is a collection of implementations of behavior, written in terms of a language, that has a well-defined.

Slides:



Advertisements
Similar presentations
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
Advertisements

Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
CSE 303 Lecture 16 Multi-file (larger) programs
Linkage Editors Difference between a linkage editor and a linking loader: Linking loader performs all linking and relocation operations, including automatic.
Linking & Loading CS-502 Operating Systems
Operating System Security : David Phillips A Study of Windows Rootkits.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Advanced Object-Oriented Programming Features
1 ENERGY 211 / CME 211 Lecture 2 September 24, 2008.
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
Debugging, Build and Version Control Rudra Dutta CSC Spring 2007, Section 001.
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Separate Assembly allows a program to be built from modules rather than a single source file.
Implementation Yaodong Bi. Introduction to Implementation Purposes of Implementation – Plan the system integrations required in each iteration – Distribute.
Computing IV Visual C Introduction with OpenCV Example Xinwen Fu.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Java Introduction to JNI Prepared by Humaira Siddiqui.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Geant4 Installation Supported platforms:  Scientific Linux with gcc 4.1.2/4.6  Mac Os X 10.7 and 10.8 with gcc 4.21  Windows7 with Visual Studio.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
Running ORGANON in R Peter Gould and David Marshall Growth Model User Group December 9, 2011.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Saves memory and reduces swapping No recompilation on changes A DLL can provide after-market support Programs written in different languages can call the.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
Introduction to C & C++ Lecture 10 – library JJCAO.
NA-MIC National Alliance for Medical Image Computing Slicer Building and Deployment Steve Pieper, PhD.
CRT State Stuff Dana Robinson The HDF Group. In the next slide, I show a single executable linked to three dlls. Two dlls and the executable were built.
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
UNIT 13 Separate Compilation.
C/C++ Programming Environment
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
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.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1 How to Install OpenGL u Software running under Microsoft Windows makes extensive use of "dynamic link libraries." A dynamic link library (DLL) is a set.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
Multiple File Compilation and linking By Bhumik Sapara.
Win32 Programming Lesson 19: Introduction to DLLs.
Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A Text Editor 3.
Build Tools 1. Building a program for a large project is usually managed by a build tool that controls the various steps involved. These steps may include:
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Visual Programming Borland Delphi. Developing Applications Borland Delphi is an object-oriented, visual programming environment to develop 32-bit applications.
OE-NIK HP Advanced Programming Using and creating DLL files.
Pyragen A PYTHON WRAPPER GENERATOR TO APPLICATION CORE LIBRARIES Fernando PEREIRA, Christian THEIS - HSE/RP EDMS tech note:
Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Introduction to GCC Department of Radio Physics and Electronics, Calcutta University.
Multiple file project management & Makefile
Node.js Modules Header Mastering Node.js, Part 2 Eric W. Greene
CSE791 - Distributed Objects, Spring 2002
CSE775 - Distributed Objects, Spring 2006
Linking & Loading.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
CS-3013 Operating Systems C-term 2008
CS1010 Programming Methodology
CS1010 Programming Methodology
File Management.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Design and Programming
Loaders and Linkers: Features
Machine Independent Features
Linking & Loading CS-502 Operating Systems
Linking & Loading CS-502 Operating Systems
CMPE 135: Object-Oriented Analysis and Design March 14 Class Meeting
Review of Previous Lesson
Outline Announcements Dynamic Libraries
SPL – PS1 Introduction to C++.
Presentation transcript:

Program Libraries 1

What is a program library? A library is a collection of implementations of behavior, written in terms of a language, that has a well-defined interface by which behavior is invoked – Wikipedia A "program library" is simple a file containing compiled code (and data) that is to be incorporated later into a program; program libraries allow programs to be more modular, faster to recompile, and easier to update. - The Linux Documentation Project 2

Program Libraries Program libraries can be divided into three types: static, shared and dynamically loaded. Static libraries are incorporated into a program executable before the program is run. Shared libraries are loaded at program start-up and may be shared by different programs. Dynamically loaded libraries are loaded while the executable is running. 3

Static Libraries Static libraries are collections of object files. This type of library file usually has a ".a" suffix. Static libraries let users link to object files without recompiling the code. It also allow a library to be distributed without releasing source code. 4

Static Libraries Example: my_library.hpp #pragma once namespace my_library { extern "C" void my_library_function(); } my_library.cpp #include #include "my_library.hpp" namespace my_library { void my_library_function() { std::cout << "My Library Function." << std::endl; } 5

Static Libraries Example: main.cpp #include #include "my_library.hpp" int main() { std::cout << "Main Function:" << std::endl; my_library::my_library_function(); } 6

Static Libraries Example – Compiling with object files. $ g++ -c my_library.cpp $ g++ -c main.cpp $ g++ main.o my_library.o –o main $./main Main Function: My Library Function. 7

Static Libraries Example – Compiling as static library. $ g++ -c my_library.cpp $ ar rcs libmy_library.a my_library.o $ g++ -c main.cpp $ g++ main.o libmy_library.a –o main $./main Main Function: My Library Function. $ g++ main.o –l my_library –L./ -o main $./main Main Function: My Library Function. 8

Shared Libraries Shared Libraries are loaded when a program is loaded. Programs can all share access to a shared library and will be upgraded if a newer version of the library is installed. Multiple versions of the library can be installed to allow programs with specific requirements use particular versions of the library. These libraries are usually.so on Linux and.dylib on MAC OS X. 9

Shared Libraries To make all of this functionality work, the shared libraries follow a specific naming convention. Every library has a "soname" which starts with the prefix "lib" followed by the name of the library, the extension ".so" then a period and a version number. 10

Shared Libraries Each shared library also has a "real name" which is the name of the file with the actual library code. This "real name" is the "soname" with additional period and minor version number and optionally a period and release number. These version and release numbers allow the exact version of the library to be determined. 11

Shared Libraries For a single shared library, the system will often have a number of files linking together. For example: soname /usr/lib/libreadline.so.3 linking to a realname like: /usr/lib/libreadline.so.3.0 Should also be: /usr/lib/libreadline.so linking to: /usr/lib/libreadline.so.3 12

Shared Libraries Example – Compiling a shared library $ g++ -fPIC –c my_library.cpp (-fPIC position independent code, needed for library code) $ g++ -shared –W1,-soname,libmy_library.so.1 \ -o libmy_library.so my_library.o –lc $ ln –s libmy_library.so libmy_library.so.1 $ ln –s libmy_library.so.1 libmy_library.so 13

Shared Libraries Example – Using a shared library $ g++ main.cpp –lmy_library –L./ -o main $./main Main Function My Library Function. 14

Shared Libraries The problem with this example is that we have a fixed path for the library. It has been told that the library is going to be in the same directory. The location of shared libraries may differ depending on the system. 15

Shared Libraries $LD_LIBRARY_PATH is one way of temporarily setting a search path for libraries. When a program is launched that requires a shared library, the system will search the directories in $LD_LIBRARY_PATH for that library. However, it is only intended for testing. 16

Shared Libraries If you want your library to be installed into the system you can copy the.so files into one of the standard directories - /usr/lib and run ldconfig Any program using your library can simply use –lmy_library and the system will find it in /usr/lib 17

Dynamically Loaded Libraries Dynamically Loaded Libraries (not to be confused with Dynamic-Link Libraries) are loaded by the program itself inside the source code. The libraries themselves are built as standard object or shared libraries, the only difference is that the libraries aren't loaded at compiler linking phase or start-up but at some point determined by the programmer. 18

Dynamically Loaded Libraries The function: void* dlopen(const char *filename, int flag); Will open a library, prepare it for use and return a handle. void* dlsym(void *handle, char *symbol); Searches the library for a specific symbol. void dlclose(); Closes the library. 19

Dynamically Loaded Libraries #include int main() { std::cout << "Main Function" << std::cout; void *handle = dlopen("libmy_library.so", RTLD_LAZY); if(!handle){/*error*/} void (*lib_fun)(); lib_fun = (void (*)())dlsym(handle,"my_library_function"); if(dlerror() != NULL){/*error*/} (*lib_fun)(); dlclose(handle); } 20

Libraries in Windows Static libraries on Windows have the extension.lib instead of.a and can be created in the same way as Linux/OS X if using MinGW or created through Visual Studio. 21

Libraries in Windows Shared libraries (.dll ) require a few changes. The extra qualifiers: __declspec(dllimport) __declspec(dllexport) Are used to define the interface of the DLL. These can be used to mark functions, data or objects as imported or exported from a DLL. DLLs can also be created from Visual Studio. 22

Summary Three types of libraries: Static Shared Dynamically Loaded Correct use of libraries avoids: Unnecessary work Coupling between projects Slow compile times Large compiled executables Sources: The Linux Documentation Project (tldp.org) 23