简体   繁体   English

如何正确地使我的makefile编译和运行?

[英]How to properly make my makefile to compile and run?

The question is probably not the best one to describe my issue but I couldn't think of a better one. 问题可能不是描述我的问题的最佳问题,但我想不出更好的问题。 My makefile goes like this: 我的makefile是这样的:

PROGRAM_NAME = prog

OBJECT_FILES = $(PROGRAM_NAME).o
CFLAGS = -O2 -Wall -g

$(PROGRAM_NAME) : $(OBJECT_FILES)
    gcc $(CFLAGS) -o $@ $(OBJECT_FILES)

$(PROGRAM_NAME).o : $(PROGRAM_NAME).c data.h
    gcc $(CFLAGS) -c $<

clean :
    $(RM) $(PROGRAM_NAME)
    $(RM) $(OBJECT_FILES)
    $(RM) *~ *.bak

run :
    @$(MAKE) && ./$(PROGRAM_NAME) $(ARGS)

When I want to compile and run I just do "make run". 当我想编译并运行时,我只是做“运行”。 The issue with this is that my program handles the signal produced by Ctrl+Z and if I start my program with "make run", the signal will be sent to "make run" and not my program itself. 这个问题是我的程序处理Ctrl + Z产生的信号,如果我用“make run”启动我的程序,信号将被发送到“make run”而不是我的程序本身。

Basically, calling "make run" is not the same thing as calling directly "make && ./prog" because in the first case, "make run" will not terminate unless "prog" terminates first. 基本上,调用“make run”与直接调用“make && ./prog”不同,因为在第一种情况下,“make run”不会终止,除非“prog”首先终止。

Is there a way around this? 有没有解决的办法?

You can simplify your 'run' target by having it depend on whether your program is up to date, and then simply run the program: 您可以通过依赖于程序是否是最新的来简化“运行”目标,然后只需运行程序:

run:    ${PROGRAM_NAME}
        ./${PROGRAM} ${ARGS}

There's not much point in running make when you're already running make - at least, not in this context. 这里没有太多运行点make ,当你已经运行make -至少,在此背景下。 Maybe for recursive operations (in different directories), but see ' Recursive Make Considered Harmful '. 也许对于递归操作(在不同的目录中),但请参阅' Recursive Make Considered Harmful '。

Also, your makefile should normally provide a target ' all ' and it should normally the first and therefore default target. 此外,你的makefile通常应该提供一个目标' all ',它通常应该是第一个默认目标。

Running from the makefile is a bit unusual. 从makefile运行有点不寻常。 Are you, perhaps, trying to duplicate the "Compile and Run" Menu item that some IDE provide? 您是否可能尝试复制某些IDE提供的“编译和运行”菜单项? Make is not well equipped to do that. Make没有那么好的装备。

All the stuff that happens in the target commands happens in sub-processes that are not attached directly to the terminal, which is why make receives your key stroke. 目标命令中发生的所有事情都发生在没有直接连接到终端的子进程中,这就是make接收键击的原因。

Another thing to look at: usually the object-file to executable stage (linking) uses a different set of flags ( LDFLAGS and LIBS ) then the compile stage. 另一件需要注意的事情是:通常对象文件到可执行阶段(链接)使用不同的标志集( LDFLAGSLIBS )然后是编译阶段。 In this simple example you can get away with it, but if you copy this makefile for use in a more complicated case you'll run into trouble. 在这个简单的例子中,你可以逃脱它,但如果你复制这个makefile以便在更复杂的情况下使用,你将遇到麻烦。

If you're going to build and run over and over, You can use the history command to help with this: 如果您要构建并反复运行,可以使用history命令来帮助解决此问题:

# Run this once
make && ./foo

# Repeat last command
!!

As dmckee's answer said, make(1) is making something, not for compile-and-run. 正如dmckee的回答所说,make(1)正在创造一些东西,而不是为了编译和运行。

Of course, nothing stops you for creating a shell alias make-run which does the intended ' make && ./prog args '. 当然,没有什么能阻止你创建一个shell别名make-run来完成预期的' make && ./prog args '。

You can try like this: 你可以尝试这样:

APP      = olupxtest

SRCS     = $(wildcard *.cpp)
OBJS     = $(SRCS:.cpp=.o)

CXXFLAGS = -g -fPIC -c
LDFLAGS  =
LIBS     =

.PHONY: all clean

all: clean $(APP) run

$(APP): $(OBJS)
        $(CXX) $(LDFLAGS) $^ $(LIBS) -o $@

clean:
        $(RM) $(OBJS) $(APP)

run:    ${APP}
        ./${APP} ${ARGS}

Here you have calling multiple rules for target: all: clean $(APP) run 在这里你可以为目标调用多个规则: all: clean $(APP) run

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

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