How to compile a C program with gcc?
You need to run
gcc b.c -o b.exe
Without -o
option it'll use the default output executable name which is a.exe
on Windows and a.out
on *nix systems. You can easily see the a.exe with the dir
command
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
If
-o
is not specified, the default is to put an executable file ina.out
, the object file for source.suffix insource.o
, its assembler file insource.s
, a precompiled header file insource.suffix.gch
, and all preprocessed C source on standard output.https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html
For more information read Determining C executable name
Of course you can also run a
instead of b
Compiling a C++ program with GCC
gcc
can actually compile C++ code just fine. The errors you received are linker errors, not compiler errors.
Odds are that if you change the compilation line to be this:
gcc info.C -lstdc++
which makes it link to the standard C++ library, then it will work just fine.
However, you should just make your life easier and use g++
.
Rup says it best in his comment to another answer:
[...] gcc will
select the correct back-end compiler
based on file extension (i.e. will
compile a .c as C and a .cc as C++)
and links binaries against just the
standard C and GCC helper libraries by
default regardless of input languages;
g++ will also select the correct
back-end based on extension except
that I think it compiles all C source
as C++ instead (i.e. it compiles both
.c and .cc as C++) and it includes
libstdc++ in its link step regardless
of input languages.
Cannot Compile C Program That Uses a Library (FFmpeg) with GCC Because of the Library's Include Statements
When #include
is used with quotation marks (e.g. #include "file path here"
), it will read that file path as a relative file path.
In the case of compiling a C program using GCC, file paths are relative to the current directory. The "current directory" is the one into which you have placed your command prompt using the cd
command.
In my case, I cd
'd into C:/Users/User/Documents/Test
, meaning that all relative file paths are relative to C:/Users/User/Documents/Test
. So when my compiler read
#include "libavutil/samplefmt.h"
it basically tried to do this:
#include C:/Users/User/Documents/Test/libavutil/samplefmt.h
when I instead needed the compiler to look at …/Test/FFmpeg/libavutil/samplefmt.h
.
It turns out that the solution to this is to give the compiler additional locations to which relative paths might be relative. This is done with the -I[file path here]
argument when you compile.
In my case, the way I needed to use this idea was to add C:/Users/User/Documents/Test/FFmpeg
as a location to which paths might be relative. Thus, I could have taken my compile command:
gcc -c test.c
And inserted this:
gcc -IC:\Users\User\Documents\Test\FFmpeg -c test.c
However, this is actually an extremely clunky solution. There is a much easier way: it turns out that these file paths you provide with the -I
argument can be relative to your current directory themselves. In my case, because my current directory in the command prompt was alreadyC:/Users/User/Documents/Test
, I could simply remove this portion from the above command, shortening it to this:
gcc -IFFmpeg -c test.c
And this solved my problem.
Compile and Run a string of C source code
@bruno My idea was not having to deal with different windows and unix path names (windows utf-16 pathnames). But perhaps just calling gcc is a preferable solution.
I think calling GCC is easiest. However, just because you're calling GCC as an external process, doesn't mean you have to write your generated C to a file.
GCC is capable of taking its output from standard in. Here's an example, written in Bash.
echo "main(){}" | gcc -x c -
Here's the same thing in C:
#include <stdio.h>
#include <string.h>
const char *prog = "#include <stdio.h>\nint main(void) {puts(\"foo\"); return 0;}";
int main() {
FILE *proc = popen("gcc -x c -", "w");
fwrite(prog, sizeof(char), strlen(prog), proc);
pclose(proc);
}
And here's the same thing, but with error handling:
#include <stdio.h>
#include <string.h>
const char *prog = "#include <stdio.h>\nint main(void) {puts(\"foo\"); return 0;}";
int main() {
FILE *proc = popen("gcc -x c -", "w");
if(!proc) {
perror("popen gcc");
}
fwrite(prog, sizeof(char), strlen(prog), proc);
if(ferror(proc)) {
perror("writing prog");
}
if(pclose(proc) == -1) {
perror("pclose gcc");
}
}
I think that's the best way of accomplishing this.
Related Topics
Fixed-Size Floating Point Types
Is Floating-Point Addition and Multiplication Associative
Serializing a Class Which Contains a Std::String
Overloading Member Access Operators -≫, .*
Libpng Warning: Iccp: Known Incorrect Srgb Profile
Catching Access Violation Exceptions
Is Main() Really Start of a C++ Program
How to Create Directory Tree in C++/Linux
Why Are References Not Reseatable in C++
How to Find the Index of Current Object in Range-Based For Loop
The Procedure Entry Point _Gxx_Personality_V0 Could Not Be Located
Scope Resolution Operator Without a Scope