Create and use static library Lib

Source: Internet
Author: User

Create a static library project 1. create a static project MathFucsLib: select the win32 console application> enter the MathFuncsLib Project Name> next> select static library; cancel the default pre-compilation header> finish 2. add the class MyMathFuncs to the static library: [cpp] # pragma once // MathFuncsLib. h namespace MathFuncs {class MyMathFuncs {public: static double Add (double a, double B); static double Substract (double a, double B); static double Multiply (double, double B); static double Divide (double a, double B) ;}}// MathFuncsLib. cpp # include "MathF UncsLib. h "# include <stdexcept> using namespace std; namespace MathFuncs {double MyMathFuncs: Add (double a, double B) {return a + B;} double MyMathFuncs :: substract (double a, double B) {return a-B;} double MyMathFuncs: Multiply (double a, double B) {return a * B;} double MyMathFuncs :: divide (double a, double B) {if (B = 0) {throw new invalid_argument ("B cannot be zero! ");} Return a/B;} 3. confirm that the lib file is generated: Project, property-> Configuration property, General-> configuration type: changed to static library (. lib); compile and generate MathFuncsLib. lib creates a console application that references the static library. 1. create a console application that references the static library: add the project MyExecRefsLib to the same solution: Select win32 console application> enter MyExecRefsLib Project Name> next> select console application; cancel the default pre-compilation header-> finish 2. in the application, use the static library function 2.1 to add the header file directory so that the header file contained in the program exists (that is, it can be found): Project, property-> C/C ++-> General-> additional include directory :.. \ MathFuncsLib or: Project properties-> VC ++ directory-> include directory :.. \ MathFuncsLib2.2 add. reference a project in the lib file, reference-> general properties-> framework and reference-> Add reference-> the project name and project directory of MathFuncsLib are displayed-> OK or, add library directory and additional library: Project, properties-> connector-> General-> additional library Directory: for example, $ (OutDir) project, property-> connector-> input-> additional dependency: MathFuncsLib. lib3. the program uses [cpp] # include <iostream> using namespace std; # include "MathFuncsLib. h "int main () {double a = 7.4; int B = 99; cout <" a + B = "<MathFuncs: MyMathFuncs: Add (a, B) <endl; cout <"a-B =" <MathFuncs: MyMathFuncs: Substract (a, B) <endl; cout <"a * B =" <MathFuncs: MyMathFuncs: Multiply (a, B) <endl; cout <"a/B =" <MathFuncs:: MyMathFuncs: Divide (a, B) <endl; return 0 ;}4. set MyExecRefsLib as the startup project and press Ctrl + F5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.