0

I'm learning C. Here is a problem that I face today. When I try to create object file for main.c I get:

cc -Wall -c main.c
main.c:6:18: warning: implicit declaration of function 'sum' is invalid in C99 [-Wimplicit-function-declaration]

How can I avoid this?

functions.c:

int sum(int x, int y)
{
  return x + y;
}

main.c:

#include <stdio.h>
#include "functions.h"

int main()
{
   printf("%d", sum(1,1));
   return 0;
}

function.h exists but I didn't add it here.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
objc_bd
  • 247
  • 1
  • 4
  • 11

4 Answers4

4

Normally, your functions.h would contain:

extern int sum(int x, int y);

possibly wrapped with header guards, and possibly omitting extern*. You should include the header in the file that defines the function, and also in any file that uses it. Both parts are crucial; they allow the header to ensure that the uses of the function agree with the definition of the function.

C99 requires functions to be declared or defined before they are used, rather like C++.

FWIW: I usually compile with GCC and usually use the options:

gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
    -Wold-style-definition -Wold-style-declaration -Werror \
    ...other arguments as appropriate...

The -Werror means my code compiles without warnings! You'll also see me add static to functions defined in a single-file program to avoid warnings (aka errors) though I don't always bother to comment on why I make that change.


* Normally, headers should include header guards. For a header that neither needs any other headers nor defines any types, it is almost permissible to omit the header guards (I'd put them in anyway, but as long as you're cognizant of the issues, omitting them would be OK too). I prefer to use extern even on functions, though it is not necessary. For variables, it is crucial.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

For the looks, you forgot to type

int sum(int x, int y);

in your functions.h file

alk
  • 69,737
  • 10
  • 105
  • 255
brunch875
  • 859
  • 5
  • 15
0

Make two files:

  1. functions.c: contains definition of the function

  2. functions.h contains the declaration of the function

*Define the 'sum' function in functions.c

*Declare the 'sum' function in functions.h

Kaustav Ray
  • 744
  • 6
  • 20
0

I don't know the use of -Wall. You are not including funcion.c file then how your program will know what is sum(). There are may be more than one way two achieve this but two way I am describing 1) your file name function.h int sum(int x,int y) { return x+y; }

then include that one your main program. 2) file name sum.c and then sum function at the time of compilation you have to compile both file at a time cc sum.c main.c then you will not get any error

asifaftab87
  • 1,315
  • 24
  • 28
  • You can build a single program from multiple separately-compiled object files. In such a case, you have a header which announces the presence of the function (declares the correct prototype for the function) which is included in both the main program (or any other code which uses the function) and in the source that defines the function. It is generally a bad idea to include source files (as opposed to header files) in a compilation. There are reasons for doing so, but they are relatively rare. – Jonathan Leffler Dec 03 '13 at 20:56