Create and use static link library in Qt

1. Create static link library

The project name of the static library

Add included models

Change the class name

My statically compiled library project,

Write a simple static cry code for later test static library use 

cpp code:

#include "staticbuild.h"


StaticBuild :: StaticBuild ()
{

}
int subtract(int x, int y)
{
    return x - y;
}


int StaticBuild::add(int x, int y)
{
    return x + y;
}

 

h code:

#ifndef STATICBUILD_H
#define STATICBUILD_H

#include <istream>
using namespace std;


int subtract(int x, int y);

class StaticBuild
{

public:
    StaticBuild();
    int add ( int x, int y);
};

#endif // STATICBUILD_H

Building (not running) the project generates the corresponding  .lib files.

Note: Debug  Version is  StaticLibd.lib(with d) and Release version is  StaticLib.lib(without d).

After compiling, a static library file will be generated in the compilation path.

 

 

The next step is to use your own static link library

Create a new project to generate a static link library and . h file is tested to the specified path of the project

Add the external static link library to the target project (to select the link of the external library)

Select the location of the external library and the platform to run on and click Next

Then click Next to complete the addition of the external library

Add a header file to the project and write the code

     StaticBuild lib;
     qDebug() << lib.add(2, 3);

     qDebug() << subtract(5, 2);

Run the test to see the results

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325156876&siteId=291194637