聪明的链接器依赖项

时间:2012-03-08 22:13:25

标签: boost linker makefile

我的项目是用make编译的,并且最近添加了用于单元测试的boost库。

目录结构如下:

Project/
  main.cpp
  main.hpp
  Makefile
  sources/
    classA.cpp
  includes/
    classA.hpp
  objects/
  test/
    Makefile
    unit_test_classA.cpp

在我运行的文件夹Project目录中:

$ make test

一切正常,但我不知道如何告诉你运行test/Makefile并自动查找../object目录中的依赖项。

我正在尝试保持makefile的通用性,因为我构建了许多对象,并希望将我的开发和测试分开。


问题是:我需要一个makefile规则来自动查找.o文件到特定的目录


Makefile

# Project/Makefile

BUILD = snake

LDFLAGS =
LDLIBS = -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system
CXXFLAGS = -Wall -I./includes
RM = rm -rf

OBJ_DIR = objects/
SRC_DIR = sources/

OBJS = $(patsubst $(SRC_DIR)%, \
    $(OBJ_DIR)%, \
    $(patsubst %.cpp,%.o,$(wildcard $(SRC_DIR)*.cpp)))

OBJECTS =  main.o $(OBJS)

all: $(BUILD)

test:
    cd test; $(MAKE)

$(BUILD): $(OBJECTS)
    $(CXX) $(LDFLAGS) $(LDLIBS) $(OBJECTS) -o $@

$(OBJ_DIR)%.o: $(SRC_DIR)%.cpp $(OBJ_DIR)
    $(CXX) $(CXXFLAGS) -c $< -o $@

clean:
    $(RM) $(OBJECTS) $(OBJ_DIR)
    mkdir $(OBJ_DIR)

.PHONY: test

# Project/test/Makefile

BUILD = boost_test

LDFLAGS =
LDLIBS = -lboost_unit_test_framework
CXXFLAGS = -Wall -I./../includes
RM = rm -rf

OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp))

OBJECTS = $(OBJS)

all: clean $(BUILD)
    ./$(BUILD)

$(BUILD): $(OBJECTS)
    $(CXX) $(LDFLAGS) $(LDLIBS) $(OBJECTS) -o $@

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

clean:
    $(RM) $(OBJECTS)

.PHONY: $(BUILD)

0 个答案:

没有答案