I'm using Ubuntu 16.10 and it uses openssl in the version 1.0.2g. I've downloaded the latest source-code openssl-1.1.0c from the website and compiled it, which worked fined. Now I've downloaded a example code and tried to compile it, but I've got a linking error.
rm -f ./cert.o
gcc -c -o cert.o cert.c
gcc -Wall -g -fPIC -I../../libraries/c/openssl-1.1.0c/include/openssl -o cert ./cert.o ../../libraries/c/openssl-1.1.0c/libssl.a ../../libraries/c/openssl-1.1.0c/libcrypto.a -lpthread -ldl
./cert.o: In Funktion `main':
cert.c:(.text+0x48d): Nicht definierter Verweis auf `sk_new_null'
cert.c:(.text+0x4d4): Nicht definierter Verweis auf `sk_push'
cert.c:(.text+0x74e): Nicht definierter Verweis auf `sk_free'
collect2: error: ld returned 1 exit status
Makefile:13: die Regel für Ziel „cert“ scheiterte
make: *** [cert] Fehler 1
Makefile:
appname := cert
CC := gcc
CCFLAGS := -Wall -g -fPIC -I../../libraries/c/openssl-1.1.0c/include/openssl
LDLIBS := ../../libraries/c/openssl-1.1.0c/libssl.a ../../libraries/c/openssl-1.1.0c/libcrypto.a -lpthread -ldl
srcfiles := $(shell find . -maxdepth 1 -name "*.c")
objects := $(patsubst %.c, %.o, $(srcfiles))
all: clean $(appname)
$(appname): $(objects)
$(CC) $(CCFLAGS) $(LDFLAGS) -o $(appname) $(objects) $(LDLIBS)
clean:
rm -f $(objects)
So i've looked for "sk_new_null" found it in "/usr/include/openssl/stack.h".
In openssl-1.1.0c it were renamed to "OPENSSL_sk_new_null".
I think, it looks for the wrong header-files.
I'm a noob in C, how do I've change the Makefile?
I've changed the wrong paths, but with almost no effect, there were also missing references. It seems that it used some of the openssl-headers in "/usr/include".
But I learned a big lesson, use "make install". I've configured openssl-1.1.0c with an prefix path to "../../libraries/c/usr". With nanomsg I did the same.
Then I add "-I../../libraries/c/usr/include" and "-L../../libraries/c/usr/lib" to the CFLAGS. It works. I can link statically and dynamically to these libraries and it use the correct header-files.
I will always install it by now to an relative path and point to it.
Related
I'm trying to make a dynamic library from source code but getting the following error. I just could not figure out why.
ld: entry.o: relocation R_X86_64_32S against `.text' can not be used when making a shared
object; recompile with -fPIC
entry.o: error adding symbols: Bad value
Makefile:12: recipe for target 'libdune.so' failed
make: *** [libdune.so] Error 1
I have the source code and my Makefile looks like the following
CC = gcc
CFLAGS = -Wall -fPIC -g -O3 -MD
LDFLAGS = -shared
OBJ = entry.o dune.o vsyscall.o elf.o vm.o util.o page.o procmap.o debug.o apic.o
NOFPU_OBJ = trap.o
$(NOFPU_OBJ): EXTRA_FLAGS := -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -msoft-float
all: libdune.so
libdune.so: $(OBJ) $(NOFPU_OBJ)
$(LD) -shared -o $(#) $(OBJ) $(NOFPU_OBJ)
clean:
rm -f *.o test *.d libdune.so
-include *.d
%.o: %.c
$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $# -c $<
“relocation R_X86_64_32S against ” linking Error talks about the almost same problem but in my case, I have the source code that is needed. Any help on how to make it work?
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
I am trying to compile a simulation program called COSI:
http://www.broadinstitute.org/~sfs/cosi/cosi_1.2.1.tar
After unzipping it with tar xfp cosi_package.tar, I try to run make as stated in the README within the newly created directory cosi_1.2. Unfortunately, I get a lot of errors such as
coalescent.a(historical.o): In function `historical_process_pop_event':
historical.c:(.text+0x5c7): undefined reference to `log'
coalescent.a(historical.o): In function `historical_next_exp':
historical.c:(.text+0x76a): undefined reference to `exp'
../cosi_rand/random.a(ranbinom.o):ranbinom.c:(.text+0x702): more undefined references to `log' follow
collect2: ld returned 1 exit status
make[1]: *** [coalescent] Error 1
make[1]: Leaving directory `/home/myname/Desktop/cosi_1.2/cosi'
make: *** [all] Error 2
The MAKEFILE in the problematic sub-directory looks like this:
PACKAGE := coalescent
BINFILES := coalescent.c
SKIPFILES :=
ARFILE := $(PACKAGE).a
BINS := $(BINFILES:.c=)
PREFILES := $(wildcard *.c)
CFILES := $(filter-out $(SKIPFILES),$(PREFILES))
LIBFILES := $(filter-out $(BINFILES),$(CFILES))
OFILES := $(LIBFILES:.c=.o)
DFILES := $(CFILES:.c=.d)
FOO := $(ARFILE)(
BAR := )
AFILES := $(addprefix $(FOO), $(OFILES))
AFILES := $(addsuffix $(BAR), $(AFILES))
CC := gcc
CFLAGS := $(DEBUG) -O3 -Wall -ansi
all : $(BINS)
# rm *.d; rm *.o
install : $(BINS)
rm *.d; rm *.o
.PHONY : cleaninstall
cleaninstall :
rm $(BINS)
$(ARFILE) : $(AFILES)
$(AR) cr $(ARFILE) $(?:.c=.o)
$(RM) $(?:.c=.o)
$(BINS) : % : %.o $(ARFILE)
ranlib $(ARFILE)
$(CC) $(CFLAGS) -lm -v -o $# $#.o $(ARFILE) ../cosi_rand/random.a
%.d: %.c
#$(SHELL) -ec '$(CC) -MM $(CPPFLAGS) $< \
| sed '\''s/\($*\)\.o[ :]*/coalescent.a(\1.o) $# : /g'\'' > $#; \
[ -s $# ] || rm -f $#'
-include $(DFILES)
Obviously, all the errors are missing mathematical functions. Hence I tried adding -lm to CFLAGS but it didn't help. Any suggestions what I could do?
-lm is a linker flag. It should appear at the end of the linker command:
$(CC) -v -o $# $#.o $(ARFILE) ../cosi_rand/random.a -lm
Apparently some compilers permit -l to appear anywhere. I still haven't found out which ones do, but my GCC wants them at the end, and in reverse order of dependency: if random.a needs libm, then libm should be linked in after random.a.
I also removed $(CFLAGS) from the linker command because you should pass linker options, not compiler options, when linking.
The linker searches for dependencies in the order they are on the command line. So when you ask the linker to link with a library (with e.g. -lm) then the linker will see if there is anything that depends on the library. If there isn't then the library is discarded.
To solve this problem it's recommended that you should always put libraries after all source/object files.
Note: This dependency order is also used between libraries, so if you have a library A that depends on library B, then you need to place A before B on the command line. This of course makes it hard if you have two libraries that depend on each other.
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.
I'm building a little OpenWRT application and I want to statically link a library to it.
EDIT: This happens with other libraries as well, not only libcurl.
I'm getting this error while building it:
make[3]: Entering directory `/home/md/work/openwrt/build_dir/target-mips_r2_uClibc-0.9.33.2/app'
mips-openwrt-linux-uclibc-gcc -c -Os -pipe -mips32r2 -mtune=34kc -mno-branch-likely -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float -Wall -Werror main.c -o main.o
mips-openwrt-linux-uclibc-gcc -Os -pipe -mips32r2 -mtune=34kc -mno-branch-likely -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -msoft-float -Wall -Werror -L/home/md/work/openwrt/staging_dir/target-mips_r2_uClibc-0.9.33.2/usr/lib -L/home/md/work/openwrt/staging_dir/target-mips_r2_uClibc-0.9.33.2/lib -L/home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/usr/lib -L/home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/lib -Wl,-Bstatic -lcurl main.o -o app
/home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/lib/gcc/mips-openwrt-linux-uclibc/4.6.4/../../../../mips-openwrt-linux-uclibc/bin/ld: cannot find -lgcc_s
/home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/lib/gcc/mips-openwrt-linux-uclibc/4.6.4/../../../../mips-openwrt-linux-uclibc/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status
make[3]: *** [app] Error 1
It's weird, because I have libgcc_s.so on the search path:
stormbreaker:openwrt> find . -name libgcc_s.*
./build_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/uClibc-0.9.33.2/libc/sysdeps/linux/common/libgcc_s.h
./build_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/gcc-linaro-4.6-2012.12-final/gcc/libgcc_s.so
./build_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/gcc-linaro-4.6-2012.12-final/gcc/libgcc_s.so.1
./build_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/gcc-linaro-4.6-2012.12-final/mips-openwrt-linux-uclibc/libgcc/libgcc_s.so
./build_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/gcc-linaro-4.6-2012.12-final/mips-openwrt-linux-uclibc/libgcc/libgcc_s.so.1
./build_dir/target-mips_r2_uClibc-0.9.33.2/toolchain/ipkg-ar71xx/libgcc/lib/libgcc_s.so.1
./build_dir/target-mips_r2_uClibc-0.9.33.2/toolchain/libgcc_s.so.1
./staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libgcc_s.so
./staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libgcc_s.so.1
./staging_dir/target-mips_r2_uClibc-0.9.33.2/root-ar71xx/lib/libgcc_s.so
./staging_dir/target-mips_r2_uClibc-0.9.33.2/root-ar71xx/lib/libgcc_s.so.1
I tried some hacks with -rpath and -rpath-link, but got the same result. As far as I know, libcurl doesn't need libgcc_s.
I created a simple case to reproduce this:
The relevant part of openwrt/package/app/Makefile:
TARGET_CFLAGS += -Wall -Werror
TARGET_LIBS = -Wl,-Bstatic -lcurl
define Build/Compile
CC="$(TARGET_CC)" \
CFLAGS="$(TARGET_CFLAGS)" \
LDFLAGS="$(TARGET_LDFLAGS)" \
LIBS="$(TARGET_LIBS)" \
$(MAKE) -C $(PKG_BUILD_DIR)
endef
openwrt/package/app/src/Makefile:
APP = app
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
$(APP): $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) $(OBJECTS) -o $(APP)
# Objects
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $#
The application itself is a single file:
#include <stdio.h>
#include <curl/curl.h>
int main (void)
{
curl_global_init(CURL_GLOBAL_ALL);
printf("Ok!\n");
return 0;
}
Increasing the verbosity of the linker (using -Wl,--verbose=99) gave me these clues:
...
attempt to open /home/md/work/openwrt/staging_dir/target-mips_r2_uClibc-0.9.33/usr/lib/libgcc_s.a failed
attempt to open /home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/lib/gcc/mips-openwrt-linux-uclibc/4.6.3/libgcc_s.a failed
attempt to open /home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/lib/gcc/mips-openwrt-linux-uclibc/4.6.3/../../../../mips-openwrt-linux-uclibc/lib/libgcc_s.a failed
attempt to open /home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/mips-openwrt-linux-uclibc/bin/../../../toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/usr/local/lib/libgcc_s.a failed
attempt to open /home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/mips-openwrt-linux-uclibc/bin/../../../toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/lib/libgcc_s.a failed
attempt to open /home/md/work/openwrt/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/mips-openwrt-linux-uclibc/bin/../../../toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33/usr/lib/libgcc_s.a failed
etc.
Looks like the static version of libgcc_s is missing.
Anyway, I changed my package Makefile to:
TARGET_LIBS = -Wl,-Bdynamic -lgcc_s \
-Wl,-Bstatic -lcurl
define Build/Compile
$(TARGET_CONFIGURE_OPTS) \
CFLAGS="$(TARGET_CFLAGS)" \
LDFLAGS="$(TARGET_LDFLAGS)" \
LIBS="$(TARGET_LIBS)" \
$(MAKE) -C $(PKG_BUILD_DIR)
endef
Works for me =)
I understand why linking with a static library would need a static version of its dependencies, But I wasn't expecting the linker to do it behind my back, without falling back the dynamic version first.