Questão principal
Estou usando GNU/Linux, enviando código C para uma biblioteca compartilhada bin/libcore.so
e, em seguida, usando a biblioteca na criação de um executável bin/test
. Os arquivos são criados com sucesso, mas bin/test
ocorre o seguinte erro ao executar:
bin/test: error while loading shared libraries: libcore.so: cannot open shared object file: No such file or directory
Subquestão
Separei isso em uma questão própria porque pode estar completamente fora do caminho certo.
Li que meu problema pode ser porque preciso definir -Wl,-rpath,bin
ao compilar bin/test
(tive que passar por algo parecido para fazer isso funcionar no Windows, ou seja, definir -Wl,-out-implib,bin/libcore.dll.a
).
Eu passo LDFLAGS
para meu Makefile da seguinte maneira:
make -f build/Makefile PLATFORM="linux" \
TARGET="bin/libcore.so" \
LDFLAGS="-shared" \
... \
e em build/Makefile
, tenho as seguintes linhas que fazem referência LDFLAGS
a:
# Linker flags.
ifeq ($(PLATFORM),linux)
LDFLAGS += -Wl,-rpath,bin
endif
ifeq ($(suffix $(TARGET)),.dll)
LDFLAGS += -Wl,--out-implib,$(dir $(TARGET))lib$(basename $(notdir $(TARGET))).dll.a
endif
# Build target.
$(TARGET): $(OBJ_FILES)
$(CC) -o $@ $^ $(LDFLAGS)
Mas quando tento executar o Makefile, recebo:
LDFLAGS += -Wl,-rpath,bin
make[1]: LDFLAGS: No such file or directory
Solução
Conforme observado na resposta aceita, a sintaxe do meu Makefile estava incorreta. Além disso, atualizei rpath
para $$ORIGIN
em vez de bin
para evitar os problemas descritos na resposta. Aqui está o Makefile funcionando:
# Additional linker flags.
EXTRA_LDFLAGS :=
ifeq ($(PLATFORM),linux)
EXTRA_LDFLAGS := -Wl,-rpath,'$$ORIGIN'
endif
ifeq ($(suffix $(TARGET)),.dll)
EXTRA_LDFLAGS := -Wl,--out-implib,$(dir $(TARGET))lib$(basename $(notdir $(TARGET))).dll.a
endif
# Build target.
$(TARGET): $(OBJ_FILES)
$(CC) -o $@ $^ $(LDFLAGS) $(EXTRA_LDFLAGS)