如何使用头文件和源文件创建 C 库(适用于 Ubuntu)? [英] How to create a C-library (for Ubuntu) out of header- and source-files?

查看:45
本文介绍了如何使用头文件和源文件创建 C 库(适用于 Ubuntu)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种基于具有以下结构的 .h 文件和 .c 文件(它是一个 FEC 库)的程序来创建 C 库(即将文件链接并编译成一个文件): www.openfec.org ).我正在使用 Ubuntu.我希望它在不手动指定每个文件的情况下执行此操作.我尝试过 WAF,但收到错误ERROR:root: error: No module named cflags".

I am looking for a program to create a C-library (i.e. to link and compile the files into one file) based on .h-files and .c-files that have the following structure (it is a FEC-library: www.openfec.org ). I am using Ubuntu. I want it to do this without manually specifying each files. I tried WAF, but got the error 'ERROR:root: error: No module named cflags'.

这是(部分)结构:

fec
 lib_advanced
 ldpc_from_file
   of_code_profile.h
   of_ldpc_ff.h
   ...
 lib_common
   linear_binary_codes_utils
       binary_matrix
       it_decoding
       ml_decoding    

   statistics
   of_cb.h
   of_debug.h
   of_mem.c
   of_mem.h
   of_openfec_api.c
   of_openfec_api.h
   of_openfec_profile.h
   of_types.h       

谢谢!!

推荐答案

*nix 系统(包括所有 linux 发行版)上的 C 库是使用标准工具创建的,该工具是 a) C 编译器b) 链接器 b) ar 实用程序 c) ranlib 实用程序.

C libraries on *nix systems (including all linux distros) are created with standards tools, this tools being a) a C compiler b) a linker b) the ar utility c) the ranlib utility.

C 编译器 99.9% 的时间是 GNU C 编译器,而链接器 ld 以及实用程序 arranlib 是 gnu 系统上 binutils 包的一部分(99.9% 的 linux 系统).

The C compiler 99.9% of the time the GNU C compiler, while the linker ld, and the utilities ar and ranlib are part of the binutils package on gnu systems (99.9% of linux systems).

arranlib 用于创建静态库,将已编译的目标文件(*.o 文件)放入存档文件 libsomething.a 使用 ar 并使用 ranlib 索引存档.

ar and ranlib are used to created static libraries, putting already compiled object files ( *.o files) in an archive file libsomething.a with ar and indexing the archive with ranlib.

可以在 gcc 编译器内部调用链接器以创建具有位置无关代码的动态库,再次将已编译的文件存档在一个特殊的文件中,该文件具有共享对象的 .so 扩展名.

The linker can be called inside the gcc compiler to create dynamic libraries with position independent code, again the already compiled files are archived on a special, this file has the .so extension for shared object.

静态库用于速度和自包含,它们生成大型可执行文件,其中包含最终可执行文件中的所有依赖项.如果单个库有很多更改,要更新它,您必须重新编译所有内容.

Static Libraries are used for speed and self-containment, they produce big executables which contain all their dependencies inside the final executable. If a single library of many changes, to update it you'll have to recompiled everything.

动态库与可执行文件分开编译和链接,它们可以被多个可执行文件同时使用,如果更新一个库,您只需要重新编译一个库,而不是每个依赖它的可执行文件.

Dynamic libraries are compiled and linked separately of the executables, they can be used simultaneously by multiple executables, if one library is updated, you just need to recompile a single library and not every executable which depends on it.

这个工具的使用是通用的和标准的程序,从 *nix 系统到 *nix 系统,它们可能会因一些细节而有所不同,但在 linux 上,您基本上总是使用 GCC 和 Binutils 包.存在以 make、cmake、autotools 等形式提供的额外构建实用程序,以帮助完成此过程,但始终使用基本工具.

The use of this tools are universal and standard procedure, they can vary by few details from *nix systems to *nix system, but on linux you essentially always use the GCC and Binutils packages. Extra build utilities on the form of make, cmake, autotools, etc exist to help on the process, but the basic tools are always used.

通常在最基本的层面上,您编写一个由 make 实用程序解释的 Makefile 脚本.根据您的命令,它可以创建一种或两种库,安装库和可执行文件,卸载它们,清理等

Generally on the most basic level you write a Makefile script which is interpreted by the make utility. And depending on your commands it can make one or both kinds of libraries, install the library and executables, uninstall them, clean up, etc

更多信息:

http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
http://www.dwheeler.com/program-library/Program-Library-如何/

这篇关于如何使用头文件和源文件创建 C 库(适用于 Ubuntu)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