简体   繁体   中英

Error in C program when using a C file in #Include directive (Multiple definition error)

Scenario :

AC application created in Netbeans IDE with below two files:

some_function.c

#include <stdio.h>
int function_1(int a, int b){
    printf("Entered Value is = %d & %d\n",a,b);
    return 0;
}

newmain.c

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    //function_2(); //Error //function name not allowed
    function_1();
    function_1(1);
    function_1(1,2);
    return (EXIT_SUCCESS);
}

When learning the need of the header file in a C program, I tried the above application (as it is). It got compiled and gave the output as below

Entered Value is = 4200800 & 102
Entered Value is = 1 & 102
Entered Value is = 1 & 2

Question 1 : (I realize, in starting stage, to understand the process of the linker program is tough, hence i ask this question.) Is my assumption correct, that when linking, "the linker will check for the function name and not the arguments" in a condition the header file not used?

Regarding the header file usage, I came across this link and there it said as, we can include the C file itself using the #include . So, I used the below line in the file newmain.c

#include "some_function.c"

As expected it shown the below error

error: too few arguments to function 'function_1()'
error: too few arguments to function 'function_1(1)'

And also, I got the below (unexpected) error:

some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here

Question 2: What error I did when including the 'c' file itself, as it gives the above said (unexpected) error?

You are probably using a pre-C99 dialect of C, which has "implicit function declarations". This means that functions without declarations are taken to have this kind of signature:

int function_1();

ie returning an int and accepting any number of arguments of any type. When you pass an argument list that is incompatible with your function definition, you invoke undefined behaviour at runtime.

Concerning the multiple definition errors, think of it. Each translation unit you include some_function.c will have its own definition of the function. It is as if you had written that definition in every other .c file. C doesn't allow multiple definitions in a program/library.

I (the questioner) post this answer for a quick understanding for some C programming starters. This answer inspired from the answers by @juanchopanza & @Sam Protsenko which are in this post.

Question 1 : Implicit function declarations in C

Question 2: When using the below line

#include "some_function.c"

The result application will change like below after the preprocessor activity
some_function.c

#include <stdio.h>
int function_1(int a, int b){
    printf("Entered Value is = %d & %d\n",a,b);
    return 0;
}

newmain.c

#include <stdio.h>
#include <stdlib.h>

/*"#include "some_function.c"" replace begin by preprocessor*/
    #include <stdio.h>
    int function_1(int a, int b){
        printf("Entered Value is = %d & %d\n",a,b);
        return 0;
    }
/*"#include "some_function.c"" replace end by preprocessor*/

int main(int argc, char** argv) {
    //function_2(); //Error //function name not allowed
    //function_1(); //Error
    //function_1(1); //Error
    function_1(1,2);
    return (EXIT_SUCCESS);
}

Note : In the above two file, the function function_1 is there in two places
So now the below error is meaningful

some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM