简体   繁体   English

编译使用库的C程序

[英]Compiling C program that uses a library

I've made a C program that uses libsndfile to extract some data from audio files. 我制作了一个使用libsndfile从音频文件中提取一些数据的C程序。

What possibilities are there to make the program as portable as possible, preferably without requiring root access when installing? 有什么可能使程序尽可能可移植,最好在安装时不需要root用户访问权限?

Libsndfile is not available at the target machines, so i need to somehow package it with my program. Libsndfile在目标计算机上不可用,因此我需要以某种方式将其与程序打包。 Is there a way to statically link the library? 有没有办法静态链接库? I've also looked at some Autotools tutorials, but I'm not sure how to proceed. 我也看过一些Autotools教程,但不确定如何继续。

I can compile without a hitch on my dev machine, where I installed the libraries using the package manager: apt-get install libsnfile1-dev 我可以在开发机器上顺利进行编译,而我使用软件包管理器在其中安装了库: apt-get install libsnfile1-dev

The makefile is very simple: makefile非常简单:

CFLAGS=-std=c99 -Wall -pedantic -g
CLIBS= -lsndfile -lm
BIN=audiodecode
CC=gcc

MAIN=main.o

FILES=

OBJS=$(FILES) $(MAIN)

.PHONY: all
all: clean $(OBJS)
    $(CC) $(CFLAGS) -o $(BIN) $(OBJS) $(CLIBS)

.PHONY: clean
clean:
    rm -f *.o $(BIN)

A lot of packages bundle their dependencies. 许多软件包捆绑了它们的依赖项。 Examples: rsync (contains a bundled libpopt), gnupg (contains a bundled libz). 示例:rsync(包含捆绑的libpopt),gnupg(包含捆绑的libz)。 Other dependencies commonly bundled are gettext or glib. 通常捆绑的其他依赖项是gettext或glib。 For inspiration look at how these popular open source projects do it. 为了获得灵感,请看一下这些流行的开源项目是如何做到的。

Put the content of http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25.tar.gz into a subdir and add apropriate rules to build the the subdir first. http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.25.tar.gz的内容放入子目录,并添加适当的规则以首先构建子目录。

Untested sample code: OBJS += libsndfile/libsndfile.a 未经测试的示例代码:OBJS + = libsndfile / libsndfile.a

libsndfile/libsndfile.a:
       cd libsdndfile && ./configure --enable-static && $(MAKE)

For Bonus points add a configure script, that check if the system has already installed libsndfile and link to it dynamically. 对于奖励积分,添加一个配置脚本,该脚本检查系统是否已经安装了libsndfile并动态链接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM