Makfile генерирует оба `(.text 0x0): множественное определение` и `(.text 0x44): неопределенная ссылка на` ошибки при связывании .o объектов

#c #makefile

#c #makefile

Вопрос:

У меня есть следующий makefile, с помощью которого мне не удается связать объекты с конечным исполняемым файлом, хотя он отлично создает отдельные объектные файлы. Он выводит оба (.text 0x0): multiple definition и (.text 0x44): undefined reference to ошибки при построении основной цели.

 # Set compiler
CC = gcc

# Include headers 
INC_HDR := -I. -IAPI/core -IAPI/platform

# Define final executable 
EXE := driver 

# List source
SRC := $(wildcard API/core/*.c) 
        $(wildcard API/platform/*.c) 
        main.c 

# List headers
HDR := $(wildcard API/core/*.h) 
        $(wildcard API/platform/*.h) 

# Derive objects
OBJ := $(patsubst %.c, %.o, $(SRC))

# Set flags 
CPPFLAGS := $(INC_HDR) -MMD -MP # -I is a preprocessor flag, not a compiler flag -MMD -MP
CFLAGS   := -Wall           # some warnings about bad code
LDFLAGS  := -L.             # -L is a linker flag
LDLIBS   := -lm             # Left empty if no libs are needed

.PHONY: all clean 

all: $(EXE)

$(EXE): $(OBJ) 
    @echo Linking $@
    $(CC) $(CPPFLAGS) $(CFLAGS) $(LDLIBS) $^ -o $@ 

$(OBJ): $(SRC) $(HDR)
    @echo Building $@ 
    $(CC) $(CPPFLAGS) $(CFLAGS) -c

lt; -o $@

clean:
@find . -type f -name '*.o' -delete
@find . -type f -name '*.d' -delete

Этот вопрос довольно близок к другим сообщениям, но я пока не смог найти, в чем проблема.

Как и было запрошено, вывод make clean all :

 Building API/core/VL53L1X_calibration.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall                -c API/core/VL53L1X_calibration.c -o API/core/VL53L1X_calibration.o
Building API/core/VL53L1X_api.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall                -c API/core/VL53L1X_calibration.c -o API/core/VL53L1X_api.o
Building API/platform/vl53l1_platform.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall                -c API/core/VL53L1X_calibration.c -o API/platform/vl53l1_platform.o
Building main.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall                -c API/core/VL53L1X_calibration.c -o main.o
Linking driver
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall                -lm              API/core/VL53L1X_calibration.o API/core/VL53L1X_api.o API/platform/vl53l1_platform.o main.o -o driver 
API/core/VL53L1X_api.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x0): multiple definition of `VL53L1X_CalibrateOffset'
API/core/VL53L1X_calibration.o:VL53L1X_calibration.c:(.text 0x0): first defined here
API/core/VL53L1X_api.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x170): multiple definition of `VL53L1X_CalibrateXtalk'
API/core/VL53L1X_calibration.o:VL53L1X_calibration.c:(.text 0x170): first defined here
API/platform/vl53l1_platform.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x0): multiple definition of `VL53L1X_CalibrateOffset'
API/core/VL53L1X_calibration.o:VL53L1X_calibration.c:(.text 0x0): first defined here
API/platform/vl53l1_platform.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x170): multiple definition of `VL53L1X_CalibrateXtalk'
API/core/VL53L1X_calibration.o:VL53L1X_calibration.c:(.text 0x170): first defined here
main.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x0): multiple definition of `VL53L1X_CalibrateOffset'
API/core/VL53L1X_calibration.o:VL53L1X_calibration.c:(.text 0x0): first defined here
main.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x170): multiple definition of `VL53L1X_CalibrateXtalk'
API/core/VL53L1X_calibration.o:VL53L1X_calibration.c:(.text 0x170): first defined here
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text 0x20): undefined reference to `main'
API/core/VL53L1X_calibration.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x44): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x5c): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x74): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x82): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0xa3): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0xc0): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0xce): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0xfa): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x14f): undefined reference to `VL53L1_WrWord'
API/core/VL53L1X_calibration.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x1cf): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x1dd): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0x201): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0x21e): undefined reference to `VL53L1X_GetSignalRate'
VL53L1X_calibration.c:(.text 0x233): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0x241): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0x26f): undefined reference to `VL53L1X_GetSpadNb'
VL53L1X_calibration.c:(.text 0x2c3): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x376): undefined reference to `VL53L1_WrWord'
API/core/VL53L1X_api.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x44): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x5c): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x74): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x82): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0xa3): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0xc0): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0xce): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0xfa): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x14f): undefined reference to `VL53L1_WrWord'
API/core/VL53L1X_api.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x1cf): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x1dd): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0x201): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0x21e): undefined reference to `VL53L1X_GetSignalRate'
VL53L1X_calibration.c:(.text 0x233): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0x241): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0x26f): undefined reference to `VL53L1X_GetSpadNb'
VL53L1X_calibration.c:(.text 0x2c3): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x376): undefined reference to `VL53L1_WrWord'
API/platform/vl53l1_platform.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x44): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x5c): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x74): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x82): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0xa3): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0xc0): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0xce): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0xfa): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x14f): undefined reference to `VL53L1_WrWord'
API/platform/vl53l1_platform.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x1cf): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x1dd): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0x201): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0x21e): undefined reference to `VL53L1X_GetSignalRate'
VL53L1X_calibration.c:(.text 0x233): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0x241): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0x26f): undefined reference to `VL53L1X_GetSpadNb'
VL53L1X_calibration.c:(.text 0x2c3): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x376): undefined reference to `VL53L1_WrWord'
main.o: In function `VL53L1X_CalibrateOffset':
VL53L1X_calibration.c:(.text 0x44): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x5c): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x74): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x82): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0xa3): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0xc0): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0xce): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0xfa): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x14f): undefined reference to `VL53L1_WrWord'
main.o: In function `VL53L1X_CalibrateXtalk':
VL53L1X_calibration.c:(.text 0x1cf): undefined reference to `VL53L1_WrWord'
VL53L1X_calibration.c:(.text 0x1dd): undefined reference to `VL53L1X_StartRanging'
VL53L1X_calibration.c:(.text 0x201): undefined reference to `VL53L1X_CheckForDataReady'
VL53L1X_calibration.c:(.text 0x21e): undefined reference to `VL53L1X_GetSignalRate'
VL53L1X_calibration.c:(.text 0x233): undefined reference to `VL53L1X_GetDistance'
VL53L1X_calibration.c:(.text 0x241): undefined reference to `VL53L1X_ClearInterrupt'
VL53L1X_calibration.c:(.text 0x26f): undefined reference to `VL53L1X_GetSpadNb'
VL53L1X_calibration.c:(.text 0x2c3): undefined reference to `VL53L1X_StopRanging'
VL53L1X_calibration.c:(.text 0x376): undefined reference to `VL53L1_WrWord'
collect2: error: ld returned 1 exit status
makefile:33: recipe for target 'driver' failed
make: *** [driver] Error 1
 

Комментарии:

1. Они выглядят как ошибки из ld , а не make .

2. Пожалуйста, покажите полный вывод make clean all .

Ответ №1:

Если вы посмотрите на эти строки:

 Building API/core/VL53L1X_calibration.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall -c API/core/VL53L1X_calibration.c -o API/core/VL53L1X_calibration.o
Building API/core/VL53L1X_api.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall -c API/core/VL53L1X_calibration.c -o API/core/VL53L1X_api.o
Building API/platform/vl53l1_platform.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall -c API/core/VL53L1X_calibration.c -o API/platform/vl53l1_platform.o
Building main.o
gcc -I. -IAPI/core -IAPI/platform -MMD -MP  -Wall -c API/core/VL53L1X_calibration.c -o main.o
 

Вы увидите, что для создания каждого объектного файла используется один и тот же исходный файл. Таким образом, все, что определено в этой функции, будет определено несколько раз, а все, что не определено, будет неопределенным.

Это связано с тем, как вы определили цель для объектных файлов:

 $(OBJ): $(SRC) $(HDR)
 

Это должно быть:

 %.o: %.c $(HDR)
 

Комментарии:

1. Большое спасибо @dbush, я не обратил достаточного внимания на сообщение об ошибке!