Creating .I file in C [closed] - c

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

Related

I accidentally wrote wrong file name in command and file has deleted [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I wanted running a c program through the terminal, and instead of writing the following: gcc -o main.c program.c
,
I have written the following: gcc -o main,c program.c. This unfortunately disappeared my file "program.c", perhaps deleted it, and created a new txt file called "main,c" which looks like that:
Now, I have searched a lot this issue over a few sites and I couldn't find a compatible case for this one. Is there a way to recover the c file?
In addition, all remained is program.c.o file and main,c file.
Thanks!
Recovering the original source file is not a programming question, it is a general computing issue. With any tool (word-processor, photo-editor or as in this case a compiler), if you rewrite a file, the original is replaced and so no longer available.
You seem very confused over how to drive the compiler - the action you said you have taken would not on its own caused the results you state; I suspect in your confusion you have taken other actions that have simply worsened your situation.
Neither command line would have been correct in any case. The -o option specifies the output file name, and in the first case it would have created or overwritten main.c, and in the second it would have created main,c.
If you only executed the second command, main,c will have been created, and main.c will still exist. Neither of those commands will have deleted anything, but may have overwritten the sourcefile with object code file rendering it useless.
It is not clear what you were actually trying to do, but if you were trying to compile main.c to create program called program, then:
gcc -o program main.c
If however you were trying to compile and link two source files called main.c and program.c to create an executable, then:
gcc -o program main.c program.c
or even:
gcc -c main.c
gcc -c program.c
gcc -o main.o program.o
From the comments it seems in fact you are attempting to compile program.c to create an executable called main.c. Don't do that! The .c extension is used to indicate a C source file - input to the compiler). The output of the compiler is binary object code (and following the implicit link a binary executable). What you need then is simply:
gcc -o program program.c
None of that explains how you managed to delete program.c or even create a file called program.c.o rather then simply program.o - "the steps to reproduce" provided are not plausible. You have clearly made a string of erroneous actions to end up where you are. Unpicking it is probably unproductive; it is gone, you must start again, take care, use the compiler correctly and perhaps make a back-up copy or use a version control system before experimenting further.

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.

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.

How do I run a C program from my Windows command prompt? [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
Using Vim as an editor, I wrote the following simple code in C and saved it as helloworld.c :
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
}
In command prompt, I wrote:
start chrome helloworld.c
This caused my browser to open up the file, but it did not print Hello World. Instead, it just displayed the code I had written. Did I not save it as a C file?
Also, I was wondering how to display the result of my C program inline on command prompt, as I am fairly new to it. While searching the internet, I could not find any answers. Am I supposed to do so from Vim? I learned that you are supposed to do ./ in the gedit command box to display the result inline, but this does not work for the one that comes with Windows.
Please help and thank you for taking the time to read and answer.
As #Ernest Friedman-Hill has already said, you normally have to compile the program. However, there are alternatives.
One alternative is the Tiny C Compiler, from http://bellard.org/tcc/. TCC does allow you to run the program without compiling it.
tcc -run helloworld.c
Does exactly what you want.
The Tiny C Compiler is not the only way to run C code from source without compiling it first. There are a few other alternatives.
CSL: http://csl.sourceforge.net/csl.html
Ch: https://www.softintegration.com/
PicoC: https://code.google.com/p/picoc/
CINT: http://root.cern.ch/drupal/content/cint
I hope this helps.

Mixing .c and .h files [closed]

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.

Resources