clawhammer1234
clawhammer1234

Reputation: 93

header source file needed after compilation?

If I make headerfile.h and then headerfile.c with the source code for the functions. Once I compile the main C file sampleprogram.c

cc -o sampleprogram headerfile.c sampleprogram.c

Is that source file for the header still needed? Is headerfile.c and sampleprogram.c compiled and linked together? Do you need to include the headerfile.h along with sampleprogram if you were to put the program on a usb drive and put it on a different computer?

Upvotes: 1

Views: 546

Answers (2)

Jazzwave06
Jazzwave06

Reputation: 1851

Headers are not required to execute a program or to use a shared/static library. However, headers are required to write code that uses the interface of a library. Usually, under RedHat distros, you'd have rpms that install the library (libuv) and rpms that install the headers (libuv-devel).

Therefore, without headers, you can execute a program, or you can link against a library. However, if you want to write code that uses the API of a library, you need the headers on your system.

If you compile a program and put it on a USB drive, all you need is the executable. However, you might need to recompile your program as the compiler targeted the specific architecture of the system it ran on. If you move the executable to another computer, it might not work.

Upvotes: 5

Erik W
Erik W

Reputation: 2628

Short answer:

No, it is not needed.

Longer answer:

No it is not needed, because when you use #include, the content of the header file will basically be pasted in to the C file. Because of that, the executable will not need the header file nor the c file to run (they are compiled into the executable already by the compiler)

Upvotes: 4

Related Questions