tags:

views:

7743

answers:

6

I have a library I created,

mylib.c:

#include <mylib.h>
int
testlib() {
    printf("Hello world\n");
    return (0);
}

mylib.h:

#include <stdio.h>
extern int testlib();

In my program, I've attempted to call this library function:

myprogram.c:

#include <mylib.h>

int
main (int argc, char *argv[]) {
    testlib();
    return (0);
}

When I attempt to compile this program I get the following error:

In file included from myprogram.c:1 mylib.h:2 warning: function declaration isn't a prototype

I'm using: gcc (GCC) 3.4.5 20051201 (Red Hat 3.4.5-2)

My question is, what is the proper way to declare a function prototype?

+2  A: 
extern int testlib(void);
Paul Tomblin
+7  A: 

Try:

extern int testlib(void);
Lasse V. Karlsen
A: 

The other suggestions might work, but I just copied your code and compiled it with gcc Red Hat 3.4.6-9 and it compiled with no errors.

Craig H
A: 

Remove extern from the declaration in mylib.h

Especially if you are writing a pure C program, the extern declaration is unnecessary there.

Ryan Ahearn
+15  A: 

In C foo() and foo(void) are different functions. foo() accepts an infinite number of arguments, while foo(void) accepts 0 arguments. In C++ they mean the same thing. I suggest that you use void consistently when you mean no arguments.

If you have a variable a, "extern int a;" is a way to tell the compiler that 'a' is a symbol that might be present in a different translation unit (c compiler speak for source file), don't resolve it until link time. On the other hand, symbols which are function names are anyway resolved at link time. The meaning of a storage class specifier on a function (extern, static) only affects its visibility and extern is the default, so extern is actually unnecessary.

I suggest removing the extern, it is extraneous and is usually omitted.

Pramod
A: 

Remove extern from the declaration in mylib.h

kaliyaperumal K
Removing `extern` doesn't make it a prototype.
pmg