1

I am coding in the Cypress PSoC IDE (C99 ARM_GCC). When I use an identically named function in two *.c modules (for example void MyClear()) I get the error:

Build error: multiple definition of `Clear'

MyClear() is not mentioned in any header, and I presumed that it is private to the .C file but I'm obviously wrong.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Brian Frost
  • 13,334
  • 11
  • 80
  • 154
  • have you tried declearing one differentlly? – BRHSM Apr 17 '15 at 08:43
  • One should add that `static` limits the function's visibility to the "translation unit" (roughly source file) it is defined in. That's the only facility to build modular programs which C provides. It should probably be used liberally in order to self document and enforce modular design. More modern languages like Modula or the C descendants provide better language features like modules, classes, namespaces because with larger programs modularization was recognized as a crucial design feature. – Peter - Reinstate Monica Apr 17 '15 at 08:53
  • @PeterSchneider I metioned the same in my [answer](http://stackoverflow.com/a/29694339/2173917). – Sourav Ghosh Apr 17 '15 at 08:54
  • And as a technical note, if you have multiple definitions in object files which are not under your control, you can at least succeed building with the linker option `--allow-multiple-definition`. If you link through gcc you'll have to pass the ld option with the `-Wl,` prefix, i.e. have `-Wl,--allow-multiple-definition` among your LDFLAGS or where ever. Of course that's not recommended; it will use the first definition of the symbol encountered by the linker. If they differ, tough. – Peter - Reinstate Monica Apr 17 '15 at 09:03

5 Answers5

4

Point 1:

In C, functions are global by default.

There is no concept of private in C. FWIW, there is static, however, which limits the scope of the function to the translation unit only . See this previous question and the answers for some clarifiction.

Point 2

You get this error in linking state. When all the translation units have been compiled and about to be linked together, linker can see more than one definition of the function and hence throws the error.

Point to note: You don't define functions in header files. you declare them, and as long as declarations don't conflict, you can have any number of declarations even inside a single translation unit.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
4

Functions are public (meaning their name gets exposed to the linker) if not defined with static.

pmg
  • 106,608
  • 13
  • 126
  • 198
2

functions can't be private in C. try defining it with static

BRHSM
  • 854
  • 3
  • 13
  • 48
0

You are linking both the files to create one executable binary isn't? And hence there cannot be a duplicate definition of a function.

Compiler will resolve the function names (symbols) to unique addresses in the executable binary. If it sees two definitions, it has a dilemma which one to choose for when the function is called during run, the execution can jump to one location and that needs to be pre-determined. As such compilers aren't happy with multiple definitions.

Prabhu
  • 3,443
  • 15
  • 26
0

To get over you could use static with that function.

And here is the best answer to what is static in c.

Community
  • 1
  • 1