So, I know that in Java, .java is the human-readable version, and .class is compiled.
In c, the human-readable version is .c, but is there any separate file extension for the compiled class?
So, I know that in Java, .java is the human-readable version, and .class is compiled.
In c, the human-readable version is .c, but is there any separate file extension for the compiled class?
C generally produces files for native platforms (not byte-code). On windows, .exe
and .dll
are common. One frequently sees intermediate "object" files with .o
across many platforms (these are intermediate in that they haven't been linked). There are header files which end in .h
. On Linux, you generally see .so
libraries and executable files don't generally have an extension. And on Mac, it depends on a large number of things.
.o
(or .obj
) object files are compiled .c
files, and are the closest thing to Java's .class
files.
They can get bundled together in an archive (.a
), or they can be linked together into an actual program or dynamic library.
The nearest analog is that C files are compiled to object files (usually ending in .o
). Object files expose symbols and are linked into an executable. Object files are not the same as Java .class
files, however.
Programs written in the C programming language are normally compiled to native executables, for example a Windows .exe
file.
Most C compilers compile each source code file to an object file, often with the extension .o
. The linker takes one or more .o
files and creates a native executable out of them.
An .o
file is not directly comparable to a Java .class
file. For example, you cannot execute an .o
file like you can run a Java .class
file.
Since .class
files serve several purposes in Java, there may need to be several different answers.
(1) When your Java code declares a variable to be of a certain type, or uses new
to create an instance of a type, the compiler needs to know what methods and other members are available for the type, as well as other information. In C, this kind of information has to be in the source; that gets done by including .h
files.
(2) Java .class
files contain the code that gets executed. When the program is executed, code from .class
files gets loaded; a program can also load a class file dynamically for a type whose name isn't known until runtime. For C, the code is generated in object files (.o
on Windows or Linux systems), and later linked into a single executable containing all the program's code (.exe
on Windows; no extension on Linux). Code can also be brought in dynamically after the program starts; on Windows, code from a .dll
dynamic library file can be loaded.
(3) Information from Java .class
files is used when a program uses reflection. C has no such concept.
(4) When debugging, the information the debugger needs about variables, methods, etc., is kept in .class
files. For C, it's normally kept in the object file, in a special format in the symbol table (such as DWARF
or STABS
). Usually you have to add a flag when running the C compiler to get it to store the extra info.