簡體   English   中英

創建將.o存儲到目錄的makefile c ++

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

這是一個如何存儲我的數據的示例:

\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

我想你明白了。

我的問題是,無論我如何嘗試我的makefile都不起作用

鐺:錯誤:沒有輸入文件

要么

架構x86_64的未定義符號:

好吧,我試圖自己學習如何構建一個makefile。 我喜歡最簡單的方法:

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

然后,對於每個.o,我要求makefile這樣做:

Objects/file1.0: Part1/file1.cpp
         $(CPP) $(CFLAGS) -o $(OBJ/file1).o Part1/file1.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

對於每個文件

我試圖學習如何更適當地構建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 $@ $<

但是,當然不行。

我的問題是:

如何創建一個可以在像我的環境(帶有不同的子文件夾)的環境中工作並將.o存儲在專用文件夾中的makefile。

我使用xcode工作,但不幸的是我想要一個菜單​​,您可以在其中使用無法在xcode控制台上使用的箭頭進行導航。

您的第二次嘗試非常接近。

問題是這條線

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

$(patsubst)的第一個參數是要匹配的模式,但是$(ALL_CPP)不是模式。 您想要那里的每個目錄為dir/%.c 可以做的模式,列表,如果你真的想,但是有一個更好的方式來做到這一點。

您這里確實有兩個轉換。 一種用Objects替換任何前導目錄,另一種用.o替換.cpp

所以分開做。

在第一部分中使用“ 替代參考 ”:

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

第二部分的notdiraddprefix 文件名功能

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

那應該使事情起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM