【问题标题】:MakeFile destination directoryMakeFile 目标目录
【发布时间】:2014-04-11 05:26:20
【问题描述】:

我的代码在 src 文件夹中。 src/Client 包含一个用于创建客户端应用程序的生成文件。 src/Server 包含一个用于创建客户端应用程序的生成文件。 我在与 src 相同的文件夹中有一个 bin 文件。 名为 ad 的文件夹包含 src 和 bin。 在 bin 我有一个 makefile

all: 
    cd ../src/Server; make
    cd ../src/Client; make

clean: 
    cd ../src/Server; make clean
    cd ../src/Client; make clean

我的问题是我希望所有可执行文件都在 bin 中,但现在它们是在服务器和客户端文件夹中创建的。

我在客户端文件夹中的生成文件:

# Define the compiler and the linker. The linker must be defined since
# the implicit rule for linking uses CC as the linker. g++ can be
# changed to clang++.
CXX = g++
CC  = g++

# Define preprocessor, compiler, and linker flags. Uncomment the # lines
# if you use clang++ and wish to use libc++ instead of libstd++.
CPPFLAGS  =-I.. -I ../database

CXXFLAGS =  -g -O2 -Wall -W -pedantic-errors
CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast 
CXXFLAGS += -std=c++11 
LDFLAGS =   -g -L.. -L../database
#CPPFLAGS += -stdlib=libc++
#CXXFLAGS += -stdlib=libc++
#LDFLAGS +=  -stdlib=libc++

# Libraries
LDLIBS = -lclientserver -ldatabase

# Targets
PROGS = interface


all: $(PROGS)

# Targets rely on implicit rules for compiling and linking
# The dependency on libclientserver.a is not defined.
interface: interface.o com.o ans.o

# Phony targets
.PHONY: all clean

# Standard clean
clean:
    rm -f *.o $(PROGS)

# Generate dependencies in *.d files
%.d: %.cc
    @set -e; rm -f $@; \
         $(CPP) -MM $(CPPFLAGS) $< > $@.$$$$; \
         sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
         rm -f $@.$$$$

# Include the *.d files
SRC = $(wildcard *.cc)
include $(SRC:.cc=.d)

我应该在这个文件中更改什么?

【问题讨论】:

    标签: c++ makefile


    【解决方案1】:

    通常,您的主 makefile 是构建可执行文件的内容。辅助的应该制作目标文件。在这种情况下,将可执行文件移动到根文件夹是可行的:

    mv src/Client/$(EXECUTABLE) src/bin
    mv src/Server/$(EXECUTABLE) src/bin
    

    如果你真的想把它做对,从主 make 链接目标文件

    all:
          $(CC) $(LDFLAGS) $(CLIENT_FOLDER)/*.o $(SERVER_FOLDER)/*.o $(BIN_FOLDER)/$(EXECUTABLE)
    

    让 aux 文件直接编译

    【讨论】:

    • 我确实喜欢这个界面:interface.o com.o ans.o $(CXX) -o ../../bin/$@ $^ $(LDLIBS) $(LDFLAGS) 同样在另一个。
    【解决方案2】:
    # Define the compiler and the linker. The linker must be defined since
    # the implicit rule for linking uses CC as the linker. g++ can be
    # changed to clang++.
    CXX = g++
    CC  = g++
    
    # Define preprocessor, compiler, and linker flags. Uncomment the # lines
    # if you use clang++ and wish to use libc++ instead of libstd++.
    CPPFLAGS  =-I.. -I ../database
    
    CXXFLAGS =  -g -O2 -Wall -W -pedantic-errors
    CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast 
    CXXFLAGS += -std=c++11 
    LDFLAGS =   -g -L.. -L../database
    #CPPFLAGS += -stdlib=libc++
    #CXXFLAGS += -stdlib=libc++
    #LDFLAGS +=  -stdlib=libc++
    
    # Libraries
    LDLIBS = -lclientserver -ldatabase
    
    # Targets
    PROGS = interface
    
    
    all: $(PROGS)
    
    # Targets rely on implicit rules for compiling and linking
    # The dependency on libclientserver.a is not defined.
    interface: interface.o com.o ans.o
         $(CXX) -o ../../bin/$@ $^ $(LDLIBS) $(LDFLAGS)
    # Phony targets
    .PHONY: all clean
    
    # Standard clean
    clean:
        rm -f *.o $(PROGS)
    
    # Generate dependencies in *.d files
    %.d: %.cc
        @set -e; rm -f $@; \
             $(CPP) -MM $(CPPFLAGS) $< > $@.$$$$; \
             sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
             rm -f $@.$$$$
    
    # Include the *.d files
    SRC = $(wildcard *.cc)
    include $(SRC:.cc=.d)
    

    这行得通

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多