undefined reference to 'function' in Xilinx Eclipse SDK - c

I am confronting
undefined-reference to 'function'
in my C code using Xilinx Eclipse SDK. However, the functions is already built !
In fact, I found a [solved] similar issue, but I couldn't reflect the solution to my case.
this my console output:
13:40:08 **** Incremental Build of configuration Debug for project FH_TX_2 ****
make all
'Building target: FH_TX_2.elf'
'Invoking: MicroBlaze gcc linker'
mb-gcc -L"D:\Users\dell\Vivado_projects\FHSS_Transmitter_2\FHSS_Transmitter_2.sdk\FH_TX_2_bsp\microblaze_0\libsrc" -Wl,-T -Wl,../src/lscript.ld -L"D:\Users\dell\Vivado_projects\FHSS_Transmitter_2\FHSS_Transmitter_2.sdk\FH_TX_2_bsp\microblaze_0\lib" -mlittle-endian -mcpu=v10.0 -mxl-soft-mul -Wl,--no-relax -Wl,--gc-sections -o "FH_TX_2.elf" ./src/testperiph.o ./src/xiomodule_selftest_example.o -Wl,--start-group,-lxil,-lgcc,-lc,--end-group
./src/testperiph.o: In function `main':
D:\Users\dell\Vivado_projects\FHSS_Transmitter_2\FHSS_Transmitter_2.sdk\FH_TX_2\Debug/../src/testperiph.c:71: undefined reference to `DA3_WriteSPI'
collect2.exe: error: ld returned 1 exit status
make: *** [FH_TX_2.elf] Error 1
13:40:08 Build Finished (took 726ms)
makefile:
COMPILER=
ARCHIVER=
CP=cp
COMPILER_FLAGS=
EXTRA_COMPILER_FLAGS=
LIB=libxil.a
RELEASEDIR=../../../lib
INCLUDEDIR=../../../include
INCLUDES=-I./. -I${INCLUDEDIR}
INCLUDEFILES=*.h
LIBSOURCES=*.c
OUTS = *.o
libs:
echo "Compiling PmodDA3..."
$(COMPILER) $(COMPILER_FLAGS) $(EXTRA_COMPILER_FLAGS) $(INCLUDES) $(LIBSOURCES)
$(ARCHIVER) -r ${RELEASEDIR}/${LIB} ${OUTS}
make clean
include:
${CP} $(INCLUDEFILES) $(INCLUDEDIR)
clean:
rm -rf ${OUTS}
# Makefile generated by Xilinx.
PROCESSOR = microblaze_0
LIBRARIES = ${PROCESSOR}/lib/libxil.a
BSP_MAKEFILES := $(wildcard $(PROCESSOR)/libsrc/*/src/Makefile)
SUBDIRS := $(patsubst %/Makefile, %, $(BSP_MAKEFILES))
ifneq (,$(findstring win,$(RDI_PLATFORM)))
SHELL = CMD
endif
all: libs
#echo 'Finished building libraries'
include: $(addsuffix /make.include,$(SUBDIRS))
libs: $(addsuffix /make.libs,$(SUBDIRS))
clean: $(addsuffix /make.clean,$(SUBDIRS))
$(PROCESSOR)/lib/libxil.a: $(PROCESSOR)/lib/libxil_init.a
cp -f $< $#
%/make.include: $(if $(wildcard $(PROCESSOR)/lib/libxil_init.a),$(PROCESSOR)/lib/libxil.a,)
#echo "Running Make include in $(subst /make.include,,$#)"
$(MAKE) -C $(subst /make.include,,$#) -s include "SHELL=$(SHELL)" "COMPILER=mb-gcc" "ARCHIVER=mb-ar" "COMPILER_FLAGS= -O2 -c -mcpu=v10.0 -mlittle-endian -mxl-soft-mul" "EXTRA_COMPILER_FLAGS=-ffunction-sections -fdata-sections -Wall -Wextra"
%/make.libs: include
#echo "Running Make libs in $(subst /make.libs,,$#)"
$(MAKE) -C $(subst /make.libs,,$#) -s libs "SHELL=$(SHELL)" "COMPILER=mb-gcc" "ARCHIVER=mb-ar" "COMPILER_FLAGS= -O2 -c -mcpu=v10.0 -mlittle-endian -mxl-soft-mul" "EXTRA_COMPILER_FLAGS=-ffunction-sections -fdata-sections -Wall -Wextra"
%/make.clean:
$(MAKE) -C $(subst /make.clean,,$#) -s clean
clean:
rm -f ${PROCESSOR}/lib/libxil.a
PmodDA3.h Libraray path
libxil.a
work window
PmodDA3.c
.o files are generated

Related

avr-gcc undefined reference to function in linked object file

I have some code for handling all uart related functions and operations in a .h/.c pair that I usually include in my project. I have a makefile that compiles all of my .c files into .o files then links them all to one executable. For some reason, one of my projects using this pair compiles fine but the other gives this error:
obj/fiveax.o: In function `fiveax_init':
fiveax.c:(.text+0x10): undefined reference to `uart_init'
collect2: error: ld returned 1 exit status
when trying to link to the exectutable after compiling all the .o files. I have included my makefile below, it is the same for both projects with the only difference being the appname.
Makefile
########################################################################
####################### Makefile Template ##############################
########################################################################
# Compiler settings - Can be customized.
CC = avr-gcc
CXXFLAGS = -mmcu=atmega2560 -D BUILD=1
LDFLAGS = -Iinclude -Ilibs/FreeRTOS/include -Llibs/FreeRTOS -lfreertos
DEV = /dev/ttyUSB0
AVRCONF = -C/etc/avrdude.conf
AVRLOAD = $(AVRCONF) -v -patmega2560 -carduino -P$(DEV) -b57600 -D -Uflash:w:bin/$(APPNAME).hex:i
# Makefile settings - Can be customized.
APPNAME = FiveAx
EXT = .c
SRCDIR = src
OBJDIR = obj
INCDIR = include
DEPDIR = dep
############## Do not change anything from here downwards! #############
SRC = $(wildcard $(SRCDIR)/*$(EXT))
OBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)/%.o)
DEP = $(OBJ:$(OBJDIR)/%.o=$(DEPDIR)/%.d)
# UNIX-based OS variables & settings
RM = rm
DELOBJ = $(OBJ)
# Windows OS variables & settings
DEL = del
EXE = .exe
WDELOBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)\\%.o)
########################################################################
####################### Targets beginning here #########################
########################################################################
all: bin $(APPNAME)
# Builds the app
$(APPNAME): $(OBJ)
$(CC) $(CXXFLAGS) -o bin/$#.elf $^ $(LDFLAGS)
avr-objcopy -j .text -j .data -O ihex bin/$(APPNAME).elf bin/$(APPNAME).hex
bin:
mkdir -p $#
# Creates the dependecy rules
$(DEPDIR)/%.d: $(SRCDIR)/%$(EXT)
#$(CPP) $(CFLAGS) $< -MM -MT $(#:$(DEPDIR)/%.d=$(OBJDIR)/%.o) >$# $(LDFLAGS)
# Includes all .h files
-include $(DEP)
# Building rule for .o files and its .c/.cpp in combination with all .h
$(OBJDIR)/%.o: $(SRCDIR)/%$(EXT)
$(CC) $(CXXFLAGS) -o $# -c $< $(LDFLAGS)
load: bin $(APPNAME)
avrdude $(AVRLOAD)
################### Cleaning rules for Unix-based OS ###################
# Cleans complete project
.PHONY: clean
clean:
$(RM) -r $(DELOBJ) $(DEP) bin
# Cleans only all files with the extension .d
.PHONY: cleandep
cleandep:
$(RM) $(DEP)
edit: I have included the header file for uart.h in the source file.
edit: I was asked for the output of the makefile
avr-gcc -mmcu=atmega2560 -D BUILD=1 -Iinclude -Ilibs/FreeRTOS/include -o obj/uart.o -c src/uart.c -Llibs/FreeRTOS -lfreertos
avr-gcc -mmcu=atmega2560 -D BUILD=1 -Iinclude -Ilibs/FreeRTOS/include -o obj/servo.o -c src/servo.c -Llibs/FreeRTOS -lfreertos
avr-gcc -mmcu=atmega2560 -D BUILD=1 -Iinclude -Ilibs/FreeRTOS/include -o obj/main.o -c src/main.c -Llibs/FreeRTOS -lfreertos
avr-gcc -mmcu=atmega2560 -D BUILD=1 -Iinclude -Ilibs/FreeRTOS/include -o obj/fiveax.o -c src/fiveax.c -Llibs/FreeRTOS -lfreertos
avr-gcc -mmcu=atmega2560 -D BUILD=1 -Iinclude -Ilibs/FreeRTOS/include -o bin/FiveAx.elf obj/uart.o obj/servo.o obj/main.o obj/fiveax.o
obj/fiveax.o: In function `fiveax_init':
fiveax.c:(.text+0x10): undefined reference to `uart_init'
collect2: error: ld returned 1 exit status
make: *** [Makefile:41: FiveAx] Error 1
It's impossible to read the information requested because it's not formatted as a code block so all the lines run together. Please use the live preview feature of the SO question editor to check that the content you're adding is readable, to save the sanity of the people trying to help you.
The link line appears to be this:
avr-gcc -mmcu=atmega2560 -D BUILD=1 -Iinclude -Ilibs/FreeRTOS/include -o bin/FiveAx.elf obj/uart.o obj/servo.o obj/main.o obj/fiveax.o
Here we can see that the library -lfreertos is not present, and also that all the object files come at the end after the flags. I don't see how this could have happened given the makefile you show above:
CXXFLAGS = -mmcu=atmega2560 -D BUILD=1
LDFLAGS = -Iinclude -Ilibs/FreeRTOS/include -Llibs/FreeRTOS -lfreertos
$(CC) $(CXXFLAGS) -o bin/$#.elf $^ $(LDFLAGS)
so it seems that the make output here doesn't match the makefile you've shown us. Are you sure you're using that makefile?

Makefile for custom raspberry pi OS not working

I'm trying to make a custom OS for the raspberry pi. I follow this tutorial and used their Makefile, but when I try to compile it, I get this error:
echo objects/kernel.o objects/uart.o objects/stdio.o objects/stdlib.o objects/boot.o
objects/kernel.o objects/uart.o objects/stdio.o objects/stdlib.o objects/boot.o
./gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-gcc -T linker.ld -o kernel.img -ffreestanding -O2 -nostdlib objects/kernel.o objects/uart.o objects/stdio.o objects/stdlib.o objects/boot.o
process_begin: CreateProcess(NULL, ./gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-gcc -T linker.ld -o kernel.img -ffreestanding -O2 -nostdlib objects/kernel.o objects/uart.o objects/stdio.o objects/stdlib.o objects/boot.o, ...) failed.
make (e=2): Das System kann die angegebene Datei nicht finden. (Translation: The system can't find the given file)
Makefile:33: recipe for target 'build' failed
mingw32-make: *** [build] Error 2
and the Makefile:
# Don't use normal gcc, use the arm cross compiler
CC = ./gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-gcc
# Set any constants based on the raspberry pi model. Version 1 has some differences to 2 and 3
ifeq ($(RASPI_MODEL),1)
CPU = arm1176jzf-s
DIRECTIVES = -D MODEL_1
else
CPU = cortex-a7
endif
CFLAGS= -mcpu=$(CPU) -fpic -ffreestanding $(DIRECTIVES)
CSRCFLAGS= -O2 -Wall -Wextra
LFLAGS= -ffreestanding -O2 -nostdlib
# Location of the files
KER_SRC = ../src/kernel
KER_HEAD = ../include
COMMON_SRC = ../src/common
OBJ_DIR = objects
KERSOURCES = $(wildcard $(KER_SRC)/*.c)
COMMONSOURCES = $(wildcard $(COMMON_SRC)/*.c)
ASMSOURCES = $(wildcard $(KER_SRC)/*.S)
OBJECTS = $(patsubst $(KER_SRC)/%.c, $(OBJ_DIR)/%.o, $(KERSOURCES))
OBJECTS += $(patsubst $(COMMON_SRC)/%.c, $(OBJ_DIR)/%.o, $(COMMONSOURCES))
OBJECTS += $(patsubst $(KER_SRC)/%.S, $(OBJ_DIR)/%.o, $(ASMSOURCES))
HEADERS = $(wildcard $(KER_HEAD)/*.h)
IMG_NAME=kernel.img
build: $(OBJECTS) $(HEADERS)
echo $(OBJECTS)
$(CC) -T linker.ld -o $(IMG_NAME) $(LFLAGS) $(OBJECTS)
$(OBJ_DIR)/%.o: $(KER_SRC)/%.c
mkdir -p $(#D)
$(CC) $(CFLAGS) -I$(KER_SRC) -I$(KER_HEAD) -c $< -o $# $(CSRCFLAGS)
$(OBJ_DIR)/%.o: $(KER_SRC)/%.S
mkdir -p $(#D)
$(CC) $(CFLAGS) -I$(KER_SRC) -c $< -o $#
$(OBJ_DIR)/%.o: $(COMMON_SRC)/%.c
mkdir -p $(#D)
$(CC) $(CFLAGS) -I$(KER_SRC) -I$(KER_HEAD) -c $< -o $# $(CSRCFLAGS)
clean:
rm -rf $(OBJ_DIR)
rm $(IMG_NAME)
run: build
qemu-system-arm -m 1024 -M raspi2 -serial stdio -kernel kernel.img
But because I've never worked with Makefiles before, I don't know what is wrong with the Makefile

Compilation issues between servers

I'm using the following Makefile to compile and run a c program on my local machine
# project name
TARGET = server
CC = gcc
# compiling flags
CFLAGS = -std=c99 -Wall -I. -g
LINKER = gcc -o
# linking flags
LFLAGS = -Wall -I. -lm
SRCDIR = src
OBJDIR = obj
BINDIR = bin
SOURCES := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm = rm -f
$(BINDIR)/$(TARGET): $(OBJECTS)
mkdir -p $(BINDIR)
#$(LINKER) $# $(LFLAGS) $(OBJECTS)
#echo "Linking complete!"
$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
#$(CC) $(CFLAGS) -c $< -o $#
#echo "Compiled "$<" successfully!"
.PHONEY: clean
clean:
#$(rm) $(OBJECTS)
#echo "Cleanup complete!"
.PHONEY: remove
remove: clean
#$(rm) $(BINDIR)/$(TARGET)
#echo "Executable removed!"
This works perfectly on my local environment which is a mac(though there's nothing in the code that's mac specific).
However when I compile this code on a linux server I get the following error.
Build failed. Wrong Architecture.
When I manually compile the source files on this server running gcc file1 file2.... It compiles fine. What am I doing wrong with my Makefile.
Did you make clean before you make? I think there are some files or libraries were built on other architecture

undefined reference to `dlsym' Learn C The Hard Way

When I try to compile my unit test files i get 'undefined reference to `dlsym' error.
I read that on Unix system (I'm on Ubuntu 12.04) adding -ldl to compiler works, but I tried to work with Zed's Shaw Makefile and still nothing happened. This is the code :
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LIBS=-ldl $(OPTLIBS)
PREFIX?=/usr/local
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
TARGET=build/libex29.a
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
# The Target Build
all: $(TARGET) $(SO_TARGET) tests
dev: CFLAGS=-g -Wall -Isrc -Wall -Wextra $(OPTFLAGS)
dev: all
$(TARGET): CFLAGS += -fPIC
$(TARGET): build $(OBJECTS)
ar rcs $# $(OBJECTS)
ranlib $#
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o $# $(OBJECTS)
build:
#mkdir -p build
#mkdir -p bin
# The Unit Tests
.PHONY: tests
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
sh ./tests/runtests.sh
valgrind:
VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE)
# The Cleaner
clean:
rm -rf build $(OBJECTS) $(TESTS)
rm -f tests/tests.log
find . -name "*.gc*" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print`
# The Install
install: all
install -d $(DESTDIR)/$(PREFIX)/lib/
install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/
And the error for the record:
michal#ubuntu:~/Documents/projectsc/c-skeleton$ make
ar rcs build/libex29.a src/libex29.o
ranlib build/libex29.a
cc -shared -o build/libex29.so src/libex29.o
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -LIBS build/libex29.a tests/libex29_tests.c -o tests/libex29_tests
tests/libex29_tests.c: In function ‘main’:
tests/libex29_tests.c:66:1: warning: parameter ‘argc’ set but not used [-Wunused-but-set-parameter]
/tmp/ccRX6ddf.o: In function `check_function':
/home/michal/Documents/projectsc/c-skeleton/tests/libex29_tests.c:10: undefined reference to `dlsym'
/home/michal/Documents/projectsc/c-skeleton/tests/libex29_tests.c:11: undefined reference to `dlerror'
/tmp/ccRX6ddf.o: In function `test_dlopen':
/home/michal/Documents/projectsc/c-skeleton/tests/libex29_tests.c:24: undefined reference to `dlopen'
/tmp/ccRX6ddf.o: In function `test_dlclose':
/home/michal/Documents/projectsc/c-skeleton/tests/libex29_tests.c:49: undefined reference to `dlclose'
collect2: ld returned 1 exit status
make: *** [tests/libex29_tests] Error 1
As I said, I tried to add '-ldl- to CFLAGS, SO_TARGET variable, almost everything when according to my analysies this could be helpfull but this didin't change anything.
After reading this question: Library Linking
I changed LIBS to LDLIBS like this and it worked for me.
LDLIBS=-ldl $(OPTLIBS)
I think it is not problem of makefile. Probably you have wrong linked files in libex29_tests.c Post your headers from this file and files tree in your direcotry.
What worked for me with the same problem was adding -Wl,--no-as-needed as linker arguments.

makefile circular dependency and overriding commands

Have a reference makefile that im slowly editing and using which spits out these two errors
Makefile:25: warning: overriding commands for target `build/semanticHash'
Makefile:21: warning: ignoring old commands for target `build/semanticHash'
make: Circular build/semanticHash <- build/semanticHash dependency dropped.
cc -g -ldl -lgsl -lgslcblas -lzmq -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/semanticHash/rmb.o src/semanticHash/rmb.c
I'm new to makefile syntax and rules, so any common mistakes I Google for, but no luck for myself in this case.
So the question is, where am I causing these errors, and is there any patterns that I should avoid in my current makefile?
CFLAGS= -g $(LIBS) -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LIBS= -ldl $(OPTLIBS)
PREFIX?=/usr/local
OPTLIBS= -lgsl -lgslcblas -lzmq
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
TARGET=build/semanticHash # Rename to library !!!!!
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
# The Target Build
all: $(TARGET) tests
$(TARGET): CFLAGS += -fPIC
$(TARGET): build $(OBJECTS)
ar rcs $# $(OBJECTS)
ranlib $#
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o $# $(OBJECTS)
build:
#mkdir -p build
#mkdir -p bin
# The Unit Tests
.PHONY: tests
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
sh ./tests/runtests.sh
valgrind:
VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE)
# The Cleaner
clean:
rm -rf build $(OBJECTS) $(TESTS)
rm -f tests/tests.log
find . -name "*.gc*" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print`
# The Install
install: all
install -d $(DESTDIR)/$(PREFIX)/lib/
install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/
You set SO_TARGET to the same as TARGET:
TARGET=build/semanticHash # Rename to library !!!!!
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
As seen TARGET does not end with .a, so nothing will be substituted making SO_TARGET the same.
Later you have
$(SO_TARGET): $(TARGET) $(OBJECTS)
As both SO_TARGET and TARGET are the same, you have a circular dependency.
The other two warnings are because of this issue as well, as you have conflicting commands for the same target.
On an unrelated note, you should not mix compiler and linker flags like you do. Compiler flags are for compilation, linker flags for linking. You should also change the order of the linker options, and place the libraries to link with after the object files. This is because the GNU linker doesn't load libraries if there isn't anything depending on them, and dependencies are not loaded until it loads the object files.

Resources