Thread: main.c: In function 'main': main.c:12:28: warning: implicit declaration of function '

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    96

    main.c: In function 'main': main.c:12:28: warning: implicit declaration of function '

    First, I have created two header files: add.h and subtract.h
    Next, created two source files: add.c and subtract.c.

    Finally, created main program in main.c:

    gcc compiler

    Code:
    //add.h
    
    #ifndef ADD_H
    #define ADD_H
    
    int add(int x, int y);
    
    #endif
    
    //subtract.h
    
    #ifndef SUBTRACT_H
    #define SUBTRACT_H
    
    int subtract(int x, int y);
    
    #endif
    
    
    //add.c
    #include "add.h"
    
    int add(int x, int y) {
        return x + y;
    }
    
    //subtract.c
    #include "subtract.h"
    
    int subtract(int x, int y) {
        return x - y;
    }
    
    //main.c 
    
    #include <stdio.h>
    #include "add.h"
    #include "subtract.h"
    
    int main() {
        int x = 10, y = 5;
        
        printf("x + y = %d\n", add(x, y));
        printf("x - y = %d\n", subtract(x, y));
        
        return 0;
    }
    Here's the compile command:
    gcc -o main add.c subtract.c main.c
    Code:
    main.c: In function 'main':
    main.c:12:28: warning: implicit declaration of function 'subtract' [-Wimplicit-function-declaration]
         printf("x - y = %d\n", subtract(x, y));
                                ^~~~~~~~
    C:\Users\33\AppData\Local\Temp\ccfJeU2u.o:subtract.c:(.text+0x0): multiple definition of `add'
    C:\Users\33\AppData\Local\Temp\ccGHzJAD.o:add.c:(.text+0x0): first defined here
    C:\Users\33\AppData\Local\Temp\ccqKrrPu.o:main.c:(.text+0x0): multiple definition of `add'
    C:\Users\33\AppData\Local\Temp\ccGHzJAD.o:add.c:(.text+0x0): first defined here
    collect2.exe: error: ld returned 1 exit status
    Last edited by Kittu20; 03-15-2023 at 09:31 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    Works for me.
    Code:
    $ for i in * ; do echo $i ; echo ========== ; cat $i ; done
    add.c
    ==========
    #include "add.h"
     
    int add(int x, int y) {
        return x + y;
    }
    add.h
    ==========
    #ifndef ADD_H
    #define ADD_H
     
    int add(int x, int y);
     
    #endif
    main.c
    ==========
    #include <stdio.h>
    #include "add.h"
    #include "subtract.h"
     
    int main() {
        int x = 10, y = 5;
         
        printf("x + y = %d\n", add(x, y));
        printf("x - y = %d\n", subtract(x, y));
         
        return 0;
    }
    subtract.c
    ==========
    #include "subtract.h"
     
    int subtract(int x, int y) {
        return x - y;
    }
    subtract.h
    ==========
    #ifndef SUBTRACT_H
    #define SUBTRACT_H
     
    int subtract(int x, int y);
     
    #endif
    $ gcc -o main add.c subtract.c main.c
    $ ./main 
    x + y = 15
    x - y = 5
    > implicit declaration of function 'subtract' [-Wimplicit-function-declaration]
    You typically see this when you miss including a header file, or you have a spelling mistake somewhere.

    > C:\Users\33\AppData\Local\Temp\ccfJeU2u.o:subtract .c:(.text+0x0): multiple definition of `add'
    You get multiple definition errors by having multiple copies of the same thing.
    One way to get this is by doing #include on the source code, not the header file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    96
    Quote Originally Posted by Salem View Post
    One way to get this is by doing #include on the source code, not the header file.
    Now it is working for me also. maybe i did something wrong.

    I don't understand if there are many source files in a project, then where should the header files be included?

    As an example I have three source files and two header files if I include both the header file in main source file the program gives correct output.


    If I include the header files with their related source file. program give the warning i but gives the output

    Last edited by Kittu20; 03-16-2023 at 08:18 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    Take for example subtract.h

    You include subtract.h in subtract.c, so that the compiler can check that the prototype in the .h file is consistent with the definition in the .c file.
    If you edited one without changing the other, you stand a chance of getting an error message.

    Everywhere you want to call subtract would need to include the subtract.h header file, so that the compiler knows how to call it.

    If a source file doesn't need to call subtract, then there is no need for it to include the subtract.h file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-25-2020, 06:09 AM
  2. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  3. Replies: 2
    Last Post: 06-04-2013, 08:25 AM
  4. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  5. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 01:31 PM

Tags for this Thread