How do I compile on a Mac? - c

I'm trying to Compile my C program I have on my Mac.
I've been using Codepad.org to check that my Code works, but Codepad doesn't let me input my own values.
I need a Compiler that lets me input my own values, and I can save the Output as a txt file (to submit to my Professor).

2 possible ways,
get xcode, which includes gcc.
install macports, and use macports to install gcc.

Just use the Terminal app with gcc.
You can then run by appending ./ before the program name and > to save the output.
gcc -Wall program.c -o program
This will compile with all warnings (-Wall) with executable name program (-o).
run ./program > textfile.txt and this will put the output in a file.
(You might have to install Xcode and command line tools depending on your OS version)

Related

C Programming: The output window in vs code is empty

the screenshot shows what the empty field and the code writtenI have just set up VS Code for programming with C, I installed all the necessary things (gcc, gdb..etc)
But when I started writing basic code, vs code shows an empty output after I run it
Installation (Ignore if installed)
Windows
Install mingw/gcc/clang compiler and then add it to the path.
GNU/Linux
Install gcc/clang compiler using your package manager.
Arch Based Distros: `sudo pacman -S gcc`
Ubuntu/Other debian based distro: `sudo apt install gcc g++`
Now, install C/C++ extension for VSCODE here's the link C/C++ extension
Installing the C/C++ VSCODE extenion
Or you can directly install it through Ctrl+Shift+P and paste this snippet ext install ms-vscode.cpptools.
Compiling
Now, open your terminal in VSCODE and run gcc Test.c -o test and then run ./test to execute your program.
Or, if you have installed clang compiler then the terminal command goes here clang Test.c -o test and then run ./test to execute your program.
Main method have return type int, try return 0; after printf. Or you need to add argument to main method, in java program i know we can not run program without argument.
Or
Try make void main method

win10 gdb not in executable format: File format not recognized

I'm a C newbie who recently installed gcc and gdb on windows 10 through MinGW. However, when I tried to debug my program using gdb through win10 cmd I got an error.
Here's what I did on cmd:
gcc ./test.c
gdb ./a.exe
And I got this:
not in executable format: File format not recognized
Then I tried to install gdb on bash as well and did the following:
gcc ./test.c
gdb ./a.out
(gcc through bash would create a.out but would create a.exe through cmd)
This time gdb seems to be able to start the executable file correctly, however when I tried typing in break commands I got another error message:
No symbol table is loaded. Use the "file" command.
Can anyone help to solve the problem?
PS. I've tried using file but it didn't work either.
PSS.after using gcc -g ./test.c the gdb seems to be working correctly on bash. However the windows one still can't run .exe file.

Using GCC to compile C code

I installed MinGW on my Windows 8 laptop and tried to compile a C code file with
gcc test.c -o test.exe
the compiler gave no warnings or errors but it did not create test.exe
how do i get the compiler to create the file
test.c
My terminal session
An interesting observation:
When I deliberately introduce an error in the code and try to compile the compiler shows the error
Code with error
Compiler output
When I try compiling the same code using Command Prompt
This is what it shows
But the file does exist in the MinGW\bin directory
I moved the
test.c
file to
C:\
and started the command prompt in the
C:\MinGW\bin
directory
and here is what it outputs
Problem partially solved:
I disabled hybrid boot in windows 8 and restarted the computer. The compiler now works in Command Prompt but not in PowerShell.
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!
I know this is an old question, but I came across this after having this same issue and managed to solve it.
When I installed MinGW on my computer, I didn't add the MinGW bin directory to my PATH (<mingw install dir>\bin). I had written some code that referred to the GNU compiler binaries by their full path, and when I tried to compile something I experienced the same behavior you described.
So it seems like MinGW won't work properly unless it is added to your PATH. I think it's weird that gcc didn't complain about it though.
One possibility is Microsoft's use of VirtualStore.
This can cause problems with "missing" files with Cygwin. See for example, Cygwin sees a file that windows can't--I want to access this file from python and https://superuser.com/questions/400600/file-only-visible-to-cygwin-not-windows.
To verify whether this is the case, try doing a search of your entire hard drive for the file test.exe. Or try MinGW's ls rather than dir.
And since the OP "partially solved" the problem by moving to another directory, this could be the cause.

How can I stop gcc under Cygwin from adding ".exe" to compiled executables?

