【问题标题】:C++ makefile building multiple dll and project structuringC ++ makefile构建多个dll和项目结构
【发布时间】:2015-07-11 00:29:08
【问题描述】:

我已尝试寻找答案但无济于事,因此鉴于我的项目具有以下结构

makefile
./src
   strings.cpp
   networking.cpp
./bin
   strings.dll
   networking.dll
./build
   strings.o
   networking.o
./include
   strings.h
   networking.h
./lib
   boost

我对 Makefiles 很陌生,从我目前所做的研究来看,我已经设法把它放在一起(不是很复杂,我知道)

CC = g++ 
SRC = src/strings.cpp
OUT = bin/strings.dll
OBJ = build/strings.o 
INC= -I include

all: strings.dll

strings.dll: strings.o
    $(CC) -shared -o $(OUT) $(OBJ) 

strings.o: $(SRC) 
    $(CC) $(INC) -DBDLL -c $(SRC) -o $(OBJ)

我遇到的问题/疑问是

1- 它总是经过整个编译过程,即使我没有更改源代码?

2- 我怎样才能让事情更“有效”?我看到了人们使用通配符等的例子,但我很难理解。我可以使用通配符开始,因为我想为每个目标使用单独的 dll 吗?

3 - 假设我介绍了 algorithms.h and algorithms.cpp 在构建中包含它的推荐方式是什么?

感谢您的帮助,非常感谢

【问题讨论】:

    标签: c++ gcc dll makefile


    【解决方案1】:

    首先。整个编译过程进行,因为搜索目标“strings.dll”但构建 bin/strings.dll。所以如果你把它换成

    bin/strings.dll: strings.o
         $(CC) -shared -o $(OUT) $(OBJ)
    
    bin/strings.o: $(SRC) 
         $(CC) $(INC) -DBDLL -c $(SRC) -o $(OBJ)
    

    只有在先决条件发生变化时才会执行目标(bin/strings.o 和 bin/strings.dll)的构建。

    第二 - 基本上通配符用于搜索目录内的所有文件,如下所示:$(whildcard *.cpp) 计算为当前目录内的所有 cpp 文件。所以你可以这样写:

    all_sources = $(wildcard *.cpp)
    all_objects = $(addprefix bin/,$(all_sources:.cpp=.o))
    
    all: bin/strings.dll
    
    bin/strings.dll: $(all_objects)
        <how to build strings.dll from objects>
    
    bin/%.o: %.cpp
        <how to build objects inside bin dir from cpp of current dir>    
    

    第三 - makefile 本身不是构建系统,它只是一个具有特定领域语言的工具。您可以使用 make 编写自己的构建系统。如果你想要准备好构建,你最好学习 automake/cmake/... 其中很多。

    开始使用 make 工具也是一个很好的开始。不要停下来,你会惊讶于它里面的力量。

    【讨论】:

    • 感谢您的详细回答...确实比我要求的要多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多