2

I wrote this code:

#include <stdio.h>

#define fctrl(N) ( (N==1)? (N) : (N*fctrl(N-1)) )

int main()
{
    int m,n=7;
    m = fctrl(n);
    printf("fctrl is %d\n",m);
    return 0;
}

While compiling, I got the following error:

/tmp/ccRODXnZ.o: In function `main':
/home//Desktop/fctrl/fctrl.c:8: undefined reference to `fctrl'
collect2: error: ld returned 1 exit status

Why was this error displayed and how can I fix it?

Jens
  • 69,818
  • 15
  • 125
  • 179
Tree
  • 145
  • 1
  • 13
  • 3
    Makros do Not expand recuesively. – Stephan Lechner Feb 26 '17 at 17:13
  • Thank you very much. @Stephan Lechner – Tree Feb 26 '17 at 17:15
  • 2
    @Lazcano "Macros are just like any other tool - a hammer used in a murder is not evil because it's a hammer. It is evil in the way the person uses it in that way. If you want to hammer in nails, a hammer is a perfect tool.". Use them right and you will be fine! ;) – gsamaras Feb 26 '17 at 17:20
  • http://googleweblight.com/i?u=http://nealabq.com/blog/2008/08/14/the-c-preprocessor-recursion/&grqid=mV7Ox5fU&hl=en-IN&geid=1005 – msc Feb 26 '17 at 17:50
  • @StephanLechner Yes they are expanded recursively (otherwise you couldn't use macros in macros). The problem is more subtle: if a macro identifier was expanded once, it will not be expanded in further expansions. The details are in the C Standard :-) – Jens Feb 26 '17 at 21:26
  • Thanks a lot! @rsp – Tree Feb 27 '17 at 06:46

2 Answers2

2

You are trying to define a macro that emulates a recursive function, but macros do not support recursiveness, thus the error.

I would use a function instead, but you always read more in C preprocessor, recursive macros.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

Macros are not processed by the "actual" C compiler. Macros are processed by c-preprocessor which is merely a text replacement mechanism thus your function-like expression defined as a macro and intended to work as a function does not result with normal function behavior.

After preprocessing takes place, your program will take the following structure,

#include <stdio.h>

int main()
{
    int m,n=7;
    m = ( (n==1)? (n) : (n*fctrl(n-1)) );
    printf("fctrl is %d\n",m);
    return 0;
}

The question you should ask yourself is where is fctrl function defined? The answer is obvious, it is not defined and that's main reason why you are getting undefined reference to 'fctrl' error.

Use cpp -nostdinc {filename} or gcc -E -nostdinc {filename} commands on terminal and you will figure out what's going on. By the way, -nostdinc is used to supress inclusion of system header files. cpp is the program to invoke C/C++ preprocessor despite the fact that C/C++ preprocessor can be used for purposes more than mere C/C++ text replacement mechanism. gcc -E option gives the same results with cpp command. In the early days of Unix, preprocessor and compilers used to be separate programs. They are bundled together in today's operating systems.

nmd_07
  • 666
  • 1
  • 9
  • 25