Undefined symbol with BoringSSL - makefile

I'm writing a test application with BoringSSL and when I execute it I get this error:
symbol lookup error: ./main: undefined symbol: SSL_CTX_set_min_proto_version
All needed libraries are getting linked in my makefile:
CC = g++
FLAGS = -Wall -g
INCLUDES = -I/home/denis/libraries/boringssl/include
LIBS = -L/home/denis/libraries/boringssl/build/crypto -L/home/denis/TLS-Bibliotheken/boringssl/build/ssl -lcrypto -lssl
OBJ = Main.o BoringSSLTest.o
# Link files
main: $(OBJ)
$(CC) $(FLAGS) -o main $(OBJ) $(LIBS)
# Compile files
Main.o: Main.cpp
$(CC) $(FLAGS) -c $(INCLUDES) Main.cpp
BoringSSLTest.o: BoringSSLTest.cpp BoringSSLTest.h
$(CC) $(FLAGS) -c $(INCLUDES) BoringSSLTest.cpp
clean:
$(RM) $(OBJ) main
When I look for the definition of the symbol with nm libssl.so | grep SSL_CTX_set_min_proto_version I see that it is not undefined (T).
Why does it not work?

It is probably loading a wrong version of libssl.so at run-time. Invoke ldd ./main to see where it loads libssl.so from.
When you use -L<dir> linker option to link shared libraries from a non-standard location you need the corresponding -Wl,-rpath=<dir> linker option to tell the run-time linker to load the library from there.

Related

Makefile cant find libraries

I'm trying to write my own makefile for a paho.mqtt project on a Raspberry Pi 4.
I've downloaded & tested the paho.mqtt install and its all working as expected.
So I'm now testing some C code but I just cant figure out the makefile (I'm new to this), my file so far,
NAME = mqtt_test
OBJ = $(NAME).o
LIBS = -libpaho-mqtt3c -libpaho-mqtt3cs
CFLAGS = -Wall -I/usr/local/include -L/usr/local/lib
CC = gcc
EXTENSION = .c
all: $(NAME)
%.o: %$(EXTENSION) $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
$(NAME): $(OBJ)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
#rm -f *.o *~ core $(NAME)
This returns,
gcc -o mqtt_test mqtt_test.o -Wall -I/usr/local/include -L/usr/local/lib -libpaho-mqtt3c -libpaho-mqtt3cs
/usr/bin/ld: cannot find -libpaho-mqtt3c
/usr/bin/ld: cannot find -libpaho-mqtt3cs
collect2: error: ld returned 1 exit status
make: *** [makefile:14: mqtt_test] Error 1
I've checked & the includes and libraries are in the directories I put after the-I and -L flags.
When I look in /usr/bin there is no ld but there are paho files prefixed with paho_ but no library files.
What am I missing?
You don't use -libpaho-mqtt3c (etc.)
The option is -l so when you write -libpaho-mqtt3c the linker is looking for libraries named ibpaho-mqtt3c which of course do not exist: that would be either libibpaho-mqtt3c.a or libibpaho-mqtt3c.so.
You want to use -lpaho-mqtt3c: remove the lib at the front and the extension .a or .so, and add in the option -l.

Makefile - include files not found when building with multiple source files

