简体   繁体   中英

Using Custom header file in two .c files

Had a customer header file contains some simple functions and using those functions in two different .c files

sampleheader.h

    #ifndef SAMPLEHEADER_H
    #define SAMPLEHEADER_H
    # include <stdio.h>
    
    add(int a, int b)
    {
    printf("Added value = %d\n", a + b);
    }
    multiply(int a, int b)
    {
    printf("Multiplied value = %d\n", a * b);
    }
    #endif

file1.c

    # include <stdio.h>
    # include "sampleheader.h"
    int file 2();
    int main (){
    add (a, b);
    if some condition matches call -- file2 (a,b);
    return 0;
    }

file2.c


    # include <stdio.h>
    int file2 (){
    multiply (a, b);
    return 0;
    }

I'm running below commands to compile the code:

    gcc  -c -o file1.o file1.c
    gcc  -c -o file2.o file2.c
    gcc  -o target file1.o file2.o

getting below warning

warning: implicit declaration of function 'multiply' [-Wimplicit-function-declaration]

Please help me to avoid "[-Wimplicit-function-declaration]" warning.

Also Tried include "sampleheader.h" in file2.c, then it is throwing an error with " multiple definition of `add'; /tmp/ccTOAq1U.o:file2.c:(.text+0x0): first defined here'"

sorry if this is repetitive question.

This:

warning: implicit declaration of function 'multiply' [-Wimplicit-function-declaration]

occurs because a call to function multiply() appears in file2.c , but that file neither provides its own declaration of that function nor #include s your header, where the declaration (and definition) of multiply() appears.

The error

multiple definition of `add'; /tmp/ccTOAq1U.o:file2.c:(.text+0x0): first defined here'

that you receive when you do include the header occurs because the header contains not just function declarations, but function definitions (implementations). There must not be more then one definition of any given external function anywhere in an entire program, and including the header into multiple source files causes there to be multiple instances of the function definitions within.

Generally speaking, C headers should not include function or object definitions, and definitely not external function or object definitions. These go into a regular source file, and only their declarations go into a header. For example,

sampleheader.h

#ifndef SAMPLEHEADER_H
#define SAMPLEHEADER_H

void add(int a, int b);
void multiply(int a, int b);

#endif

sample.c

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

void add(int a, int b) {
    printf("Added value = %d\n", a + b);
}

void multiply(int a, int b) {
    printf("Multiplied value = %d\n", a * b);
}

You would include that version of the header in every source file that wants to call one of the functions declared within, and you would compile the associated source and include it in your link. Note also that you don't need to put the the function definitions in their own file, nor even in the same file as each other. The just need to be in exactly one source file that gets compiled and linked.

Note also that in C99 and later, you must declare the return types of your functions, as shown. If the function does not return a value then it should declare void as its return type.

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