0

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?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
MarkusWillson
  • 411
  • 1
  • 3
  • 11

5 Answers5

6

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
5

.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.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
3

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.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
3

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • you can't run a class file unless it declares a `final static void main()` – rupps Jan 13 '15 at 21:40
  • 2
    @rupps That's `public static void main`. And while you can't run a class as a main program otherwise, you _can_ find a `.class` dynamically at run time and execute its methods. You can't do that with an `.o` file. (But you can with, say, a `.dll`.) – ajb Jan 13 '15 at 21:56
3

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.

ajb
  • 31,309
  • 3
  • 58
  • 84