Makefile将.o文件导出到与.cpp不同的路径 [英] Makefile export .o file to a different path than .cpp

查看:404
本文介绍了Makefile将.o文件导出到与.cpp不同的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的任务很简单,我创建了makefile(makefile新建),并且我希望将.o文件保存在另一个文件夹中,以提供更整洁的目录,并允许其他人使用.o文件.

So my task is simple, I have created the makefile (New with makefiles) and I want to keep my .o files in a different folder to have a cleaner directory and allow the usage of .o files by others.

我搜索并找到了许多解决方案,这些解决方案指向使用-o $< $ @

I searched and found many solution pointing to using -o $< $@

但是,它给了我g++: cannot specify -o with -c or -S with multiple files

这就是我想要做的:

$(OBJECT_PATH)/file1.o: $(SOURCE_PATH)/file2.cpp $(SOURCE_PATH)/file1.cpp
        $(CC) $(CFLAGS) $(SOURCE_PATH)/file2.cpp $(SOURCE_PATH)/file1.cpp -o $@

file1.cpp具有#include"file1.h",所以从我的阅读中,我应该在依赖项中包含file1.cpp.但是,现在我无法导出到其他目录.

file1.cpp has #include "file1.h", so from what I read I should include file1.cpp in the dependencies. However, now I can't export to a different directory.

有解决方案吗?还是我的概念有误?

Is there a solution? Or do I have the concept wrong?

推荐答案

使用make -d甚至更好的 remake -x了解要调用的命令.

Use make -d or even better remake -x to understand what commands are invoked.

还运行make -p以了解使用了哪些内置规则.

Run also make -p to understand what builtin rules are used.

我们无法为您提供更多帮助,因为我们不知道您是否重新定义了CFLAGS.

We cannot help you more, because we have no idea if you redefined CFLAGS.

并且C ++编译最好使用CXXCXXFLAGSg++完成,例如与(我正在从我的make -p输出中提取出来)

And C++ compilation should better be done with g++ that is CXX and CXXFLAGS, e.g. with (I am extracting this from my make -p output)

LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
CXX = g++
%.o: %.cc
      $(COMPILE.cc) $(OUTPUT_OPTION) $<

我强烈建议至少在开发阶段使用CXXFLAGS= -Wall -g.还学习使用gdbvalgrind.

I strongly suggest to have CXXFLAGS= -Wall -g at least during the development phase. Learn also to use gdb and valgrind.

您可以在Makefile

 CXXFLAGS= -g -Wall
 SOURCES=f1.cc f2.cc
 SOURCE_PATH=yoursourcedir/
 OBJECT_PATH=yourobjectdir/
 SRCFILES=$(patsubst %.cc,$(SOURCE_PATH)/%.cc,$(SOURCES))
 OBJFILES=$(patsubst %.cc,$(OBJECT_PATH)/%.o,$(SOURCES))
 PROGFILE=$(OBJECT_PATH)
 .PHONY: all clean
 all: $(PROGFILE)
 $(PROGFILE): $(OBJFILES)
         $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@
 $(OBJECT_PATH)/%.o: $(SOURCE_PATH)/%.cc
         $(COMPILE.cc)  $(OUTPUT_OPTION) $<
 clean:
         $(RM) $(OBJECT_PATH)/*.o $(PROGFILE)

这篇关于Makefile将.o文件导出到与.cpp不同的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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