How to create lib (static link library) file with dev-c++

Although dev-c++ is suitable for beginners, its functions are still very powerful. So how to use it to make a lib (static link library)?

Table of contents

Step one: Make a static link library

Step 2: Link the static link library

Method 1: Use projects

Method 2: Modify compilation options

Step 3: Use library functions

method one

Method Two:


Step one: Make a static link library

1. Open dev-c++ and select "New-Project", as shown in the figure below.

2. Select "Static Library", select the programming language (c and c++ does not matter) and set a name for the project.

3. Select the location you want to save.

4. Add functions to the newly created file. I have added two here: one is called hello, which pops up a dialog box; the other is called add, which returns the sum of two numbers.

5. Save the file (you can choose any file name, I chose main.cpp here)

6. Click the "Compile" button.

 After compilation is completed, the following interface may appear:

At this time, some people may find it strange: "There is nothing wrong with my program, why does it report an error?????"

If you look closely, it just displays the command used instead of reporting an error.

7. Find an .a file in the generated file (I am hello.a here, because the project name is hello, yours may be different). This is the generated static link library file.

Step 2: Link the static link library

The library is ready, but how do we use it when programming? The method is to link . There are two methods for linking static link libraries in your own code, each with its own advantages and disadvantages.

Method 1: Use projects

1. Create a new console application project. (It cannot be source code, it must be console application. If you don’t like console application like me , please use method 2)

2. Select the save location.

 3. Open "Project Properties->Parameters".

4. Click "Add Library or Object" to add the library just now.

5. Click "OK" and you're done!

advantage:

1. Only the files of this project will be linked to the library, and other files will not.

2. It is more common online.

shortcoming:

1. Only the console application can be created, but the source code cannot be created. (To be honest, I dislike console applications very much)

Therefore, this method is suitable for linking less commonly used libraries.

Method 2: Modify compilation options

1. Rename your library to "libxxx.a", such as "libhello.a".

2. Create a new source code (no need to use console application this time, I’m so happy!

3. Open "Tools->Compile Options."

4. Find "Add the following command to the connector command line" and add the following command in that box (if there is already a command in the box, add a space at the end and enter the following command):

-l你的库名(去掉开头的lib和结尾的.a)

 For example: my library name is libhello.a (see step 1), then I should add the command:

-lhello

Note that the following three ways of writing are all wrong :

-llibhello.a
-llibhello
-lhello.a

For another example, if your library name is lib test .a, you should add: 

-ltest

Example diagram (taking my library as an example):

  

Can I add a space between -l and the library name? sure!

5. It’s not over yet! Find "Directory->Library".

 6. Add the path to your library. How to add? Click the "Browse" button in the lower right corner, find the path to the library, and click "OK".

7. Click "Add", then click "OK", OK!

advantage:

1. You can create new source code instead of the console application that I hate . . .  

shortcoming:

1. The library will be automatically linked every time, resulting in a huge executable file size.

2. It is not common on the Internet, and it is difficult to solve problems when you encounter them.

Therefore, this approach is suitable for frequently used libraries.

Step 3: Use library functions

All is ready except for the opportunity! The library has been built and the links have been linked. All that remains is to call the library functions! How to call it? There are still two methods.

Note: If you link a static link library in a console application, you can only call library functions in the project file; if you modify the compilation options, every program can call library functions!

method one

1. In the code that needs to call the library function, write the function prototype of the library function. (You must write a function prototype, otherwise an error will be reported!)

 2. Just call the function directly. (The picture below shows the complete code)

 

#include <cstdio>
void hello();
int add(int a,int b);
int main(){
	printf("%d",add(1,2));
	hello();
	return 0;
}

running result:

Advantages: Simple operation.

Disadvantage: Only you know the function prototype. If others use this function, they have to ask you for the function prototype.

Therefore, this method is suitable for using library functions yourself.

Method Two:

1. Write the function prototype in a header file .

 2. Introduce the header file and you can use the library functions!

Advantages: Others can know the function prototype.

Disadvantages: More troublesome.

Therefore, this method is suitable for sharing with others.

Guess you like

Origin blog.csdn.net/nnKevi/article/details/122552542