C项目:使用共享源编译多个应用程序 [英] C project: compiling multiple applications with shared sources

查看:61
本文介绍了C项目:使用共享源编译多个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一个有多个应用程序的项目.这些应用程序在源文件方面共享一些通用功能.每个应用程序都应该有自己的make文件,但能够从一个公共位置链接这些共享源文件.

I am starting a project which has several applications. These applications share some common functionality in terms of source files. Each application should have its own make file but be able to link in these shared source files from a common location.

为了说明这一点,这是我的目录布局:

To illustrate this, here's my directory layout:

project/
    lib/
        shared1/
            shared1.c
            shared1.h
        shared2/
            shared2.c
            shared2.h
    app1/
        app1.c
        app1.h
        makefile
    app2/
        app2.c
        app2.h
        makefile

目前,其中一个makefile文件看起来像这样:

Currently one of the makefiles looks a bit like this:

CC=gcc
XXXCFLAGS=-Wall -Wextra -pedantic -I../../lib/shared1
CFLAGS=-I. $(XXXCFLAGS)
DEPS = app2.h ../../lib/shared1/shared1.h
OBJ = ${DEPS:.h=.o}
LIBS=-lpthread

%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

app2: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

  1. 这是管理共享源的大致正确方法,还是有更好的方法.是否应该将它们分别编译为库?如果是这样,怎么办?

  1. Is this roughly the right way to go about managing shared source, or is there a better way. Should they be separately compiled libraries? If so, how?

这样的相对路径可以吗?

Are relative paths like this ok?

我应该对这种东西使用自动工具吗?看完示例之后,我看不到如何链接共享代码或库.

Should I be using autotools for something like this? After looking at examples I couldn't see how to link shared code or a library.

任何指导都值得赞赏.

推荐答案

这是布局源代码树的一种合理方法,尽管您可能会考虑将源文件和头文件分开.如果要构建库(如果许多应用程序使用了shared1),则可以为其编写规则,然后将该规则放入shared1/中的makefile中.有多种使用这种规则的方法,但是在确定要使用库之前,不必担心.

This is a reasonable way to lay out your source tree, although you might consider separating source files and header files. If you want to build a library (if many apps use, say, shared1) you can write a rule for it, and put the rule in a makefile in shared1/. There are several ways to use such a rule, but don't worry about that until you're sure you want a library.

路径是必不可少的.它们将makefile锁定为特定的目录布局,但是如果没有它们,您将无法完全做到这一点.您可以执行的操作是使它们一直处于蠕动状态:

Paths are a necessary evil. They lock the makefile to a particular directory layout, but you can't entirely do without them. What you can do is keep them corralled:

SHARED1LIB = ../../lib/shared1
XXXCFLAGS=-Wall -Wextra -pedantic -I$(SHARED1DIR)
DEPS = app2.h $(SHARED1DIR)/shared1.h

您对依赖项的使用有些特殊.一方面,最好从源列表中导出对象列表,因为您可能有一个没有标题的源或没有源的标题:

Your use of dependencies is a little peculiar. For one thing, it's better to derive the list of objects from the list of sources, since you might have a source without a header or a header without a source:

SRCS = app2.c $(SHARED1DIR)/shared1.c
OBJ = ${SRCS:.c=.o}

第二,您使每个对象文件都依赖 all 标头,这可能是过分的.假设我们假设foo.o将取决于foo.h,并且app2.o取决于shared1.h;那么我们可以写出更好的规则:

Second, you make every object file depend on all headers, which is probably overkill. Suppose we can assume foo.o will depend on foo.h, and also that app2.o depends on shared1.h; then we can write a much better rule:

%.o: %.c %.h
    $(CC) -c -o $@ $< $(CFLAGS)

app2.o: $(SHARED1DIR)/shared1.h

如果您有很多对象并且这种方法太麻烦了,那么有一种非常复杂的方法可以自动处理这些列表,但这是一种高级技术,您最好不要尝试

If you have a lot of objects and this approach is too cumbersome, there is a very sophisticated method of handling these lists automatically, but it's an advanced technique you'd probably better not try just yet.

最后,如果您发现所有应用程序的Makefile看起来都很相似,则可以将所有常见的东西放入project/

Finally, if you find that all of your app makefiles look much alike, you can take all of the common stuff and put it in a makefile in project/

SHARED1LIB = ../../lib/shared1
SHARED2LIB = ../../lib/shared2

CC=gcc
XXXCFLAGS=-Wall -Wextra -pedantic
CFLAGS=-I. $(XXXCFLAGS)
OBJ = ${SRCS:.c=.o}
LIBS=-lpthread

%.o: %.c %.h
    $(CC) -c -o $@ $< $(CFLAGS)

app%: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

然后,应用程序的makefile可以如下所示:

Then the app makefiles can look like this:

SRCS = app2.c $(SHARED1DIR)/shared1.c

app2:
include ../Makefile

XXXCFLAGS += -I$(SHARED1DIR)

app2.o: $(SHARED1DIR)/shared1.h

这篇关于C项目:使用共享源编译多个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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