c语言创建静态库
All but the simplest C projects will consist of more than one source code file, one for the main function and others for functions used by main. This is purely to make the source code easier to handle than would be the case if all the code were in one large source file.
除了最简单的C项目之外,所有项目都将包含多个源代码文件,一个用于主要功能,另一个用于主要使用的功能。 纯粹是为了使源代码比所有代码都在一个大的源文件中更容易处理。
In this project I will go beyond separating source code into different files by compiling the additional files into static libraries which can be copied to a central location along with their header files and used by many programs, in exactly the same way as the standard library is used.
在这个项目中,我将通过将其他文件编译成静态库,而不是将源代码分成不同的文件,这些库可以与标准库完全相同的方式与它们的头文件一起复制到中央位置,并且可以被许多程序使用。用过的。
This demo consists of the following files which you can clone/download from the Github repository.
该演示包含以下文件,您可以从Github存储库克隆/下载这些文件。
main.c main.c library.h 库 library.c 库To concentrate on the central purpose of demonstrating how to create a static library I will keep the library’s actual functionality trivial. It will contain one function which takes an int array as an argument and returns the total.
为了专注于演示如何创建静态库的中心目的,我将使该库的实际功能微不足道。 它将包含一个以int数组为参数并返回总数的函数。
This is library.h.
这是library.h 。
#include<stdlib.h> #include<stdbool.h> #include<stdio.h> #include<math.h> //-------------------------------------------------------- // FUNCTION PROTOTYPES //-------------------------------------------------------- int total(int* data, int size);And now library.c.
现在是library.c 。
//-------------------------------------------------------- // FUNCTION total //-------------------------------------------------------- int total(int* data, int size) { int t = 0; for(int i = 0; i < size; i++) { t+= data[i]; } return t; }And finally main.c which simply creates an array of ints, calls the total function, and prints out the result.
最后main.c简单地创建一个ints数组,调用total函数,并输出结果。
#include<stdio.h> #include<stdlib.h> #include"library.h" //-------------------------------------------------------- // FUNCTION main //-------------------------------------------------------- int main(int argc, char* argv[]) { puts("------------------"); puts("| codedrome.com |"); puts("| Static Library |"); puts("------------------\n"); int data[] = {11,22,33,44,55,66,77,88,99}; int t = total(data, 9); printf("The total is %d\n", t); return EXIT_SUCCESS; }We could now compile and run the code with this:
现在,我们可以使用以下代码编译并运行代码:
gcc main.c library.c -std=c11 -o main ./main
gcc main.c library.c -std=c11 -o main ./main
but the purpose of this project is to compile library.c into a .o file: the first line of the following does this. The next lines compile and run the actual program.
但是该项目的目的是将library.c编译为.o文件:以下内容的第一行就是这样做。 下几行编译并运行实际程序。
gcc -c library.c -std=c11 -o library.o gcc main.c library.o -std=c11 -o main ./main
gcc -c library.c -std=c11 -o library.o gcc main.c library.o -std=c11 -o main ./main
The program output looks like this:
程序输出如下所示:
It may seem that we have just introduced an intermediate stage of compiling library.c for no real purpose. However, you can copy the .h and .o files somewhere central for multiple use, or distribute them to other people to use in their projects.
似乎我们只是出于没有实际目的而引入了一个编译library.c的中间阶段。 但是,您可以将.h和.o文件复制到中央某处以进行多次使用,或将它们分发给其他人以在他们的项目中使用。
Let’s take a look at how to use .h and .o files stored in a common location in your projects.
让我们看一下如何使用存储在项目公用位置的.h和.o文件。
Firstly you can copy the .h files to (on Linux) /usr/local/include, and the .o files to /usr/local/lib. (You will need root privileges to do this.) This is the easiest method as gcc will look in these locations by default so it’s all you need to do. However, if you are actively developing the library you’ll need to copy the file across every time you recompile them. To #include them use this syntax — note use of < and > as you would with standard library headers.
首先,您可以将.h文件复制到(在Linux上) / usr / local / include ,然后将.o文件复制到/ usr / local / lib 。 (您需要root权限才能执行此操作。)这是最简单的方法,因为gcc默认情况下会在这些位置中查找,因此这就是您需要做的所有事情。 但是,如果您正在积极开发库,则每次重新编译文件时都需要复制文件。 要使用此语法#include它们, 请注意 - 和 标准库标头一样 使用 < 和 > 。
#include<library.h>
#include<library.h>
Secondly, you may wish to have the .h and .o files in a different location, particularly if you are actively working on the library as it is being used by various projects. For example you might have the library source code, .h and .o files in /projectlibrary. To use them you can either type the full path like this — note use of quotes as with local header files:
其次,您可能希望将.h和.o文件放在不同的位置,尤其是当您正在积极地使用库时,尤其是由于各种项目正在使用该库时,尤其如此。 例如,您可能在/ projectlibrary中具有库源代码,.h和.o文件。 要使用它们,您可以像这样键入完整的路径- 请注意使用引号和本地头文件一样:
#include“/projectlibrary/library.h”
#include“/projectlibrary/library.h”
or add the location to the gcc command like this:
或将位置添加到gcc命令,如下所示:
gcc -I/projectlibrary
gcc -I/projectlibrary
I would suggest that you use a combination of these options — start off with a custom location where you have your .h and .o files while you are working on them, and then save them to /usr/local/include and /usr/local/lib respectively for long-term use when they are finished.
我建议您使用这些选项的组合-从一个自定义位置开始,在该位置上使用.h和.o文件,然后将它们保存到/ usr / local / include和/ usr /它们分别在本地/ lib中供长期使用。
翻译自: https://medium.com/programming-in-c/creating-a-static-library-in-c-eed8b885dac0
c语言创建静态库