Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile - .o: file not recognized: File truncated?

I'm just learning GNU make, but I'm having trouble linking when using .d (dependency) files. Can anyone point me in the right direction with this error:

...../part1.o: file not recognized: File truncated recipe for target 'bin/target/prog' failed

It's a simple program containing: main.cpp, part1.cpp, part1.h, part2.cpp, part2.h

Where part1 and part2 have a method to print something.

This is from terminal when running make:

I don't get why I'm getting a warning for using #pragma once?

stud@GoldenImageASE:~/Desktop/ISU/L1/2$ make ARCH=target -f Makefile.th
Compiling...part2.cpp
arm-devkit-g++ -MTbuild/target/part2.o -MM -I. part2.cpp > build/target/part2.d
Compiling...part1.cpp
arm-devkit-g++ -MTbuild/target/part1.o -MM -I. part1.cpp > build/target/part1.d
Compiling...main.cpp
arm-devkit-g++ -MTbuild/target/main.o -MM -I. main.cpp > build/target/main.d
object file....main.o
arm-devkit-g++ -I. -c main.cpp part1.h part2.h > build/target/main.o
part1.h:1:9: warning: #pragma once in main file
 #pragma once
         ^
part2.h:1:9: warning: #pragma once in main file
 #pragma once
         ^
object file....part1.o
arm-devkit-g++ -I. -c part1.cpp > build/target/part1.o
object file....part2.o
arm-devkit-g++ -I. -c part2.cpp > build/target/part2.o
arm-devkit-g++ -I. -o build/target/main.o build/target/part1.o build/target/part2.o -o prog
build/target/part1.o: file not recognized: File truncated
collect2: error: ld returned 1 exit status
Makefile.th:27: recipe for target 'bin/target/prog' failed
make: *** [bin/target/prog] Error 1

My Makefile is found below:

# Variables
SOURCES=main.cpp part1.cpp part2.cpp
OBJECTS=$(SOURCES:.cpp=.o)
DEPS=$(SOURCES:.cpp=.d)
EXE=prog
CXXFLAGS =-I.


# Making for host
# > make ARCH=host
ifeq (${ARCH},host)
CXX=g++
BUILD_DIR=build/host
EXE_DIR=bin/host
endif

# Making for target
# > make ARCH= target
ifeq (${ARCH},target)
CXX=arm-devkit-g++
BUILD_DIR=build/target
EXE_DIR=bin/target
endif

$(addprefix ${EXE_DIR}/,$(EXE)): $(addprefix ${BUILD_DIR}/,$(DEPS)) $(addprefix ${BUILD_DIR}/,$(OBJECTS))
# << Check the $(DEPS) new dependency
    @mkdir -p $(dir $@)
    $(CXX) $(CXXFLAGS) -o $(addprefix ${BUILD_DIR}/,$(OBJECTS))

$(addprefix $(BUILD_DIR)/, %.o): %.cpp
    @echo "object file...."$*.o
    $(CXX) $(CXXFLAGS) -c $^ > $@


# Rule that describes how a .d ( dependency ) file is created from a .cpp
# Similar to the assigment %. cpp -> %.o
${BUILD_DIR}/%.d: %.cpp
    @mkdir -p $(dir $@)
    @echo "Compiling..."$<
    $(CXX) -MT$(@:.d=.o) -MM $(CXXFLAGS) $^ > $@

debug:
    @echo "DEPS: "$(DEPS)"\n"
    @echo "OBJ: " $(addprefix ${BUILD_DIR}/,$(OBJECTS))"\n"
    @echo "EXE: " $(addprefix ${EXE_DIR}/,$(EXE))"\n"


.PHONY:clean
clean: 
    rm -f $(EXE) $(addprefix ${BUILD_DIR}/,$(DEPS)) $(addprefix ${BUILD_DIR}/,$(OBJECTS))

ifneq ($(MAKECMDGOALS),clean)
-include $(addprefix ${BUILD_DIR}/,$(DEPS))
endif
like image 343
Mat0 Avatar asked Oct 19 '25 14:10

Mat0


1 Answers

You have two unrelated problems. The first one is that you have two conflicting -o options when linking.

The actual problem you ask about is something different, but still related to the -o option: Namely that you don't have one when attempting to create the object files.

When creating the object files, the generated object file is not written to standard output, therefore your redirection will not cause the gcc frontend program to create an object file with the name you think.

For example:

arm-devkit-g++ -I. -c part1.cpp > build/target/part1.o

The above command will create an object file named part1.o in the current directory, and write the (empty) standard output to the file build/target/part1.o. That will leave build/target/part1.o empty, which is what the linker is complaining about (that's what it means when it says the file is truncated).

The command should instead look like

arm-devkit-g++ -I. -c part1.cpp -o build/target/part1.o

Note the use of the -o option to name the output file.

You need to modify the makefile to not use redirection when building the object files.


Also, you should not list the header files when building object files, only the source file you want to build, so the command

arm-devkit-g++ -I. -c main.cpp part1.h part2.h > build/target/main.o

should really be

arm-devkit-g++ -I. -c main.cpp -o build/target/main.o
like image 65
Some programmer dude Avatar answered Oct 22 '25 03:10

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!