I would like to know how I can prevent gcc under Cygwin from automatically adding the .exe extension to compiled files, because I just caused myself a lot of confusion with "missing files". For context, I am working on a C project for university and I usually work in the labs which run Ubuntu (dual-boot with Windows), but to work from home I prefer using my Windows machine, ergo Cygwin. If I just remove the extension it still works just fine on either system, but it is rather frustrating to have to change the command to include the extension whenever I've just compiled it under Cygwin.
I looked up the FAQ from Cygwin to find that it is probably an issue related to an environment variable in .bashrc or .bash_profile (see here), but I am no command-line ninja and am not very familiar with editing configuration files... I found two related questions as well that show the same behaviour, but have nothing to do with trying to change it:
Compiling with gcc (cygwin on windows)
Executable file generated using gcc under cygwin
Any ideas?
It is actually for an MPI in C project so I have a Makefile that calls mpicc but that is not really relevant to the problem, since I just tried with gcc as well and both do the same thing. For the purpose of this question, the commands and outputs I get are:
$ gcc -o hello hello.c
$ ls
hello.c hello.exe
$./hello
Hello, world!
$./hello.exe
Hello, world!
Note that running with or without the extension does the same thing in the shell, but it does not with mpirun which is why I want to change this behaviour.
I eventually decided that Windows is not the programming environment for me. From now on all work that can be done in Linux will be.
7 years and no one to tell ?
My answer : Yes it's possible to produce an executable without .exe extension under Cygwin GCC. By telling the linker how to name its output.
$ echo -e "#include <stdio.h>\nint main(int nbargs, char *args[]) {
printf(\"Hello \\\n\");
}" | gcc -pipe -x c - -Wl,-oess2
This will produce an ess PE32 / PE32+ executable file, not a ess.exe.
The -pipe option instructs the GCC build chain to not write temporary files but use pipe between stages instead. The -Wl,-o option inhibits the default --force-exe-suffix.
And this way you can really nullify Cygwin GCC output with -Wl,-o/dev/null, the linker will fail when trying to close the output but you can trap the error message. If you get it, you can be assured that GCC reaches the link stage far enough to produce an output, which means that GCC can build an executable with this code.
From the ld man page :
--noinhibit-exec Retain the executable output file whenever it is still usable. Normally, the linker will not produce an output file if
it encounters errors during the link process; it exits without writing
an output file when it issues any error whatsoever.
DO NOT USE -Wl,-o/dev/stdout under Cygwin. Under Cygwin, /dev/stdout is a symlink, and if the linker fails it will DELETE /dev/stdout.
On the other end, -Wl,-o/proc/self/fd/1 will do no harm, but the linker will fail and will produce only an error message on stdout. Currently, it seems there is no direct way under Cygwin to pipe the linker output, even with named pipes.
The automatic exe extension for executables is there for a reason (Windows requires it). You should deconfuse (aka educate :-) yourself and accept the way Cygwin works. This is a feature rooted so deeply in the Cygwin/Windows guts that it is almost impossible to make it run without it.
For a "Unix feeling on Windows" with a different approach you want to check out AT&T's UWin.

How to fix C program error for Openmpi with netbeans running on ubuntu 12.04

I am trying to run a MPI program with C language.
I have installed GCC compiler and the openmpi libraries. I am running ubuntu Linux and Netbeans IDE. My challenge is that after including ‘mpi.h’ in my header file and compiling the application, I still get ‘fatal error : cannot find file mpi.c’. I have the files in home/user/lib/openmpi/include, but I cant get it too work.
Can anyone help?
You could try to change the compiler to /path/mpicc and the debugger to mpirun. This should work, although I did not test it, but probably the best way to compile MPI code is via terminal.
If you really depend on the IDE you cound try writing your code with it (to take advantage of auto-completion and such) and compile it in terminal using mpicc -o main.exe main.cpp [other .cpp files] and run it with mpirun -np number_of_processes_to_use ./main.exe [args]. You could write a small script or a Makefile to do it all in one command.
Good luck!
to save yourself some sanity, I'd recommend opening up a terminal and going from there (at least until you figure out what's what).
Also, using the mpi compiler to do things would simplify your life. (and likely automatically solve the missing source issue, as it should know where they are by default).
If you still can't locate them during compile then I'd look at adding the location where mpi.c & mpi.h are located to your C Include Path: How to add a default include path for gcc in linux?

Resources