Unable to link against BFD [duplicate]

2019-08-14 14:40发布

问题:

This question already has an answer here:

  • Trying to include a library, but keep getting 'undefined reference to' messages 3 answers

I am trying to include this file in my project: http://cairo.sourcearchive.com/documentation/1.9.4/backtrace-symbols_8c-source.html

But it requires linking against BFD. I have both binutils and binutils-devel installed. I have tried linking with -lbfd as well as directly to /usr/lib64/libbfd.so and /usr/lib64/libbfd.a (which both do exist). I also tried searching pkg-config to see if there was a different flag I should be using, but there is no reference to BFD or binutils in pkg-config.

No matter what I've tried I get the following errors:

undefined reference to 'bfd_init'
undefined reference to 'bfd_openr'
undefined reference to 'bfd_check_format'
undefined reference to 'bfd_checkformat_matches'
undefined reference to 'bfd_close'
undefined reference to 'bfd_map_over_sections'

First I am compiling my logger and the backtrace-symbols.c file I linked to above (as the logger is where I intend to use this for printing traces). Then I am linking those two object files together into a combined object file:

CC = clang
CFLAGS = -g -Wall -c
SOURCE = simplog.c
OBJ = simplog.o, simplog-temp.o, backtrace-symbols.o

all:
    $(CC) $(CFLAGS) $(SOURCE); mv simplog.o simplog-temp.o; \
    $(CC) -ansi $(CFLAGS) backtrace-symbols.c; \
    ld -r simplog-temp.o backtrace-symbols.o -o simplog.o

clean:
    rm -f $(OBJ) 

Then I am linking this object file into my main project:

CC= clang++
PROG= ./bin/chiplus8
OBJS= ./src/main.o ./src/Chip8.o ./src/EmuCPU.o ./src/SimpleLogger/simplog.o
LIBS= 
CXXFLAGS= -g -Wall -std=c++11 $(shell pkg-config --cflags ${LIBS})
LDFLAGS= $(shell pkg-config --static --libs ${LIBS})

all: logger $(PROG)

$(PROG): $(OBJS)
    mkdir -p ./bin/
    $(CC) -g -rdynamic -o $(PROG) $(LDFLAGS) -lbfd -liberty $(OBJS)
    rm -f $(OBJS)

logger:
    cd ./src/SimpleLogger; make clean all

clean:
    rm -f $(PROG) $(OBJS)

I'm really not sure what I need to do to get this to link properly anymore. Is there something I'm missing?

回答1:

Change:

$(PROG): $(OBJS)
    mkdir -p ./bin/
    $(CC) -g -rdynamic -o $(PROG) $(LDFLAGS) -lbfd -liberty $(OBJS)
    rm -f $(OBJS)

To this:

$(PROG): $(OBJS)
    mkdir -p ./bin/
    $(CC) -g -rdynamic -o $(PROG) $(LDFLAGS) $(OBJS) -lbfd -liberty
    rm -f $(OBJS)

Object files have to be placed before libraries with the compilation command. See this question.