I have a make file:
# Makefile
CC = gcc
SRC = pixel.c
TARGET = pixel.exe
OBJECTS = bmp.o pixel.o
#CFLAGS_W = -Wall -Wl,-subsystem,windows
CFLAGS_W = -Wall
LFLAGS_W = -lSDL2
INCS_W = -I.\SDL2\include
LIBS_W = -L.\SDL2\lib\x64
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET) $(OBJECTS)
it will build bmp.c, but when it tries to build pixel.c I get the error:
pixel.c:6:10: fatal error: SDL.h: No such file or directory
however, pixel.c has been building fine all the time I was just building it on its own (before I added bmp.c), using the command:
all:
$(CC) $(SRC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET)
I really struggle with makefiles because I just don't use them enough, but this doesn't really feel like a "make" problem.
I'm using mingw32-make and gcc in a windows sandbox.
EDIT:
I have added
.c.o:
$(CC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -c $< -o $#
to my makefile, which gets me a step further (I think), but now complains that all of the SDL functions are undefined
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel>mingw32-make
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -c bmp.c -o bmp.o
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -c pixel.c -o pixel.o
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -o pixel.exe bmp.o pixel.o
pixel.o: In function `main':
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:119: undefined reference to `SDL_Init'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:121: undefined reference to `SDL_GetError'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:126: undefined reference to `SDL_CreateWindow'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:131: undefined reference to `SDL_GetError'
...
I can get the whole project to compile if I just do this on its own:
SRC = bmp.c pixel.c
...
test:
$(CC) $(SRC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET)
which is fine, it'll do, but it rebuilds everything every time, and I'd like to know why it isn't working anyway :)

gcc undefined reference with reference paths defined in makefile

I am learning GCC and have some undefined reference errors when running make. I am using a makefile which I did not create, however while troubleshooting these undefined reference errors I went through each line of the makefile to understand what is happening and try to fix the problem but haven't been successful although I did learn a lot about the syntax used in the makefile.
Here is the undefined reference output when using make:
/usr/bin/arm-none-eabi-gcc -O0 -g -mcpu=cortex-m3 -mthumb -I/home/np/STMBook -I/home/np/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x -I/home/np/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/CoreSupport -I/home/np/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/inc -I. -DSTM32F10X_MD_VL -DUSE_STDPERIPH_DRIVER -DUSE_FULL_ASSERT -I/home/np/STMBook/Library/ff9/src -I/home/np/STMBook/Library -T/home/np/STMBook/stm32f100.ld -mthumb -mcpu=cortex-m3 BlinkLight.c -o BlinkLight
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/bin/ld: warning: cannot find entry symbol Reset_Handler; defaulting to 08000000
/tmp/ccZbJmMi.o: In function `main':
/home/np/STMBook/BlinkLight.c:11: undefined reference to `RCC_APB2PeriphClockCmd'
/home/np/STMBook/BlinkLight.c:14: undefined reference to `GPIO_StructInit'
/home/np/STMBook/BlinkLight.c:18: undefined reference to `GPIO_Init'
/home/np/STMBook/BlinkLight.c:21: undefined reference to `SystemCoreClock'
/home/np/STMBook/BlinkLight.c:21: undefined reference to `SystemCoreClock'
/home/np/STMBook/BlinkLight.c:30: undefined reference to `GPIO_WriteBit'
collect2: error: ld returned 1 exit status
make: *** [BlinkLight] Error 1
And here is my makefile which has two parts:
Part 1:
TEMPLATEROOT = /home/np/STMBook
# compilation flags for gdb
CFLAGS += -O0 -g
ASFLAGS += -g
# object files
OBJS= $(STARTUP) BlinkLight.o
OBJS+= stm32f10x_gpio.o stm32f10x_rcc.o
# include common make file
include $(TEMPLATEROOT)/Makefile.common
Part 2 (makefile.common):
# name of executable
ELF=$(notdir $(CURDIR)).elf
# Tool path
TOOLROOT=/usr/bin
# Library path
LIBROOT=/home/np/STM32F10x_StdPeriph_Lib_V3.5.0
# Tools
CC=$(TOOLROOT)/arm-none-eabi-gcc
LD=$(TOOLROOT)/arm-none-eabi-gcc
AR=$(TOOLROOT)/arm-none-eabi-ar
AS=$(TOOLROOT)/arm-none-eabi-as
# Code Paths
DEVICE=$(LIBROOT)/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x
CORE=$(LIBROOT)/Libraries/CMSIS/CM3/CoreSupport
PERIPH=$(LIBROOT)/Libraries/STM32F10x_StdPeriph_Driver
# Search path for standard files
vpath %.c $(TEMPLATEROOT)
# Search path for perpheral library
vpath %.c $(CORE)
vpath %.c $(PERIPH)/src
vpath %.c $(DEVICE)
# Search path for Library
vpath %.c $(TEMPLATEROOT)/Library/ff9/src
vpath %.c $(TEMPLATEROOT)/Library/ff9/src/option
vpath %.c $(TEMPLATEROOT)/Library
# Processor specific
PTYPE = STM32F10X_MD_VL
LDSCRIPT = $(TEMPLATEROOT)/stm32f100.ld
STARTUP= startup_stm32f10x.o system_stm32f10x.o
# Compilation Flags
FULLASSERT = -DUSE_FULL_ASSERT
LDFLAGS+= -T$(LDSCRIPT) -mthumb -mcpu=cortex-m3
CFLAGS+= -mcpu=cortex-m3 -mthumb
CFLAGS+= -I$(TEMPLATEROOT) -I$(DEVICE) -I$(CORE) -I$(PERIPH)/inc -I.
CFLAGS+= -D$(PTYPE) -DUSE_STDPERIPH_DRIVER $(FULLASSERT)
CFLAGS+= -I$(TEMPLATEROOT)/Library/ff9/src -I$(TEMPLATEROOT)/Library
# Build executable
$(ELF) : $(OBJS)
$(LD) $(LDFLAGS) -o $# $(OBJS) $(LDLIBS)
# compile and generate dependency info
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $#
$(CC) -MM $(CFLAGS) $< > $*.d
%.o: %.s
$(CC) -c $(CFLAGS) $< -o $#
clean:
rm -f $(OBJS) $(OBJS:.o=.d) $(ELF) startup_stm32f* $(CLEANOTHER)
debug: $(ELF)
arm-none-eabi-gdb $(ELF)
# pull in dependencies
-include $(OBJS:.o=.d)
Here is my understanding:
The undefined references are contained in files stm32f10x_gpio.c (GPIO undefined references) and stm32f10x_rcc.c (RCC undefined reference) and these are declared as required objects in the OBJS variable in Part 1 of the makefile:
OBJS= $(STARTUP) BlinkLight.o
OBJS+= stm32f10x_gpio.o stm32f10x_rcc.o
Undefined reference 'SystemCoreClock' is located in system_stm32f10x.c file in the directory given in the makefile as:
STARTUP= startup_stm32f10x.o system_stm32f10x.o
and is included in the OBJS variable.
The path to the .c files for compiling the required objects are defined in the makefile:
Location of stm32f10x_gpio.c and stm32f10x_rcc.c:
vpath %.c $(PERIPH)/src
Location of system_stm32f10x.c:
vpath %.c $(DEVICE)
All of the required objects which contain the undefined references are passed to the target build instructions as variable OBJS:
$(ELF) : $(OBJS)
$(LD) $(LDFLAGS) -o $# $(OBJS) $(LDLIBS)
Also included in the makefile is build instructions on how to obtain the object files from the C source files:
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $#
$(CC) -MM $(CFLAGS) $< > $*.d
I do not understand why it seems to be that the C source files cannot be found. Thank you for any suggestions, I really appreciate any help.
Sorry for my post being so long, I tried to go through each line of the makefile and research all the symbols and syntax to figure out the problem on my own and went through many of the undefined reference questions on this site but have not figured it out.

Error on makefile with threads and my library

So I have been searching and looking for something that could help me with the Makefile, but I did not find anything, so thats why I am here.
My makefile right now is like this:
CC = gcc
CFLAGS = -Wall
LDFLAGS += -L$(LIBB)
LDFLAGS += -static lib1.h
LDLIBS = -lm -lpthread -lrt -l
SOURCES=lib1.c prac3.c prac3_reader.c
LIBRARIES=lib1.o
INCLUDES=lib1.h
PROGRAMS=prac3 prac3_reader
all: $(OBJS) $(PROGRAMS)
$(PROGRAMS): $(LIBRARIES) $(INCLUDES)
$(CC) $(LDFLAGS) $(LIBRARIES) $(LDLIBS) $#.o -o $#
%.o: %.c $(INCLUDES)
$(CC) $(CFLAGS) -o $# -c $<
clean:
rm -rf *.o *~ $(PROGRAMS)
I know there are probably a lot of things that can be removed, but I do not really know which ones are. I have two programs called
prac3.c
and
prac3_reader.c
Also, I have my own library called
lib1.c
and also compiled like
lib1.h
When I go to my directory with the terminal and use the command make I recive this error:
gcc -L -static lib1.h lib1.o -lm -lpthread -lrt -l prac3.o -o prac3
/usr/bin/ld: no se puede encontrar -lprac3.o
collect2: error: ld returned 1 exit status
Makefile:15: recipe for target 'prac3' failed
make: *** [prac3] Error 1
I am running on Ubuntu.
The -l flag expects an argument. When it is combined in the gcc statement it causes the prac3.o argument to be considered as the name of a library. There is no such library prac3.o, so you get the error.
In general .o files aren't "libraries". They are object files. Remove the -l flag and you will be fine.
"libraries" are generally .a or .so files from a library path - but even then, you wouldn't specify the suffix (.e.g "-lpthreads").

ld can't find library given -L

I have an object file main.o, and need to link it against a shared library at ./libsvm/libsvm.so.2. I have the following Makefile but it doesn't work for me. Library path has been specified in -L./libsvm but gcc -lsvm still can't find the shared library (libsvm.so.2).
This is my Makefile:
CC = g++ -g
CFLAGS = -Wall
HEADERS = -I./libsvm
OBJ = main.o
LIBS = -L./libsvm
all: lib $(OBJ)
$(CC) $(LIBS) -lsvm $(OBJ) -o main
%.o: %.c
$(CC) $(CFLAGS) $(HEADERS) -c -o $# $<
lib:
cd libsvm; make
It just works if link them directly, as in
ld main.o libsvm/libsvm.so.2 -o main
I wonder what's wrong in the Makefile. Error message is the following
g++ -g -L./libsvm -lsvm main.o -o main
ld: library not found for -lsvm
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
-lsvm means use the file svm.so
But your library file has name svm.so.2. (Version 2)
So either rename or make a symbolic link with
ln -s svm.so.2 svm.so
Now the makefile should work.

Resources