3

I have a program that requires atol() in one of its functions. So I included stdlib.h but it doesn't seem to see it.

EDIT: I'm aware to use it, I should include stdlib.h. I did that but I'm still getting this error. The function:

void
intstr( char *t )
{
    long int atol( char * );
    long x;
    int i;
    x=atol(t);

    for(i=0;i<nilit;i++){
        if(x == ilit[i]){
        lsymb =symbol[nsymb++] = 250+i;
        return;}
    }
    if( 50 <= nilit){ 
        puts("** too many int literals**");
        exit(1);
    }
    ilit[nilit++] = x;
    lsymb = symbol[nsymb++] = 249 + nilit;
}

The error I get when I try to build

 error LNK2019: unresolved external symbol "long __cdecl atol(char *)" (?atol@@YAJPAD@Z) referenced in function "void __cdecl intstr(char *)" (?intstr@@YAXPAD@Z)
C:x\X\X\X\Debug\p8program.exe : fatal error LNK1120: 1 unresolved externals
  • Don't declare system or standard functions yourself, use the proper header files. Read [this reference of `atol`](http://en.cppreference.com/w/cpp/string/byte/atoi) and pay close attention to the argument type. – Some programmer dude Jun 10 '15 at 17:13
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – AndyG Jun 10 '15 at 17:22

1 Answers1

2

You have this code:

void
intstr( char *t )
{
    long int atol( char * );

What's the point of this atol() wrong declaration?
To use atol() in your code, just #include <stdlib.h>.

Note that the prototype of atol() is:

long atol( const char *str );

(The input parameter is a const pointer.)

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • I think the problem is that you declared by mistake a "new" non-standard atol overload, with a non-const char pointer, and the linker is trying to find its definition, and it's not finding it, since the correct standard implemented atol takes a const char*. – Mr.C64 Jun 10 '15 at 17:26