Try to compile your code normally as
gcc test.c
If you get default output file a.exe,then go for
gcc test.c -o test.exe
Try to compile your code normally as
gcc test.c
If you get default output file a.exe,then go for
gcc test.c -o test.exe
I would suggest you go through this compilation instruction :-
gcc -o test.exe test.c
I believe this code runs perfectly on your windows system.Please inform if it doesn't!
Videos
if you have codeblocks installed with mingw as the gcc compiler then follow these steps
- Right click on my computer -> go to properties -> advance system settings
- Then make an environment variable named PATH and paste the complete url like program file (x86)/codeblocks/mingw/bin.
- now open cmd
- go to the directory where your program is saved.
- type gcc program_name.c -o program_name.exe to compile the program.
- run the program by typing program_name
Please read Compile Programs with MinGW -- A Guide for New Users.
To make gcc produce assembler code, use -S option:
-S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s. Input files that don't require compilation are ignored.
Good luck!
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
-ois 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
Compile like this
gcc -o b b.c
by specifying output filename
-o <output_file>
I've been learning C and playing around with it for a couple of months already.
I'm following CS50 course and first half of the course is almost entirely in C.
That being said, I only used their online codespaces so far, - and for my own personal practice, I used online complilers such as https://www.programiz.com/c-programming/online-compiler/
and https://www.onlinegdb.com/online_c_compiler .
This allowed me to focus on coding and not worry about compiling. I've made some stuff already, entirely using these tools.
However, now arrives the time that I need my own compiler on local machine.
The main reason is that I want to start practicing working with files on my own hard disk, and also using libraries outside of what these tools offer, such as conio.h.
I already tried to google how to set-up a compiler in Windows, but I've bumped into many hoops and obstacles and it's not (at least for me) as straightforward as it might seem.
So I'm asking you for help to set up my own coding environment for C in Windows, where I could compile the files (ideally with make too, and not just clang), where I could include external libraries, where I could work with files on my own HDD, etc... And ideally, where I could even turn these files into classical .exe files.
Thanks!
EDIT: Resolved:
Installing Visual Studio 2022 Community with included tools for C++, and then running Developer Command Prompt allowed me to compile C files by using command: cl filename.c which returns exe file, filename.exe . This exe file can be started by simply executing filename in command prompt or even from Windows by simply double clicking on its icon, like any other file. Thanks for all the responses, especially to RidderHaddock and _mutaz_ who gave me best directions.
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.
If you give the code a .c extension the compiler thinks it is C code, not C++. And the C++ compiler driver is called g++, if you use the gcc driver you will have linker problems, as the standard C++ libraries will not be linked by default. So you want:
g++ myprog.cpp
And do not even consider using an uppercase .C extension, unless you never want to port your code, and are prepared to be hated by those you work with.
I've got a program that is split into header files and c files along with the main. I was told to do gcc main.c <library.c>... for every other code but i don't remember having to do that when I've done stuff with header files in the past. How are you supposed to do this. Also are you supposed to include the headerfile in the .c version that the header is declaring the functions from? Sorry for my lack of technical terms. I'm exhausted today