未定义的引用:带有宏

时间:2015-05-23 13:17:06

标签: c generics vector

我在那里尝试了一些东西,但我是一个菜鸟,所以如果有人能解释为什么我有多重定义错误?为什么顺便说一下我有未申报的功能呢?

谢谢!

修改

我尝试减少代码,但我认为我无法进一步减少代码,因此它看起来与此编辑之前的版本非常相似。遗憾。

编辑2:

我尝试了一些东西(单独的struct和向量的函数声明)但我还有最后一个错误:s

vector.h:

#ifndef VECTOR_H_
# define VECTOR_H_

# include   <stdlib.h>

# define    DEFAULT_VECTOR_SIZE 2

typedef unsigned char   t_byte;
typedef struct s_vector t_vector;

struct      s_vector
{
  size_t    stype;
  size_t    size;
  size_t    capacity;
  void      *data;
};

t_vector    *new_vector(size_t);
void        delete_vector(t_vector *);

# define VECTOR_GENERATE_STRUCT_TYPE(TYPE) VECTOR_GENERATE_NAME(TYPE, TYPE)
# define VECTOR_GENERATE_STRUCT_NAME(TYPE, NAME)                     \
  typedef struct    s_vector_##NAME{                                 \
    size_t      size;                                                \
    size_t      capacity;                                            \
    TYPE        *data;                                               \
  }         t_vector_##NAME;                                         \
                                                                     \
  t_vector_##NAME   *vector_##NAME_new();                            \
  void          vector_##NAME_delete(t_vector_##NAME *);             \
  int           vector_##NAME_init(t_vector_##NAME *this);           \


