DEV Community

Adam Crockett πŸŒ€
Adam Crockett πŸŒ€

Posted on

The different files of C

I'm trying to form a mental model of something that is never truly explained in any post or tutorial.

What is the purpose of the following files:

  • .c
  • .h
  • .a
  • .o

.c now I assume I could write the whole program in this one file but honestly I like making libraries so I guess my functions go somewhere else and are included in. But is that where the .h comes in?

As for .a and .o let's go, tell me more 😁 are there any more of the alphabet I should know?

.c .h .a .o .s seems like there a hidden meaning to c programming?

Top comments (11)

Collapse
 
juancarlospaco profile image
Juan Carlos
  • .c is source code, human readable, logic.
  • .h is header file, human readable, pre-processor logic, definitions.
  • .o is object file, not human readable, binary format, linker can "concatenate" these and a binary header to get an EXE.
  • .a is a library, binary format, statically linked, has everything it needs inside itself, an EXE can call functions inside directly.
  • .so same as .a but dynamically linked, it imports functions from other .so, other .so can import functions inside, an EXE can call functions inside directly.
  • .dll same as .so but for Windows.
  • .dynlib same as .so but for Mac.

I dont code C directly, but I use Nim that compiles to optimized C/C++.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

Nice and simple thanks Jaun. I can tell your enjoying Nim so I won't probe to much. But I still wonder how I can write a program in c that consists of several, from what you say. .c files. I guess .h files are just for macros and those define statements etc. Not for:

int add(int a, int b) {
    return a + b;
}

Maybe I can include .c files into the main .c file?

Collapse
 
nestedsoftware profile image
Nested Software • Edited

You can declare functions in header files:

#ifndef D_H_
#define D_H_

int add(int a, int b);

#endif

These are used during compilation, where multiple files A.c, B.c, C.c depend on some given function(s) in file D.c. The files A.c, B.c, C.c can be compiled using the information in the header file D.h only, which contains the function name and signature. The real code in D.c that has the logic you want to run is associated later, during linking.

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Be careful though, according to the video, you declare a function prototype in the header is clearer, otherwise people like myself would had wrote a real function in the header with logic (which I have done 😳).

Thread Thread
 
konstantinklima profile image
Konstantin Klima

Well, there are two terms here which are distinct - declaration, as Nested said, is giving a prototype of a function or a name of a variable.

void f()

Defining is giving the function it's body (the code of the function).

void f() { return; }

The declaration tells the compiler what to expect and a promise that we will give him that exact thing at some point later.

We can use this to our advatange - for instance by declaring all the functions at the top of the file and then defining them bellow, which helps us better organize the file for human reading.

Similarilay, you put the declarations in the header, and the definitions in the .c file.

You can also both declare and define the functions in the header file - these are called header only libraries (many cool ones available online). The problem with this is that if you have A.c, B.c, C.c, all of them will copy-paste all the functions from the h file into the end result, which can cause slower compilation and potential conflicts. This can by solved by using #ifndef (guards) or the #pragma once preprocessior directive.

Collapse
 
juancarlospaco profile image
Juan Carlos

Yes you can include other c files in your c file.
Or you can tell the compiler to include the file via a command line parameter, then it builds all C files as one thing, this way the compiler can compile the files in parallel.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Practically, I don't understand why webview.h, not webview.c.

Also, I wrote cgo, but I seemed to write functions that where copied from webview.h? Also, it is indeed possible to reference webview-custom.h externally.

Collapse
 
konstantinklima profile image
Konstantin Klima

These are called header only libraries.
The idea is portability primarily - you don't drag along C files which you have to compile and keep track of (by using make and linking them separately). The compiler does copy all the code into the .o file made from your .c that included the .h and that is the down side.

Collapse
 
manchicken profile image
Mike Stemle

Very close. The .a is a static library, the .a stands for β€œarchive.” It contains symbols which the linker can extract from the archive and insert into an executable as it is being built.

A .so file uses some sort of dynamic library loader (ELF in GNU/Linux) to load the library, and then the executable actually runs the symbol from the external file using that ELF subsystem.

Collapse
 
agsb profile image
Alvaro Gomes Sobral Barcellos

Sure.

In old epoch of Unix on PDPs, those extentions are used to distinct types of original files, as .c text in C language, .h original headers of functions and defines, .a code in assembler, .o object code for processor, .s text in assembler language.

and yes, those was choose for chaos.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

This video helped me a lot.