6

Is there a FLAG setting in makefile to detect duplicate function declarations?

Duplicate function declarations in a header file are found, but compiler doesn't report it even FLAG is set as "warning as error".

Does this bring any implicit problem?

5
  • If you have two declarations that mismatch in the same translation unit you should get a warning, but if they are in different translation units then there's really nothing the compiler (or the linker) can do. For that you need some kind of static analyzer that can handle multiple translation units. Commented Mar 11, 2016 at 15:48
  • 3
    If you want to get warnings for multiple declarations even though they are the same and there's no conflict, then may I ask why? Commented Mar 11, 2016 at 15:51
  • 1
    There's nothing wrong with duplicated declarations. Only mismatched declarations and duplicate definitions. Commented Mar 11, 2016 at 15:52
  • If there is not a problem with duplicate declarations, then the other reason to get rid of it is to keep the code clean. I will check my static code analysis tool. Commented Mar 11, 2016 at 16:11
  • 1
    Every function definition is also a declaration. Since one often wants or needs function prototypes as well, multiple declarations are routine. Commented Mar 11, 2016 at 16:16

3 Answers 3

3

You're trying to solve a problem that doesn't exist. There is generally no problem with duplicated function declarations, so there is no reason for a compiler to diagnose them.

A C compiler will typically diagnose situations, in the same compilation unit, where a function is declared in more than one way (e.g. two declarations of the same function with different return type or argument types).

Duplicate function definitions (a particular type of function declaration - which implements the function) are a problem. Practically, a compiler will give warnings or errors when multiple definitions of a function occur in a compilation unit. A linker will also report cases where a function is defined in more than one compilation unit - the exceptions being functions that are either inline or static (local to its compilation unit).

Sign up to request clarification or add additional context in comments.

1 Comment

@jinawee - Not really. Duplicate declarations are simply an equivalent restatement of an interface specification, and that has no effect on code which depends on that interface. In a language like C or C++ based on a separate compilation model, duplication of declarations between compilation units is normal and encouraged practice (e.g. by encouraging use of header files being included by both). Duplicate declarations within a compilation unit has no effect (unless they are definitions, but that's a diagnosable error)- removing one doesn't affect program behaviour at all.
0

Duplicate function declarations are NOT a problem so why compiler need to report about it as a problem?

This scenario is really common:

source 1:

//definition int func(void) { ... } 

source 2:

//declaration extern int func(void); 

source 3:

extern int func(void); //duplicate with source 2 

1 Comment

In your scenario, it is normal to have multiple declarations; duplicate declaration in one header file is redundant if it is not confusing (that is why I post this question). From comments of other people, repeated declaration doesn't pose a problem.
0

I don't like using extern, especially for functions. Declare your function in a single header file and then include that header file in the other compilation units. If the function is only used by a single compilation unit, declare it as static with the .c file itself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.