简体   繁体   中英

Compiling files with own header in C

I am currently at the beginning stage of learning how to program in C, and I came across some questions regrading header files. For example

I have a header file named header.h ,

which has int comp (int, int) declared in header.h and

defined in header.c

In such case, If i were to compile a test.c using the comp function, I would have to go like

gcc test.c header.c

First question: having to add header.c everytime I gcc seems too inconvenient and redundant. Is it a necessity? If not, is there a way I can get around it? If so, why? Or is it, in fact, not redundant compared to its usage, and am I just complaining?

Second question: if I were to use multiple .c files with functions declared in header.h my gcc would have to go

gcc test.c header.c header2.c header3.c .....

and that again seems too redundant. (and from hereon, same questions as First question..)

Thanks in advance. First time asking questions in SO. Please tell me if there is anyway I can improve the clarity of the question.

I think you are looking for the make, that automates the execution of the files you are compiling.

With the use of make, you don't need to write every time the commands like 'gcc test.c header1.c header2.c ...' in the terminal (if you type the commands directly to the terminal, yes, you will need to do this all the time, which is very redundant and costs lots of time). Using make, you only do this one time, and then all you have to do is run the make command.

You can see more about makefile at https://en.wikibooks.org/wiki/Make . I hope it can help you.

You could make a shell macro or script to execute the command, if it is too much effort to use shell history feature to re-execute the command.

When your project starts to get complicated it's normal to use a build system in which you configure the build commands in the build system, and then you invoke the build system when you want to build. For example, write a Makefile that lists all the .c files using the right syntax for makefiles, and then type make each time you want to build.

You could consider using an Integrated Development Environment which is a (usually) GUI that includes a build system and other useful features, in which case you just need to hit a key to build and run.

Writing a simple Makefile is very useful for compiling C programs. Here's an example

CC = gcc
CFLAGS = -g -Wall
OBJECTS = main.o cfile1.o cfile2.o cfile3.o
run: $(OBJECTS)
    $(CC) $(CFLAGS) -o run $(OBJECTS)

Don't worry about header files when writing a Makefile; only worry about your ".c" files. This example Makefile assumes that you want to compile main.c, cfile1.c, cfile2.c, and cfile3.c. When adding the C files to your Makefile, make sure to use ".o" instead of ".c".

The Makefile should be in the same directory as your C files and must be named "Makefile" with a capital M. Simply type "make" to compile. Then run the program with "./run".

It is often desirable to spread out your code across multiple files; this helps to ease code management. Header files provide a unified way to expose functions defined in libraries or source code files to other source code files without including the actual code for those functions. This way, the same header file can be included in multiple source code files without compiling the same code for each of those files. However, this means that the source code for functions in the header file must be given to the compiler also. As such, you have to give your header.c to the compiler each time you compile.

This does mean that you will compile header.c each time you build your project, which is a bit redundant. One way around this is to compile the header.c into an object file , and then give that the compiler when you build:

gcc -c header.c -o header.o
gcc header.o test.c

Furthermore, software developers often like to distribute their program functions to other developer but without providing the actaul code. To do this, they often use software libraries, which contain the compiled source code, along with header files to access this code. This is probably a little more than what your looking for, so I'll leave you read up on it.

All this is used not to reduce redundancy in your compiler commands, but in your compiled programs. To make programmers' lives easier building their programs, makefiles and IDEs are often used. These may be things you might have to read up on, but the other answers posted here should provide a good starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM