简体   繁体   English

对于 C 项目,如何有效地修补外部库并将其编译到 Makefile 中?

[英]How can I efficiently patch an external library and compile it in a Makefile for a C project?

I'm working on a C project which needs an external open source library.我正在开发一个需要外部开源库的 C 项目。 In particular, it needs a version I patched myself in order to add some needed features.特别是,它需要一个我自己修补的版本,以添加一些需要的功能。

At the moment I'm using a Makefile which expects a statically compiled version of the patched library inside the ./lib folder (let's call it libpatched.a ), and the corresponding header files in ./include/libpatched .目前我正在使用 Makefile ,它需要./lib文件夹中的补丁库的静态编译版本(我们称之为libpatched.a ),以及 ./include/libpatched 中相应的./include/libpatched文件。 The following are the main parts of the aforementioned Makefile:以下是前面提到的Makefile的主要部分:

EXECNAME=MyExecutable

CC=gcc

SRC_DIR=src
OBJ_DIR=obj

SRC=$(wildcard $(SRC_DIR)/*.c)

OBJ=$(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

CFLAGS += -Wall -O2 -Iinclude -Iinclude/libpatched
LDFLAGS += -Llib
LDLIBS += -lpatched

.PHONY: all clean

all: $(EXECNAME)

$(EXECNAME): $(OBJ_CC)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
    $(CC) $(CFLAGS) -c $< -o $@

clean:
    $(RM) $(OBJ_DIR)/*.o

This Makefile is working correctly;这个 Makefile 工作正常; however, I was looking for a more flexible solution, which does not need any statically compiled library before make is called.但是,我正在寻找一种更灵活的解决方案,它在调用make之前不需要任何静态编译的库。 What I would like to accomplish is having a Makefile which does something like:我想要完成的是拥有一个 Makefile ,它执行以下操作:

  1. Download a specific version of the original library (in order to never have any compatibility problem)下载原始库的特定版本(为了永远不会有任何兼容性问题)

  2. Apply a patch using patch and a diff file ( .patch )使用patch和差异文件 ( .patch ) 应用补丁

  3. Compile the patched library (either statically or dynamically) for the current platform, using cmake , as required by the original library根据原始库的要求,使用cmake为当前平台编译补丁库(静态或动态)

  4. Compile my project, using libpatched使用libpatched编译我的项目

Are these steps valid in your opinion, or is there a much better way to handle this need for a patched library?您认为这些步骤是否有效,或者是否有更好的方法来处理对修补库的需求?

If yes, as I'm not an expert at all in creating Makefiles, is there an easy way to reach this goal by simply leveraging on a properly written Makefile?如果是,因为我根本不是创建 Makefile 的专家,是否有一种简单的方法可以通过简单地利用正确编写的 Makefile 来实现这一目标?

Which could be the best way to do so?这可能是最好的方法吗?

Thank you very much in advance.非常感谢您提前。

I've done exactly this before, when building cross compiler etc with my patches for my operating system kernel.在为我的操作系统 kernel 使用我的补丁构建交叉编译器等时,我之前已经这样做了。 You can use the wget or curl commands in the Makefile.您可以在 Makefile 中使用wgetcurl命令。 For example something like例如像

# foo.tar.gz needs to be downloaded
foo.tar.gz:
    wget https://download.source.from/here/foo.tar.gz -O foo.tar.gz

# the makefile requires the downloaded file.
foo_src/CMakeLists.txt: foo.tar.gz
    mkdir -p foo_src
    cd foo_src && tar xfz ../foo.tar.gz

# patch the library if flag not present
foo_patched.flag:
    cd foo_src && patch -p1 ../foo.patch
    touch foo_patched.flag

# this depends on patching
libpatched.a: foo_src/CMakeLists.txt foo_patched.flag 
    cd foo_src && cmake
    cp foo_src/libfoo.a libpatched.a

The Makefile format is very simple - unlike CMake: - the rules just say, "to generate the file on the left. please build the prerequisites on the right side first. Then execute these commands to actually generate the file on the left hand side" Makefile 格式很简单 - 不像 CMake : - 规则只是说,“要生成左侧的文件。请先在右侧构建先决条件。然后执行这些命令来实际生成左侧的文件”

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

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