Błąd kompilacji undefined reference to `print_hex'

0

Witam!
Zaczynam swoją przygodę z C, używam ubuntu, za pomocą terminala chcę skompilowac program.
Wpisuję polecenie gcc -o nfc-emulate-forum-tag2 nfc-emulate-forum-tag2.c -lnfc

I otrzymuję takie błędy

/tmp/ccYg8DcW.o: In function `nfcforum_tag2_io':
nfc-emulate-forum-tag2.c:(.text+0x91): undefined reference to `print_hex'
nfc-emulate-forum-tag2.c:(.text+0x178): undefined reference to `print_hex'
collect2: error: ld returned 1 exit status 

Czym to może być spowodowane ?

Poniżej załączam kod źródłowy:

nfc-emulate-forum-tag2.c

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif // HAVE_CONFIG_H

#include <errno.h>
#include <signal.h>
#include <stdlib.h>

#include <nfc/nfc.h>
#include <nfc/nfc-emulation.h>

#include "utils/nfc-utils.h"

static nfc_device *pnd;
static nfc_context *context;

static void
stop_emulation(int sig)
{
  (void)sig;
  if (pnd != NULL) {
    nfc_abort_command(pnd);
  } else {
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
}

static uint8_t __nfcforum_tag2_memory_area[] = {
  0x00, 0x00, 0x00, 0x00,  // Block 0
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xFF, 0xFF,  // Block 2 (Static lock bytes: CC area and data area are read-only locked)
  0xE1, 0x10, 0x06, 0x0F,  // Block 3 (CC - NFC-Forum Tag Type 2 version 1.0, Data area (from block 4 to the end) is 48 bytes, Read-only mode)

  0x03, 33,   0xd1, 0x02,  // Block 4 (NDEF)
  0x1c, 0x53, 0x70, 0x91,
  0x01, 0x09, 0x54, 0x02,
  0x65, 0x6e, 0x4c, 0x69,

  0x62, 0x6e, 0x66, 0x63,
  0x51, 0x01, 0x0b, 0x55,
  0x03, 0x6c, 0x69, 0x62,
  0x6e, 0x66, 0x63, 0x2e,

  0x6f, 0x72, 0x67, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
};

#define READ 		0x30
#define WRITE 		0xA2
#define SECTOR_SELECT 	0xC2

#define HALT 		0x50
static int
nfcforum_tag2_io(struct nfc_emulator *emulator, const uint8_t *data_in, const size_t data_in_len, uint8_t *data_out, const size_t data_out_len)
{
  int res = 0;

  uint8_t *nfcforum_tag2_memory_area = (uint8_t *)(emulator->user_data);

  printf("    In: ");
  print_hex(data_in, data_in_len);

  switch (data_in[0]) {
    case READ:
      if (data_out_len >= 16) {
        memcpy(data_out, nfcforum_tag2_memory_area + (data_in[1] * 4), 16);
        res = 16;
      } else {
        res = -ENOSPC;
      }
      break;
    case HALT:
      printf("HALT sent\n");
      res = -ECONNABORTED;
      break;
    default:
      printf("Unknown command: 0x%02x\n", data_in[0]);
      res = -ENOTSUP;
  }

  if (res < 0) {
    ERR("%s (%d)", strerror(-res), -res);
  } else {
    printf("    Out: ");
    print_hex(data_out, res);
  }

  return res;
}

int
main(int argc, char *argv[])
{
  (void)argc;
  (void)argv;

  nfc_target nt = {
    .nm = {
      .nmt = NMT_ISO14443A,
      .nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init()
    },
    .nti = {
      .nai = {
        .abtAtqa = { 0x00, 0x04 },
        .abtUid = { 0x08, 0x00, 0xb0, 0x0b },
        .szUidLen = 4,
        .btSak = 0x00,
        .szAtsLen = 0,
      },
    }
  };

  struct nfc_emulation_state_machine state_machine = {
    .io = nfcforum_tag2_io
  };

  struct nfc_emulator emulator = {
    .target = &nt,
    .state_machine = &state_machine,
    .user_data = __nfcforum_tag2_memory_area,
  };

  signal(SIGINT, stop_emulation);

  nfc_init(&context);
  if (context == NULL) {
    ERR("Unable to init libnfc (malloc)");
    exit(EXIT_FAILURE);
  }
  pnd = nfc_open(context, NULL);

  if (pnd == NULL) {
    ERR("Unable to open NFC device");
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
  printf("Emulating NDEF tag now, please touch it with a second NFC device\n");

  if (nfc_emulate_target(pnd, &emulator, 0) < 0) {
    nfc_perror(pnd, argv[0]);
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

  nfc_close(pnd);
  nfc_exit(context);
  exit(EXIT_SUCCESS);
}
 

Oraz plik nagłówkowy utils/nfc-utils.h który moim zdaniem może mieć coś wspólnego z tym błędem.

 
#ifndef _EXAMPLES_NFC_UTILS_H_
#  define _EXAMPLES_NFC_UTILS_H_

#  include <stdlib.h>
#  include <string.h>
#  include <err.h>

/**
 * @macro DBG
 * @brief Print a message of standard output only in DEBUG mode
 */
#ifdef DEBUG
#  define DBG(...) do { \
    warnx ("DBG %s:%d", __FILE__, __LINE__); \
    warnx ("    " __VA_ARGS__ ); \
  } while (0)
#else
#  define DBG(...) {}
#endif

/**
 * @macro WARN
 * @brief Print a warn message
 */
#ifdef DEBUG
#  define WARN(...) do { \
    warnx ("WARNING %s:%d", __FILE__, __LINE__); \
    warnx ("    " __VA_ARGS__ ); \
  } while (0)
#else
#  define WARN(...) warnx ("WARNING: " __VA_ARGS__ )
#endif

/**
 * @macro ERR
 * @brief Print a error message
 */
#ifdef DEBUG
#  define ERR(...) do { \
    warnx ("ERROR %s:%d", __FILE__, __LINE__); \
    warnx ("    " __VA_ARGS__ ); \
  } while (0)
