简体   繁体   中英

Problem compiling just 1 cpp file: "make: *** No rule to target 'build/main.o, needed by 'executable'. Stop."

I'm writing the most basic makefile I can think of, just compiling one main.cpp. I tried every suggestion I can find from reading similar questions but I still cannot pinpoint my issue. Common mistake is using spaces instead of tabs, I checked that too.

The whole project layout is as follows:

/bin
/build
/src
  |-----main.cpp
/Makefile

The main.cpp just prints "hello world".

Here's what the Makefile contains:

CXX = g++
WARNINGS = -Wall
OBJ = -c
OUTPUT = -o 
BIN = bin
BUILD = build
SOURCE = src

executable:${BUILD}/main.o
    ${CXX} ${WARNINGS} ${OUTPUT} ${BIN}/program ${BUILD}/main.o
    #c++ -wall -o bin/program src/main.o

main.o: ${SOURCE}/main.cpp
    ${CXX} ${WARNINGS} ${OBJ} ${OUTPUT} ${BUILD}/main.o ${SOURCE}/main.cpp

As you can see, there are instructions to build main.o! If I don't use the makefile variables and plainly write the commands (the commented out line), it works.

There is no missing file. Here's proof that main.cpp exists在此处输入图片说明

Your Fix: change main.o to ${BUILD}/main.o then make command can relate it

CXX = g++
WARNINGS = -Wall
OBJ = -c
OUTPUT = -o 
BIN = bin
BUILD = build
SOURCE = src

executable:${BUILD}/main.o
    ${CXX} ${WARNINGS} ${OUTPUT} ${BIN}/program ${BUILD}/main.o

${BUILD}/main.o: ${SOURCE}/main.cpp          #<------ watch this line
    ${CXX} ${WARNINGS} ${OBJ} ${OUTPUT} ${BUILD}/main.o ${SOURCE}/main.cpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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