Question

I'm trying to use the BFD library, and so I've installed package binutils-dev and have included:

#include <bfd.h>

and am calling bfd_openr and bfd_close and so on from my code.

Recently I have upgraded packages and now I get an error from here:

bfd.h:

/* PR 14072: Ensure that config.h is included first.  */
#if !defined PACKAGE && !defined PACKAGE_VERSION
#error config.h must be included before this header
#endif

...that I should include config.h - but I am not using autoconf.

Am I including the wrong header file? How are you supposed to use binutils-dev?

Here is a demo program:

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

int main()
{
    bfd_init();

    bfd* file = bfd_openr("a.out", 0);

    if (!file)
        return -1;

    if (bfd_check_format(file, bfd_object))
        printf("object file\n");
    else
        printf("not object file\n");

    bfd_close(file);

    return 0;
}

try to compile and run as follows:

$ sudo apt-get install binutils-dev
$ gcc test.c
In file included from test.c:3:0:
/usr/include/bfd.h:37:2: error: #error config.h must be included before this header
Était-ce utile?

La solution

Well, the most correct way of using the header is to use autotools in your package as well. Some people are simply stubborn and I don't think you can do much about it.

An alternative is to work-around the check, by defining the macros it is using:

#define PACKAGE 1
#define PACKAGE_VERSION 1

Of course, if you are already defining those, you may as well set them to some reasonable values like:

#define PACKAGE "your-program-name"
#define PACKAGE_VERSION "1.2.3"

and use them for your program. You'll usually going to use something like that anyway at some point to keep the versions consistent.

This should be enough if you're using a standards-compliant compiler because then the __STDC__ macro will be declared and everything will go on just fine. Well, as long as the headers you're using won't require more autoconf-generated defines.

For example, if you wanted to use plugin-api.h, you'd actually have to handle checking for stdint.h and inttypes.h...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top