#else
#  define ERR(...)  warnx ("ERROR: " __VA_ARGS__ )
#endif

#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif

uint8_t  oddparity(const uint8_t bt);
void    oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar);

void    print_hex(const uint8_t *pbtData, const size_t szLen);
void    print_hex_bits(const uint8_t *pbtData, const size_t szBits);
void    print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar);

void    print_nfc_target(const nfc_target *pnt, bool verbose);

#endif

Ta kompilacja to próba wykorzystania jednego z przykładów projektu libnfc.
Libnfc instalowałem zgodnie z instukcją, instalacja przebiegła poprawnie.
Próbowałem przeprowadzić kompilację na systemie Fedora 24 gdzie Libnfc jest domyślnie zainstalowane, również bezskutecznie.

2

Jeśli mnie wzrok nie myli to masz deklarację funkcji print_hex (patrz dół pliku nagłówkowego) ale nigdzie nie ma jej zdefiniowanej.
Błąd undefined reference zazwyczaj oznacza taką sytuację.
Wygląda jakby czegoś brakowało, jakiegoś pliku albo coś, masz 6 deklaracji funkcji a żadnej definicji.

2

Jakby brakowało pliku "nfc-utils.c" na moje oko.

0
Khuzy napisał(a):

Jakby brakowało pliku "nfc-utils.c" na moje oko.

nfc-utils.c nie jest nigdzie zadeklarowny

zadeklarowny jest nfc-utils.h, ale nfc-utils.c też znajduje się w tym samym katalogu

czy powinienem też zadeklarować nfc-utils.c, jeśli tak to w którym miejscu?

2

No to przecież ewidentnie nie ma reguł kompilacji dla nfc-utils.c.
Zrób sobie make i kompiluj.

myprogam: nfc-emulate-forum-tag2.o nfc-utils.o
    gcc -o myprogram nfc-emulate-forum-tag2.o nfc-utils.o

nfc-emulate-forum-tag2.o: nfc-emulate-forum-tag2.c 
    gcc -c nfc-emulate-forum-tag2.c

nfc-utils.o: nfc-utils.c nfc-utils.h
    gcc -c nfc-utils.c
0

Udało mi się przeprowadzić poprawną kompilację, w terminalu wpisałem:
gcc -o nfc-emulate-forum-tag2 utils/nfc-utils.c nfc-emulate-forum-tag2.c -lnfc

Dzięki za pomoc, cenne wskazówki :)
Temat do zamknięcia.

1 użytkowników online, w tym zalogowanych: 0, gości: 1