Search This Blog

Wednesday, April 16, 2008

Leading underscores in C

C compilers were prepending an underscore to all the symbols (function, variable names). I read from somewhere this is to avoid name collision with other language libraries. This convention was started with UNIX. When UNIX was written in C to use the existing assembly libraries, C symbols were added with a leading underscore by compilers and linkers.

Currently there is no need for this unless otherwise if you want to use old library. So most of the new C compilers are not adding leading underscores; however it also depends on the platform and object file. For example, Linux a.out GCC does such prepending, while Linux ELF GCC does not.

To explicitly tell the GCC compiler to not to use leading underscore you can use –fno-leading-underscore option.

Note: FORTRAN was adding both leading and trailing underscores to avoid name collision to avoid naming collision with C.