0

I cannot compile a C file that contain a function-call of a function, which is in another file. The compilation gives an error which says that there is an undefined reference even if I included the relative path to the Header file in the compilated file.

#include <stdio.h>
#include "../libft.h"

void    *ft_memmove(void *dest, const void *src, size_t n)
{
  unsigned char *d;
  unsigned char *s;
  size_t i;

  d = (unsigned char *)dest;
  s = (unsigned char *)src;
  i = 0;
  if (s < d)
    {
      while (n--)
          d[n] = s[n];
    }
  else
      ft_memcpy(d, s, n);
  return (d);
}

int main()
{
  char str[] = "memmove can be very useful.....";
  ft_memmove (str+20,  str+15, 11);
  puts (str);
  return (0);
}

The error that I get : gcc complier error

The header file : the header file

Can you help me please to resolve this problem ?

  • 2
    Please post text instead of picutes of text. – Yunnosch Jan 15 '18 at 18:38
  • this is a linker error, where is the function ft_memcy? Did you write it? Is it in a library? – pm100 Jan 15 '18 at 18:39
  • Please give details on how you build, ideally the gcc commandline. – Yunnosch Jan 15 '18 at 18:40
  • 1
    You'll need to link to the library (`.a`) or object (`.o`) that contains `ft_memcpy`. This is done using `-l`. – ikegami Jan 15 '18 at 18:47
  • the function is in a folder with the relative path : `../mem/ft_memcpy.c` – oanamateiflorin Jan 15 '18 at 18:56
  • the commandline was : gcc ft_memmove.c – oanamateiflorin Jan 15 '18 at 18:58
  • 1
    If the command line is `gcc ft_memmove.c` how is it supposed to find `../mem/ft_memcpy.c`? The header file only *declares* the function (tells it that it exists and what the data types are, etc). It does not tell the compiler where or how it is *defined*. You need `gcc ft_memmove.c ../mem/ft_memcpy.c` if you want to compile/link both in one command. – lurker Jan 15 '18 at 19:30
  • Header files tell the compiler (preprocessor) where to find information about declarations etc (and work with text). The header files do not tell the linker anything about where to find the object code that will define those symbols. You have to tell the linker where to find the object code (as well as the preprocessor where to find the header files). Note that [relative header paths](https://stackoverflow.com/questions/597318/what-are-the-benefits-of-a-relative-path-such-as-include-header-h-for-a-hea) are not really a good idea. – Jonathan Leffler Jan 15 '18 at 20:07

0 Answers0