# define VECTOR_GENERATE_FUNC_TYPE(TYPE)    VECTOR_GENERATE_FUNC_NAME(TYPE, NAME)
# define VECTOR_GENERATE_FUNC_NAME(TYPE, NAME)                      \
  t_vector_##NAME   *vector_##NAME_new()                            \
  {                                                                 \
    return ((t_vector_##NAME *)new_vector(sizeof(TYPE)));           \
  }                                                                 \
                                                                    \
  void          vector_##NAME_delete(t_vector_##NAME *this)         \
  {                                                                 \
    delete_vector((t_vector *)this);                                \
  }                                                                 \
    int         vector_##NAME_init(t_vector_##NAME *this)           \
  {                                                                 \
    return (vector_init((t_vector *)this, sizeof(TYPE)));           \
  }                                                                 \

VECTOR_GENERATE_STRUCT_NAME(t_byte, byte)
#endif

vector.c:

#include    "vector.h"

VECTOR_GENERATE_FUNC_NAME(t_byte, byte)
int     vector_init(t_vector *vector)
{
  vector->size = 0;
  vector->capacity = DEFAULT_VECTOR_SIZE;
  if ((vector->data = malloc(DEFAULT_VECTOR_SIZE)) == NULL)
    return (-1);
  umemset(vector->data, 0, DEFAULT_VECTOR_SIZE);
  return (0);
}

t_vector    *new_vector(size_t stype)
{
  t_vector  *vector;

  vector = (t_vector *)malloc(sizeof(t_vector));
  if (vector != NULL)
    {
      vector->size = 0;
      vector->capacity = DEFAULT_VECTOR_SIZE;
      if ((vector->data = malloc(DEFAULT_VECTOR_SIZE)) == NULL)
    {
      free(vector);
      return (NULL);
    }
      umemset(vector->data, 0, DEFAULT_VECTOR_SIZE);
    }
  return (vector);
}

void        delete_vector(t_vector *this)
{
  if (this->data != NULL)
    free(this->data);
  free(this);
}

int     vector_extend(t_vector *this,
                  size_t type)
{
  void      *new;
  unsigned int  i;

  i = 0;
  this->capacity = this->size * type;
  --this->capacity;
  while (i < type)
    {
      this->capacity |= this->capacity << i;
      i *= 2;
    }
  ++this->capacity;
  new = malloc(this->capacity * 2);
  if (new == NULL)
    return (-1);
  this->capacity *= 2;
  umemcpy(this->data, new, this->size * type);
  return (0);
}

void        vector_clear(t_vector *this)
{
  free(this->data);
  this->size = 0;
  this->capacity = 0;
  this->data = NULL;
}

buffer.h:

#ifndef BUFFER_H_
# define BUFFER_H_

# include   <stdlib.h>
# include   <unistd.h>
# include   "vector.h"

typedef unsigned char   t_byte;

# define    IN  0
# define    OUT 1

VECTOR_GENERATE_NAME(t_byte, byte)

typedef struct s_buffer t_buffer;

struct      s_buffer
{
  int       fd[2];
  unsigned int  cursor;
  t_vector_byte *contents;
};

t_buffer    *new_buffer(int, int);
void        delete_buffer(t_buffer *);
#endif

buffer_new.c:

#include    "buff.h"

t_buffer    *new_buffer(int in,
                int out)
{
  t_buffer  *buffer;

  buffer = (t_buffer *)malloc(sizeof(t_buffer));
  if (buffer != NULL)
    {
      buffer->fd[IN] = in;
      buffer->fd[OUT] = out;
      buffer->cursor = 0;
      vector_byte_init(buffer->contents);
    }
  return (buffer);
}

buffer_delete.c:

#include    "buff.h"

void        delete_buffer(t_buffer *buffer)
{
  if (buffer != NULL)
    {
      free(buffer);
    }
}

关于错误:

 vector.c: In function ‘vector_init’:
vector.c:11:3: warning: implicit declaration of function ‘umemset’ [-Wimplicit-function-declaration]
   umemset(vector->data, 0, DEFAULT_VECTOR_SIZE);
   ^
vector.c: In function ‘vector_extend’:
vector.c:60:3: warning: implicit declaration of function ‘umemcpy’ [-Wimplicit-function-declaration]
   umemcpy(this->data, new, this->size * type);
   ^
buff_new.c: In function ‘new_buffer’:
buff_new.c:14:7: warning: implicit declaration of function ‘vector_byte_init’ [-Wimplicit-function-declaration]
       vector_byte_init(buffer->contents);
       ^
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccbJdFjO.o: In function `vector_init':
vector.c:(.text+0xac): undefined reference to `umemset'
/tmp/ccbJdFjO.o: In function `new_vector':
vector.c:(.text+0x13c): undefined reference to `umemset'
/tmp/ccbJdFjO.o: In function `vector_extend':
vector.c:(.text+0x269): undefined reference to `umemcpy'
/tmp/ccdymmO4.o: In function `new_buffer':
buff_new.c:(.text+0x52): undefined reference to `vector_byte_init'
collect2: error: ld returned 1 exit status

buffer.h是唯一包含vector.h的文件,但它包含在其他一些文件中。

我认为这就是全部;我用GCC 4.9编译。 所以,如果有人对我有任何线索,那就太酷了!

1 个答案:

答案 0 :(得分:0)

您在头文件中确实有对象 definitions 。这包含在每个实现文件(.c)中,就像粘贴了相同的文本一样。这导致每个实现文件定义创建的对象=&gt;多个定义。

只需在标题中包含对象声明(每个标题前面都有extern限定符来表示声明 -only)并将定义放入单个实现文件中(很可能与头文件的基本名称相同。就像你的项目一样。

修改

根据错误报告中的名称,问题似乎是由于未发布的buffer.h,但是因为它包含了vector.h,这将导致我上面描述的内容。您应该仔细阅读错误消息并尝试理解它们。

编辑2:

您应该检查宏是否确实扩展了您期望的方式。对于这样的大型宏,最好从小版本开始,逐步详细说明。这样的结构倾向于创造出与预期不同的东西,并且可能会被忽视。