1 / 21

Introduction to C & C++

Introduction to C & C++. Lecture 10 – library JJCAO. Content. Static lib Dynamic lib Mex Open source libraries. Library. A  library  is a package of code that is meant to be reused by many programs. Typically, a C++ library comes in two pieces:

malina
Download Presentation

Introduction to C & C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to C & C++ Lecture 10 – library JJCAO

  2. Content • Static lib • Dynamic lib • Mex • Open source libraries

  3. Library A library is a package of code that is meant to be reused by many programs. Typically, a C++ library comes in two pieces: 1) A header file that defines the functionality the library is exposing (offering) to the programs using it.2) A precompiled binary that contains the implementation of that functionality pre-compiled into machine language. Note: Some libraries may be split into multiple files and/or have multiple header files.

  4. Libraries are precompiled for several reasons • First, since libraries rarely change, they do not need to be recompiled often. It would be a waste of time to recompile the library every time you wrote a program that used them. • Second, because precompiled objects are in machine language, it prevents people from accessing or changing the source code, which is important to businesses or people who don’t want to make their source code available for intellectual property reasons.

  5. Static & dynamic library • A static library(archive) consists of routines that are compiled and linked directly into your program. When you compile a program that uses a static library, all the functionality of the static library becomes part of your executable. • .lib extension for windows • .a extension for Linux • A dynamic library(shared library) consists of routines that are loaded into your application at run time. When you compile a program that uses a dynamic library, the library does not become part of your executable — it remains as a separate unit. • .dllextension for windows • .so extension for Linux

  6. Create a static lib 1. Win32 console application

  7. 2. Application settings

  8. Call a static lib • Create a project, such as win32 console app • Include header files • Call functions in the lib & • Tell your program where to locate the lib

  9. Create a dll • 1. Win32 console application • 2. Application settings

  10. 3. After the project is created, it defined MYDLL_EXPORTS automatically. How to use it?

  11. #ifdef MYDLL_EXPORTS #define MY_DLL_API __declspec(dllexport) #else #define MY_DLL_API __declspec(dllimport) #endif MY_DLL_API voidprint_dynamic(); class MY_DLL_API PointArray { private: std::vector<Point> pts; int size; public: PointArray(){} voidpush_back( constPoint &p); };

  12. Call a dll • Create a project, such as win32 console app • Include header files • Call functions in the dll & • Tell your program where to locate the lib

  13. Demo

  14. MEX-files • Interface to external programs written in C and Fortran • Dynamically linked subroutines behaving like M-files • But platform-specific (dll for wondows) • Applications: • Pre-existing C and Fortran programs can be called without having to be rewritten • Bottleneck computations, e.g., for-loops, can be recoded in C or Fortran for efficiency

  15. Matlab Data • Only mxArray: • Its type • Its dimensions • The data associated with this array • If numeric, whether the variable is real or complex • If sparse, its indices and nonzero maximum elements • If a structure or object, the number of fields and field names

  16. Build Mex-file C:\Program Files\MATLAB\R2009a\bin\win32\mexopts • Compile: mex filename -f <optionsfile> • Configure: mex–setup • Test your configure • mexyprime.c • yprime(1,1:4) C:\Program Files\MATLAB\R2009a\extern\examples\mex\ yprime.c

  17. Structure of C Mex-File Mex-file = Gateway routine + computational routine • A C MEX-file gateway routine: void mexFunction( intnlhs, mxArray *plhs[], intnrhs, const mxArray *prhs[]) { /* more C code ... */ }

  18. Two simple examples • Scalar_times.c • Matrix_times.c • Important functions: • mexErrMsgTxt("Two input arguments required."); • mxGetPr • mxCreate* functions, like mxCreateDoubleMatrix, mxCreateSparse, or mxCreateString

  19. Advanced Topics • Help • Use m-file sharing same name • Link multiple files • mexcircle.c square.obj rectangle.c shapes.lib • Output: circle. Mexw32 • Memory management • Automatic Cleanup of Temporary Arrays • You are more efficient than the automatic mechanism. • Persistent Arrays • …

  20. Team work • SVN

  21. Competition • ACM/ICPC

More Related