3

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.

Michał Bil
  • 3,179
  • 5
  • 20
  • 24

3 Answers3

7

After reading this question: Library Linking

I changed LIBS to LDLIBS like this and it worked for me.

LDLIBS=-ldl $(OPTLIBS)
Community
  • 1
  • 1
Olppah
  • 790
  • 1
  • 10
  • 21
0

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.

Piotr Zych
  • 483
  • 4
  • 19
0

What worked for me with the same problem was adding -Wl,--no-as-needed as linker arguments.

tomato
  • 747
  • 5
  • 11