Makefile error: /usr/bin/ld: cannot find -lsqlite3 - c

I've installed sqlite3 from source on Linux and placed this into the subdirectory of C code with a Makefile. This is what the Makefile looks like
CC = gcc
CFLAGS = -Wall -g -std=c99
SOURCES := src/file1.c src/file2.c src/file3.c
LIB := -lm -lsqlite3
INC := -I include -I path/to/pathname/sqlite3/include
all:
#mkdir -p bin/
$(CC) $(CFLAGS) $(SOURCES) main.c -L path/to/pathname/sqlite3/ -o bin/software $(LIB) $(INC)
clean:
rm -f bin/sofware
However, whenever I try executing make, I get this error:
gcc -Wall -g -std=c99 src/file1.c src/file2.c src/file3.c -I include -I path/to/pathname/sqlite3/include
/usr/bin/ld: cannot find -lsqlite3
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
I don't understand. sqlite3 is in path/to/pathname/sqlite3/
Here is the list of files/executables in path/to/pathname/sqlite3/:
aclocal.m4 config.log configure.ac install-sh ltmain.sh missing shell.o sqlite3.h sqlite3.pc.in
autom4te.cache config.status depcomp lib Makefile README sqlite3 sqlite3.lo tea
bin config.sub include libsqlite3.la Makefile.am share sqlite3.c sqlite3.o
config.guess configure INSTALL libtool Makefile.in shell.c sqlite3ext.h sqlite3.pc
How does one properly allow this C code to compile properly with access to sqlite3?

You need to add -L/path/to/lib/dir

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 compiling and linking problems

So I've been trying to search for a solution that Im having with my make file currently. I get the error:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
So I have 5 files in my src folder: find.c, rebalance.c, remove.c, lib.c, and insert.c. In my src/bin folder I just have main.c and in another folder 'include' outside of src I have bst.h. This is what my makefile looks like:
all: static bin shared
mkdir -p build/bin
mkdir -p build/lib
mkdir -p build/objects
mv *.o build/objects
mv *.a build/lib
mv main build/bin
mv libbst.so build/lib
static:
gcc src/lib.c -c -I include
gcc src/find.c -c -I include
gcc src/insert.c -c -I include
gcc src/rebalance.c -c -I include
gcc src/remove.c -c -I include
ar rcs libbst.a find.o insert.o lib.o rebalance.o remove.o
shared:
gcc -c -o lib.o src/lib.c
gcc -c -o find.o src/find.c
gcc -c -o insert.o src/insert.c
gcc -c -o rebalance.o src/rebalance.c
gcc -c -o remove.o src/remove.c
gcc -shared -o libbst.so find.o insert.o lib.o rebalance.o remove.o
bin:
gcc src/lib.c -c -I include
gcc lib.o -o lib -lbst -L.
gcc src/bin/main.c -c -I include
gcc main.o -o main -lbst -L.
clean:
rm -rf *.so *.a *.o main build
install:
Im assuming that I'm just not using the right commands to link everything together. This always fails in the 'bin' section of the Makefile. I would really appreciate any help or advice on what I can do here, im kind of stumped right now.

make: the system cannot find the file specified (Error 2)

I'm trying to compile a C program on Windows for use on a linux dev board.
When I try to compile using a makefile I get this output:
$ make
arm-linux-gnueabihf-gcc -g -Wall main.c -o filetest
process_begin: CreateProcess(NULL, arm-linux-gnueabihf-gcc -g -Wall main.c -o filetest, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [filetest] Error 2
This happens whether or not a blank file called filetest is present or not in the same directory. If it is present, I can run make clean and it will remove the file.
Here is the makefile I'm using:
#
TARGET = filetest
ALT_DEVICE_FAMILY ?= soc_cv_av
SOCEDS_ROOT ?= $(SOCEDS_DEST_ROOT)
HWLIBS_ROOT = $(SOCEDS_ROOT)/ip/altera/hps/altera_hps/hwlib
CROSS_COMPILE = arm-linux-gnueabihf-
CFLAGS = -g -Wall -D$(ALT_DEVICE_FAMILY) -I$(HWLIBS_ROOT)/include/$(ALT_DEVICE_FAMILY) -I$(HWLIBS_ROOT)/include/
LDFLAGS = -g -Wall
CC = $(CROSS_COMPILE)gcc
ARCH= arm
build: $(TARGET)
#
$(TARGET):main.c
$(CC) $(LDFLAGS) $^ -o $#
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $#
.PHONY: clean
clean:
rm -f $(TARGET) *.a *.o *~
I used tabs in my actual makefile instead of spaces for the code block above ^^
Also, I'm working within Intel's FPGA SoC EDS for a Cyclone V board.

how to add interdependent libraries in a makefile

I need to make a makefile for my project and I have two interdependent libraries which are libpcsc.so and libccid.so .
could someone tell me what my mistake is? Thanks for your answers in advance.
and please let me know when you need more information.
my makefile is like:
by the way, one of lib directory is /home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/ and other one's is usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/
CC = gcc
LIB_DIRS = -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/
-L../../usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/
INCLUDE_DIR = /home/hasanbucak/Desktop/ccid-1.4.28/src/ #ccid_usb.h
CFLAGS = -g -Wall
obj-y:= smcard
default: all
all: smcard
smcard:
$(CC) $(CFLAGS) $(LIB_DIRS) -I$(INCLUDE_DIR) -c -o $(obj-y).o $(obj-y).c
$(CC) $(CFLAGS) -lccid -lpcsclite $(LIB_DIRS) -I$(INCLUDE_DIR) -o $(obj-y) $(obj-y).c
clean:
rm $(obj-y).o $(obj-y)
and when I write make in terminal , it says:
hasanbucak#ubuntu:~/Desktop/hasan$ make
gcc -g -Wall -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/ -L../../usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/ -I/home/hasanbucak/Desktop/ccid-1.4.28/src/ -c -o smcard.o smcard.c
gcc -g -Wall -lccid -lpcsclite -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/ -L../../usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/ -I/home/hasanbucak/Desktop/ccid-1.4.28/src/ -o smcard smcard.c
/usr/bin/ld: cannot find -lccid
collect2: error: ld returned 1 exit status
Makefile:20: recipe for target 'smcard' failed
make: *** [smcard] Error 1
Make already told you what was wrong with the Makefile:
/usr/bin/ld: cannot find -lccid
You should specify the path to ccid properly:
LIB_DIRS = -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/
-L/usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/
Note that /usr and ../../usr are not at all the same directory.
The first one is absolute (i.e. relative to your root directory), while the second is relative, it expands to ~/Desktop/hasan/../../usr, which is equivalent to /home/hasanbucak/usr.

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.

Resources