0

I am trying to link a library to my c program so that my main program can use functions inside the library I am creating, however I am getting errors.

This is some of my library code (well call this file lib.c):

#include <bfd.h>
#include <stdio.h>

static void doDump ( bfd *abfd )
{
    printf (_("\n%s:     file format %s\n"), bfd_get_filename (abfd), abfd->xvec->name);
    doHeaders ( abfd );
}

This is my main program (well call this file main.c):

#include "bfd.h"

static void getFile ( char *filename, char *target )
{
    bfd *file;
    file = bfd_openr (filename, target);
    doDump (file);
    bfd_close (file);
}

int main (int argc, char **argv)
{
    char *target = NULL;
    bfd_init ();
    getFile ("a.out", target);
}

These are the commands I run to link the libraries:

cc -Wall -c lib.c

ar -cvq libdata.a lib.o

cc -o mainprog main.c lib.a -lbfd

However, I am getting this error:

undefined reference to doDump

Which is pointing to the line:

doDump (file);

What am I doing wrong?

Thanks

Community
  • 1
  • 1
Pat
  • 649
  • 1
  • 8
  • 24

2 Answers2

5

You have defined doDump in lib.c as static. This means it is invisible outside that file. Remove the static keyword and the problem should go away.

lxop
  • 7,596
  • 3
  • 27
  • 42
  • Wow, I totally missed that. Thanks – Pat Feb 12 '13 at 20:46
  • Also, the linker processes files in the order given, if something is defined but only used later, it won't be included (and thus give an error). Always place libraries last. – vonbrand Feb 19 '13 at 17:11
1

Because you have declared doDump to be static it will not be "visible" in your main program. Remove the static qualifier and provide a valid declaration in your main.c (or some other header):

extern void doDump(bfd * file);

static void getFile ( char *filename, char *target )
{
    bfd *file;
    file = bfd_openr (filename, target);
    doDump (file);
    bfd_close (file);
}
user7116
  • 63,008
  • 17
  • 141
  • 172