简体   繁体   English

Makefile用于构建链接先前构建的库的多个应用程序

[英]Makefile for building multiple applications linking a previous built library

I think to organize my project as follows: 我认为组织我的项目如下:

my_project 我的项目

|--src | --src

|--include |-包括

|--test |-测试

The core files reside in the directories src and include, whereas the different test applications of the core are within test directory (multiple *.c containing each a main() function). 核心文件位于src目录中并包含目录,而核心的不同测试应用程序位于测试目录中(多个* .c包含每个main()函数)。 The core should be build as static library which will be linked to all applications). 核心应构建为静态库,并将其链接到所有应用程序。

How to write a basic Makefile to match these requirements? 如何编写基本的Makefile来满足这些要求? I already googled and found the following website providing a Makefile template for building an executable 1 . 我已经在Google上搜索了,发现以下网站提供了一个用于生成可执行文件的Makefile模板1 How can this Makefile be extended for my needs? 如何扩展此Makefile以满足我的需求?

If you think my project organization is bad or you have a better idea, let me know! 如果您认为我的项目组织不好,或者您有更好的主意,请告诉我!

Any help is appreciated! 任何帮助表示赞赏!

Thanks, Jonas 谢谢乔纳斯

There is a small project called dotmk which turns the creation of a Makefile very easy. 有一个名为dotmk的小项目,使创建Makefile非常容易。

Here is the link: https://github.com/swrh/dotmk 这是链接: https : //github.com/swrh/dotmk

You just have to run the install script and create the Makefile like the examples 您只需要运行安装脚本并像示例一样创建Makefile

You haven't said where you want the library to go, so I'll assume it should go into src/ where the objects already go. 您没有说要将库放到哪里,所以我假设它应该放到src/对象已经放到的地方。

src/libcore.a: $(OBJECTS)
    $(AR) -cvq $@ $^

It looks as if the makefile you have will build test/testfoo.o from test/testfoo.c so I'll assume that that works. 看来您拥有的makefile将从test/testfoo.o test/testfoo.c因此我认为这是test/testfoo.c And you haven't said where you want the executable tests (eg testfoo ) to go, so I'll put them in test/ . 而且您还没有说出要把可执行测试(例如testfoo )放到哪里,所以我将它们放在test/

test/%: test/%.o src/libcore.a
    $(CC) -o $@ $< -Lsrc -lcore

EDIT: 编辑:

If you want Make to build all of the tests by default, then you should have this before any other rule in the makefile: 如果要让Make默认生成所有测试,则应在makefile中的其他任何规则之前使用它:

TESTS := $(patsubst %.c,%,$(wildcard test/*.c))

all: $(TESTS)

my Makefile now looks as follows: 我的Makefile现在看起来如下:

SHELL = /bin/sh
CC    = gcc
AR    = ar

CFLAGS       = -std=gnu99 -Iinclude -pedantic -Wall -Wextra -march=native -ggdb3
DEBUGFLAGS   = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG -combine -fwhole-program
PROFILEFLAGS = -pg

ARFLAGS      = -cvq

# core and test common includes
COMMON_INC          = include/definitions.h include/debug.h

# core stuff 
CORE_LIB            = lib/libcore.a
CORE_SRC            = $(shell echo src/*.c)
CORE_INC            = $(shell echo include/*.h)
CORE_OBJ            = $(CORE_SRC:.c=.o)
CORE_PUB_INC        =

# test stuff
TEST_SERVER         = test/server/server
TEST_SERVER_SRC     = $(shell echo test/server/*.c)
TEST_SERVER_INC     = $(shell echo test/server/*.h)
TEST_SERVER_OBJ     = $(TEST_SERVER_SRC:.c=.o)

TEST_CLIENT         = test/client/client
TEST_CLIENT_SRC     = $(shell echo test/client/*.c)
TEST_CLIENT_INC     = $(shell echo test/client/*.h)
TEST_CLIENT_OBJ     = $(TEST_CLIENT_SRC:.c=.o)

all: $(CORE_LIB) $(TEST_SERVER) $(TEST_CLIENT)

core: $(CORE_LIB)

server: core $(TEST_SERVER)

client: core $(TEST_CLIENT)

$(CORE_LIB): $(CORE_OBJ)
    $(AR) -cvq $@ $^

$(TEST_SERVER): $(TEST_SERVER_OBJ)
    $(CC) $(CFLAGS) -o $@ $< -Llib -lcore

$(TEST_CLIENT): $(TEST_CLIENT_OBJ)
    $(CC)  $(CFLAGS) -o $@ $< -Llib -lcore


debug: CFLAGS += $(DEBUGFLAGS)
debug: all

profile: CFLAGS += $(PROFILEFLAGS)
profile: all

release: CFLAGS += $(RELEASEFLAGS)
release: all

.PHONY : clean depend

clean:
    -rm -f $(CORE_LIB) $(CORE_OBJ)
    -rm -f $(TEST_SERVER) $(TEST_SERVER_OBJ)
    -rm -f $(TEST_CLIENT) $(TEST_CLIENT_OBJ)
    -rm -f gmon.out

depend:
    @makedepend -- $(CFLAGS) -- $(CORE_SRC) $(TEST_SERVER_SRC) $(TEST_CLIENT_SRC)

I still have some problems: - make creates the lib, the server and client test app. 我仍然有一些问题:-make创建lib,服务器和客户端测试应用程序。 If I know modify one of the source files of the core (lib), make builds the lib newly, but not the server and client apps. 如果我知道修改内核(lib)的源文件之一,请make新构建lib,而不是服务器和客户端应用。 Do I have a dependency problem? 我有依赖性问题吗? - make or make all should build by default as DEBUG, whereas release and profile build a release, or profile version respectively. -make或make all在默认情况下应构建为DEBUG,而release和profile分别构建发布或概要版本。

I wonder, what is wrong in my makefile. 我想知道,我的makefile中出了什么问题。

Thanks again! 再次感谢!

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

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