Introduction to C & C++
description
Transcript of Introduction to C & C++
![Page 1: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/1.jpg)
Introduction to C & C++
Lecture 10 – library
JJCAO
![Page 2: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/2.jpg)
Content
• Static lib• Dynamic lib• Mex• Open source libraries
![Page 3: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/3.jpg)
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.
![Page 4: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/4.jpg)
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.
![Page 5: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/5.jpg)
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. – .dll extension for windows– .so extension for Linux
![Page 6: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/6.jpg)
Create a static lib
1. Win32 console application
![Page 7: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/7.jpg)
• 2. Application settings
![Page 8: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/8.jpg)
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
![Page 9: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/9.jpg)
Create a dll• 1. Win32 console application• 2. Application settings
![Page 10: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/10.jpg)
3. After the project is created, it defined MYDLL_EXPORTS automatically. How to use it?
![Page 11: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/11.jpg)
#ifdef MYDLL_EXPORTS#define MY_DLL_API __declspec(dllexport)#else #define MY_DLL_API __declspec(dllimport)#endif
MY_DLL_API void print_dynamic();
class MY_DLL_API PointArray{private:
std::vector<Point> pts;int size;
public:PointArray(){}void push_back( const Point &p);
};
![Page 12: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/12.jpg)
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
![Page 13: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/13.jpg)
Demo
![Page 14: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/14.jpg)
MEX-files
1. Interface to external programs written in C and Fortran
2. Dynamically linked subroutines behaving like M-files
3. But platform-specific (dll for wondows)4. 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
![Page 15: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/15.jpg)
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
![Page 16: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/16.jpg)
Build Mex-fileSupported compilers options files
Lcc C Compiler (bundled) lccopts.bat
Microsoft Visual C++ msvc90opts.bat
System ANSI Compiler mexopts.sh
GCC gccopts.sh
• Compile: mex filename -f <optionsfile>• Configure: mex–setup
• Test your configure– mex yprime.c– yprime(1,1:4)
C:\Program Files\MATLAB\R2009a\bin\win32\mexopts
C:\Program Files\MATLAB\R2009a\extern\examples\mex\ yprime.c
![Page 17: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/17.jpg)
Structure of C Mex-File
• A C MEX-file gateway routine:void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* more C code ... */ }
Mex-file = Gateway routine + computational routine
![Page 18: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/18.jpg)
• Two simple examples– Scalar_times.c– Matrix_times.c
• Important functions:– mexErrMsgTxt("Two input arguments required.");– mxGetPr– mxCreate* functions, like mxCreateDoubleMatrix,
mxCreateSparse, or mxCreateString
![Page 19: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/19.jpg)
Advanced Topics
• Help– Use m-file sharing same name
• Link multiple files– mex circle.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– …
![Page 20: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/20.jpg)
Team work
• SVN
![Page 21: Introduction to C & C++](https://reader036.fdocuments.net/reader036/viewer/2022081419/568148a4550346895db5b778/html5/thumbnails/21.jpg)
Competition
• ACM/ICPC