0

As you can see above,I want to know how library functions (like printf) are made in C. I am using the borlandC++ compiler.

They are defined in lib files (***.lib), header files only have prototypes.

Lib files cannot be read in text editors.

So, please let me know how they could read?

fuz
  • 88,405
  • 25
  • 200
  • 352
prog
  • 77
  • 3
  • 2
    Depends on which compiler you use. GCC generally uses glibc, whose source is available at http://www.gnu.org/software/libc/ – Lee Daniel Crocker Dec 22 '14 at 22:43
  • possible duplicate of [source code of c/c++ functions](http://stackoverflow.com/questions/1127328/source-code-of-c-c-functions) – clcto Dec 22 '14 at 22:43
  • .lib files are compiled binary code and don't contain the source code (just like .exe for executables). The source code for some open source C standard libraries can be found online. For example `printf` in GNU libc: https://sourceware.org/git/?p=glibc.git;a=tree;f=libio;h=9b6ae9cab66e6d1e21f072d93255ebaf4d650454;hb=HEAD – tmlen Dec 22 '14 at 22:48
  • The implementation of `printf`: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=stdio-common/vfprintf.c;hb=HEAD – tmlen Dec 22 '14 at 22:56
  • Please notice that C++ is not the same thing as C. – fuz Dec 22 '14 at 22:57
  • There is no guarantee functions in a .lib file are written in C, it's quite the reverse: a library can be created in lots of different languages. As far as the linker goes, it's only the generate *code* it is interested in, not its source. – Jongware Dec 22 '14 at 23:04
  • 1
    See if your compiler came with the source code for its libraries. If it didn't, there's not much you can do. (All versions of C++Builder did include this) – M.M Dec 22 '14 at 23:57

1 Answers1

1

C is a compiled language, so the C source code gets translated to binary machine-language code.

Because of that, you can't see the actual source code of any given library you have.

If you want to know how it works, you can see if it's an open source library, find the source code of the particular revision that generated the version you're using, and read it.

If it's not open source, you could try decompiling - use a tool that tries to guess what the original source code could have been like for generating the machine code your library has. As you can guess, this is not an accurate process - compiling isn't an isomorphic process - and, as you probably wouldn't have guessed, it could be illegal - but I'm not really sure what conditions it depends on, if any.

mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81