简体   繁体   English

创建将.o存储到目录的makefile c ++

[英]Creating makefile c++ that stores .o to a directory

Here is an example of how my datas are stored : 这是一个如何存储我的数据的示例:

\Program
     main.cpp
     makefile
     \Part1
          file1.cpp
          file1.h
          file2.cpp
          etc
     \Part2
          file3.cpp
          file4.cpp
          file4.h
          etc..
     \Part3
          file5.cpp
          file5.h
          etc..

     \Objects
          file1.o
          file2.o
          file3.o
          file4.o

I think you understood. 我想你明白了。

My problem is that whatever I try my makefile does not work 我的问题是,无论我如何尝试我的makefile都不起作用

clang: error: no input files 铛:错误:没有输入文件

or 要么

Undefined symbols for architecture x86_64: 架构x86_64的未定义符号:

Well, I tried to learn by myself how to build a makefile. 好吧,我试图自己学习如何构建一个makefile。 I leant the easiest way like : 我喜欢最简单的方法:

all:exe
exe: Objects/file1.o Objects/file2.o Objects/file3.o Objects/file4.o Objects/file5.o .....
     $(CPP) $(LFLAGS) Objects/file1.o Objects/file2.o Objects/file3.o ... -o exe

And then, for each .o I ask the makefile to do like : 然后,对于每个.o,我要求makefile这样做:

Objects/file1.0: Part1/file1.cpp
         $(CPP) $(CFLAGS) -o $(OBJ/file1).o Part1/file1.o

But I keep having this issue : 但是我一直有这个问题:

makefile:XX: warning: overriding commands for target .o' makefile:XX: warning: ignoring old commands for target .o' makefile:XX:警告:覆盖目标.o' makefile:XX: warning: ignoring old commands for target命令.o' makefile:XX: warning: ignoring old commands for target .o的.o' makefile:XX: warning: ignoring old commands for target

For every file 对于每个文件

I tried to learn how to build makefile more proprelly but it's quite difficult. 我试图学习如何更适当地构建makefile,但这非常困难。 I tried many, many things and it doesn't work. 我尝试了很多很多东西,但是没有用。

Here is what I mean when I say a proper way to write makefile 这是我说写Makefile的正确方法时的意思

ALL_CPP=Part1/*.cpp, Part2/*.cpp, Part3/*.cpp, Part4/*.cpp
CPP_FILES := $(wildcard $(ALL_CPP))
OBJ_FILES = $(patsubst $(ALL_CPP),Objects/%.o,$(CPP_FILES))

main: $(OBJ_FILES)
    g++ -o $@ $^

Objects/%.o: Animal/%.cpp
    g++ $(CFLAGS) -c -o $@ $<
Objects/%.o: Enclos/%.cpp
    g++ $(CFLAGS) -c -o $@ $<
Objects/%.o: Menu/%.cpp
    g++ $(CFLAGS) -c -o $@ $<
Objects/%.o: Zoo/%.cpp
    g++ $(CFLAGS) -c -o $@ $<

But of course it doesn't work. 但是,当然不行。

My question is : 我的问题是:

How to create a makefile which would work in an environment like mine (with different subfolder) and which would store the .o in a dedicated folder. 如何创建一个可以在像我的环境(带有不同的子文件夹)的环境中工作并将.o存储在专用文件夹中的makefile。

I worked using xcode but unfortunately I want a menu where you can navigate using the arrows which doesn't work on the xcode console. 我使用xcode工作,但不幸的是我想要一个菜单​​,您可以在其中使用无法在xcode控制台上使用的箭头进行导航。

Your second attempt was pretty close. 您的第二次尝试非常接近。

The problem is with this line 问题是这条线

OBJ_FILES = $(patsubst $(ALL_CPP),Objects/%.o,$(CPP_FILES))

The first argument to $(patsubst) is the pattern to match but $(ALL_CPP) isn't a pattern. $(patsubst)的第一个参数是要匹配的模式,但是$(ALL_CPP)不是模式。 You want dir/%.c for each directory there. 您想要那里的每个目录为dir/%.c You could make that list of patterns if you really wanted to but there's a better way to do it. 可以做的模式,列表,如果你真的想,但是有一个更好的方式来做到这一点。

You really have two transformations here. 您这里确实有两个转换。 One to replace any leading directory with Objects and one to replace .cpp with .o . 一种用Objects替换任何前导目录,另一种用.o替换.cpp

So do them separately. 所以分开做。

Use a Substitution Reference for the first part: 在第一部分中使用“ 替代参考 ”:

OBJ_FILES := $(CPP_FILES:.cpp=.o)

and the notdir and addprefix file name functions for the second part: 第二部分的notdiraddprefix 文件名功能

OBJ_FILES := $(addprefix Objects/,$(notdir $(OBJ_FILES))

and that should make things work. 那应该使事情起作用。

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

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