Mixing .c and .h files [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have seen C programs split into .h files with its respective .c files.
I know that header files are for macros/declarations/function prototypes and .c files for function definition.
But how are those .c got used ? passed by compiler's command line?

The C files are not combined by the compiler, but by the linker. Depending on how you compile your code this may be hidden from you.
The compiler will compile each of the C files into an object file for each C file. Then the linker will take these object files and combine them into an executable file or a library file.

2 options:
Pass the .c files to the compiler's command line, all together, for a "compile and link all in one" command. You usually only do that for toy and very small projects
compile each .c file individually (giving it as a separate compile command line), generating .o files. Then passing all these .o files together to the linker (normally it's the same command as the compiler...) to create your executable. This way if you change a .c file you only have to compile that file (to create its .o file again) and link, saving a lot of time.

Related

How to access all the files before compilation? [duplicate]

This question already has an answer here:
How do I save preprocessor output using the Dev-C++ IDE?
(1 answer)
Closed 2 years ago.
There are several steps involved from the stage of writing a C program to the stage of getting it executed.
when I compile the code I only get an .exe file. But I want to get all the files which are being made before the compilation (preprocess ones), an intermediate code file
where all those macros with are replaced with their actual values and preprocessor are replaced with their actual header files.
in general can we get all those files (preprocess one, compile one and linker one) separately?
To access all the intermediate files use the command (Ubuntu):
gcc –Wall –save-temps filename.c –o filename. This command generates all the intermediate files in current working directory.

Locate root cause of preprocessor errors - gcc [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to build a big project step by step. I'm working on Linux and using qt-creator and gcc compiler. I include a set of related files in the .pri file and then build, test functionality and then add a new set of existing files and so on.
The project compiled successfully then I included a folder containing some header files then when compiled I got many errors in libc-header-start.h, string.h, cpu-set.h, time.h and many other stdlib and system files. Errors like missing binary operator before token (, unknown type name __cpu_mask, expected ';', ',' or ')' before '*' token.
I think these are likely to be preprocessor errors that are caused, for example, if I forget a semicolumn or so, right?
My question is, how can I locate the exact location in the header/source files where all these errors started, for example, the line of code where the semicolumn is missing.
This answer is just collecting the comments on my question.
Here is what can help locating what is the root cause of preprocessor errors:
Make sure that the names of your header files don't conflict with standard header files.
Check the first file in the errors list. Check where this file is included. If this file is included by another standard header file, check where the parent header file is included in your code. The root cause of the errors may be in the lines before that #include statement in your code. For example, you may have forgetten a semi-column before this #include.
Use gcc -H file.c to check all the header files included with file.c.
Use gcc -E file.c to generate a preprocessed version of file.c, then you will have the full end picture of your file which you can then investigate.

Where can I get the source and header files for the gnu.org Simple Makefile? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Where can I get the source and header files for the gnu.org "Simple Makefile" example?
I reallywant to experiment with this very good example, except the fact that I can't find the referenced source & header files.
Any pointers?
To create minimal files with which the sample Makefile will work, you can run:
touch {kbd,command,display,insert,search,files,utils}.c \
{defs,command,buffer}.h;\
echo 'int main(){}' > main.c
Explanation:
The minimum C file that will compile into an object file is the empty file. For linking to succeed, you need one and exactly one main function.
The above code just creates empty .c and .h files mentioned in the sample Makefile and the main.c file with the main function.
As has been said, the contents don't matter so you might as well make it the minimum that works.
The C build system and the interaction of .c and .h files
In a real-case scenario, the role of .h files is just that they get #included from .c files. #inclusion is a literal copy and paste—you might as well replace each #include directive with the exact contents of what's getting included because that's exactly what the compiler will do.
The files that depend on .h files (according to the directives in the Makefile) should normally correspondingly #include the same header files.
Header-file dependency deduplication
Real-world Makefiles normally don't duplicate this info, but instead create header dependency files out out .c files (based on the #include directives contained within them) with something like (the header dependency files usually have the .d suffix):
#A makefile rule with some dynamic variables (more advanced make)
%.d: %.c
$(C) $(CFLAGS) -MM -MF $# -MT $*.o $<
#include will remake the dependency files if they need to be
-include $(CLIENT_SRC_C_D)
(tup, which is a great Make alternative does this automatically and language-agnostically based on filesystem reads).
Does the exact content of the files REALLY matter here? I believe the purpose of the linked manual is to describe the workings of the make, to show the syntax and semantics of writing and using a makefile, not for building any particular real application. The names used are just a notion. You can simply create the files with that particular name, if you want, and put some dummy functions.
For your reference, you can check this alternative link which has the source codes for the used files, too. This is very simple example but should be enough to give you the idea.

How to find a function which is declared in a .h file in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to find the specific definitions of some functions which are declared in a .h file. The question is how I can accomplish that? Because there're hundreds of .c files in the directory. And I have tried to use grep command, it doesn't work efficiently. Is there any other method?
Slice of my .h file:
248 #define EXPAND(a,b,c) a b
I use vim, so:
:tag EXPAND
E433: No tags file
E426: tag not found
248, etc. is the line number. and 433, 426 has no relation with EXPAND, one is a blank line, the other is within a comment section.
EXPAND is a macro? I thought it was a function because it appeared in other .c files like:
u[EN] = EXPAND(v[VX]*v[VX], + v[VY]*v[VY], + v[VZ]*v[VZ])
#didierc is right, I found this is a macro. I'm really green to C. Many thanks to #Basile Starynkevitch, offered a way to trace a function, that's another prob. puzzled me
Use etags with emacs (or else ctags, e.g. if using vi). Instead of grep consider using ack.
Read more about the linker (e.g. Levine's book Linkers & Loaders; details are operating system specific); on Linux see binutils. Notice that externapplies to declarations, not definitions of function or variable names.
Don't forget to run the ctags or etags command from time to time, and before using tags in your emacs or vi editor.
About macros: please understand that the C preprocessor is the first phase of compilation. Read documentation of GNU cpp. Don't expect macros to be functions, they are textual devices! Use perhaps gcc -Wall -C -E foo.c > foo.i (with some more options to gcc, e.g. -I and -D ones) to get the preprocessed form foo.i of source file foo.c; look inside the generated foo.i with your editor.

Creating .I file in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Sir pls tell me how to create .I file (extended source file) in c
A common way to create files in C is with the fopen() function.
#include <stdio.h>
FILE *handle;
handle = fopen("extended.I", "w");
if (handle != NULL) {
/* ... */
fclose(handle);
}
Terribly vague question, but it sounds like you are using Visual Studio. Right-click your project, Properties, C/C++, Preprocessor, change "Generate Preprocessed File" to Yes.
After you rebuild, you'll get the .i files with the preprocessor output in your project directory.
arsane's comment is the correct response if you are on Linux. "To expand the macro, you can try
gcc -E -o main.I main.c
"
When using the gcc compiler system, it is possible to halt the compiler system at particular phases by using compiler flags. -E for .i files, -S for .s files (an assembly language version of the program)
From the gcc man page:
-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of preprocessed source code, which is
sent to the standard output.
Input files which don't require preprocessing are ignored.
The following command will create a .i file from a .c file
cc -E main.c -o main.i

Resources