How to protect one's own intellectual property rights and build a code moat - building one's own static library, examples of x86 and arm platforms

foreword

(1) Imagine that if we were lucky enough to write a package library code, in order to build a moat, our company is unwilling to provide the real code to users. I am afraid that if the customer takes the code, the contract will end, and if we change it a little bit, our technology will be stolen, and then we will say that it is completely independent innovation. That was really painful and hard to describe.
(2) But what? If you don't give your code to the customer, the customer can't use it, and you need to use it. In the end, you will package the entire project. Then this so-called client is making money lying down, and you are losing too much, and it is not realistic at all.
(3) Therefore, in order to protect their own intellectual property rights, they can also provide customer service obediently. We can build a static library ourselves.

Create a static library under the x86 platform

prepare test program

static library program

Here is the static library program I prepared, only one sentence is printed.

#include <stdio.h>

void mylib(void)
{
    
    
        printf("This is mylib!\r\n");
}

test program

(1) Here is the test program, there is a function declaration void mylib(void).
(2) This function declaration can also be placed in a header file, and then this c file includes that header file.
(3) Students who don’t understand explain that they don’t understand the header file of **#include** enough. You can look at #include and header files for a deep understanding of C programs, so that the C project only has .h files (dog head)

void mylib(void);

int main(void)
{
    
    
        mylib();
        return 0;
}

(1) Change the C file into an o file

(1) Let's change the c file into 01 language first, and stop the link in the last step.

gcc -c libtest.c

(2) Turn the o file into a static library

(1) Here we use the ar command to turn the .o file into a static library
(2)Note: It is stipulated that the static library must start with lib and end with .a. This is the rule!

ar cr libmylib.a libmylib.o

(3) Link the test program with the static library

(1) Here we use the ar command to turn the .o file into a static library
(2) It should be noted here that -o indicates the name of the final executable file output. It is not necessary to write, then the final generated executable file has the same name as the c file.
(3) -lmylib represents the name of the static library.libmylib.a is the name of the library file, remove the beginning of lib, and mylib at the end of .a is the real library name. Hence -l + library name.
(4) -L is used to specify the static library path, and the last '.' indicates that the static library is under the current path.

gcc -o test test.c -lmylib -L .

Create a static library under the arm platform

Determine the cross compiler

(1) First you need to know what cross compiler tool you are using. Some of you may have followed the tutorial and already set up the cross compile toolchain in your .bashrc file.
(2) So you need to execute the following command

vim ~/.bashrc

(3) In this file, enter the following command to find out what your own cross-compilation toolchain is.

/CROSS_COMPILE

insert image description here

Create a static library

(1) The test procedure is the same as above, and the execution process is the same. It's just that before executing the gcc and ar instructions, a cross-compilation toolchain needs to be added.

arm-buildroot-linux-gnueabihf-gcc -c libtest.c  //将C文件变成o文件
arm-buildroot-linux-gnueabihf-ar cr libmylib.a libmylib.o  //将o文件变成静态库
arm-buildroot-linux-gnueabihf-gcc -o test test.c -lmylib -L . //将测试程序与静态库链接

What are the benefits of building a static library

(1) Establishing a static library can protect the privacy of one's own code well and allow others to use it. But why does the C library function need to be built as a static library?
(2) Have we found a problem? If we want to see the underlying implementation of a C library function, many times we jump directly to a header file, and the underlying implementation cannot be seen.
(3) This is because the C library has been compiled into a static library. We said above that the static library can protect the privacy of its own code, but isn't the C library open source?
(4) This will involve the second benefit of the static library, which provides compilation efficiency.
(5) The code of the static library has gone through the process of preprocessing, compiling, and assembling, and only the final link is missing. If others want to use the C library, they can directly perform the link operation in the last step, effectively improving the compilation efficiency.

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/132479937