Makefile编译错误

时间:2014-10-26 16:04:24

标签: c

我有一个makefile但不起作用。终端中的错误是: cc -shared -o build / liblcthw.so src / lcthw / list.o

/ usr / bin / ld:src / lcthw / list.o:针对`.rodata'重新定位R_X86_64_32制作共享对象时不能使用;用-fPIC重新编译 src / lcthw / list.o:错误添加符号:错误值

我需要帮助把gcc命令gcc -shared -o target.so -fPIC target.c,我不知道如何把target.c放到list.o 这是makefile:

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/liblcthw.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/

# The Checker
BADFUNCS='[^_.>a-zA-Z0-9](str(n?cpy|n?cat|xfrm|n?dup|str|pbrk|tok|_)|stpn?cpy|a?sn?printf|byte_)'
check:
    @echo Files with potentially dangerous functions.
    @egrep $(BADFUNCS) $(SOURCES) || true

1 个答案:

答案 0 :(得分:1)

liblcthw.a的对象是使用-fPIC构建的。 liblcthw.so的对象不是使用此设置构建的。

尝试更改规则:

$(SO_TARGET): $(TARGET) $(OBJECTS)
    $(CC) -shared -o $@ $(OBJECTS)

为:

$(SO_TARGET): $(TARGET)
    $(CC) -shared -o $@ $^

由于所需对象已在liblcthw.a

或者,或者:

$(SO_TARGET): CFLAGS += -fPIC
$(SO_TARGET): $(OBJECTS)
    $(CC) -shared -o $@ $(OBJECTS)