2013-10-05 60 views
3

當我嘗試編譯我的單元測試文件時,我得到'未定義對`dlsym'的引用錯誤。 我在Unix系統上(我在Ubuntu 12.04上)添加了-ldl到編譯器工程,但我試圖使用Zed的Shaw Makefile,但仍然沒有發生任何事情。這是代碼:未定義對`dlsym'的引用學習C困難的方式

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 [email protected] $(OBJECTS) 
    ranlib [email protected] 

$(SO_TARGET): $(TARGET) $(OBJECTS) 
    $(CC) -shared -o [email protected] $(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/ 

並記錄在案的錯誤:根據時

[email protected]:~/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 

正如我所說的,我試圖-ldl-添加」 CFLAGS,SO_TARGET變量,幾乎所有的東西我分析這可能是有幫助的,但這並沒有改變任何東西。

回答

0

我認爲這不是makefile的問題。可能您在libex29_tests.c中鏈接了錯誤的文件。在您的direcotry中從此文件和文件樹中發佈您的標題。

0

對於我來說,同樣的問題有效的是將-Wl,--no-as-needed作爲鏈接器參數。

6

閱讀這個問題後:Library Linking

我改變LIBS到LDLIBS這樣,它爲我工作。

LDLIBS=-ldl $(OPTLIBS) 
相關問題