C shell script for gcc compiler - c

I would like to make a script for testing my c program but I could not figure out why it does not work
I tested it with a easy code so that I am sure that the problem is not because of the C file.
My C Code is:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
return 0;
}
And my Shell Script is:
gcc -o main main.c
echo "Hello world"
If I execute the script I get the error message on my console
: No such file or directory
gcc: fatal error: no input files
compilation terminated
This is the Error I get if I want to execute the script
https://imgur.com/a/zIl55
In the image you can see my problem
If I compile the C file “per hand” it has no problem but if I execute the script which contains the same statement it does not work.
If I just want to compile and only write the command for compiling in my script it works but as soon as I ad an echo or any other command it will not work.
I am using an Ubuntu Shell under Windows.
Any help would be very appreciated.

My guess: you wrote your script with a Windows-only editor such as Notepad, so it used Windows newlines (\r\n AKA CRLF). bash passes main.c\r as argument to gcc, which cannot find it. Printing out the error, the terminal interpret \r as carriage return character, so, it goes in column 1 and prints the rest of the message, which results in the bizarre thing you are seeing.
You can check if this is the case by running dos2unix over your script, if now it works correctly it's as I suspected.
Solution: use a more serious editor and/or make sure it writes Unix newlines (plain \n, AKA LF).

Related

Why does the terminal keep returning a zsh parse error no matter my input?

I am incredibly new to the C language: I am trying to run sample C programs from codecademy, such as the hello world command below:
#include <stdio.h>
int main() {
// output a line
printf("Hello World!\n");
}
Bottom line is, every time I try to run any code in my macOS terminal, I always get the following error:
zsh: parse error near `\n'
What can I do to resolve this problem?
c is a language where you need to compile the code you've written. You do that by starting a C compiler and give the file containing your C code as input.
Example:
File: myfirstcprogram.c
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
}
Then at the zsh prompt, invoke the compiler:
cc myfirstcprogram.c -o myfirstcprogram
-o myfirstcprogram is here an argument to the compiler telling it what to call the final program.
cc may be clang or gcc or any other compiler you've got installed if cc isn't already linked to the proper compiler.
When the compilation is done, the executable myfirstcprogram should have been created. You can now run it from your zsh prompt:
./myfirstcprogram
You can run it without recompiling it as many times as you like. Only when you change the source code (myfirstcprogram.c) do you need to compile the program into an executable again.

Cannot run C program from command prompt on windows 10

I am using Windows 10 OS. I installed MinGW for compiling C programs. I tried running my program using the gcc command on the Command Prompt. The file compiles and an executable file(.exe) is formed in the same folder as my source file. But when I try running this file, I keep getting the message 'Access is denied'. Also the .exe file vanishes after this. I do not know what is wrong. Please help me out.
P.S Another time I did the same thing mentioned above and the .exe file ran and I was able to see the output on the Command line. And this time the .exe file did not vanish either.
What was the command you put to compile the program? Also to compile and make an executable file, you have to put this command - "gcc -o nameofexecutablefile nameofsourcefile.c", to run this program just type the name of your executable file in cmd. And to stop the program's window from closing, put "system("pause");" right before the "return 0;" in your program, at the end of the program it will display "Press any key to continue..." and when you press any key, the window will close. Also check if you did any mistakes in your program. I'll also give an example -
------------CODE-----------
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
system("pause"); // this will pause the window
return 0;
}
-------OUTPUT--------
Hello World
Press any key to continue...

Daemon on embedded Linux device using Busybox be written in C or as a script

Should a daemon on an embedded device using Busybox be written in C or as a script?
All the examples I have seen use #! /bin/ash at the top of the file and that is for scripting? But in the device I'm writing to has only complied C files (I think) and symbolic links in /usr/bin.
Every way I try to compile a C file with #include </bin/ash> (e.g. gcc -Wall -o daemon_busybox daemon_busybox.c) I get error after error report in /bin/ash:
/bin/ash:174:1: error: stray ‘\213’ in program
/bin/ash:174:1: error: stray ‘\10’ in program
/bin/ash:174:1: error: stray ‘\273’ in program
/bin/ash:174:1: error: stray ‘\204’ in program
/bin/ash:174:1: error: stray ‘\342’ in program
Note I have set this: /bin/ash -> busybox
Any ideas which way I should go?
Update:
I've been given the task trying to see if a daemon can be run on a small device that runs Linux (2.6.35-at-alpha4) and Java (SE Embedded Runtime Environment) with very limited memory (i.e. a 10 second wait to get java -version to report back).
Two weeks ago I didn't know much about daemons — only knew the word. So, this is all new to me.
On my development machine I have built two different daemon files, one in C and one as a script. Both run very nicely on my Linux machine.
But because of the very small size of the target device there is only busybox (no /lib/lsb/init-functions). So I'm trying to build a 3rd daemon file. I believe it should be written in C for this device, but all examples for busybox point to scripting.
Once your question is edited so that the file name you're trying to #include is visible, the problem becomes self-evident:
#include </bin/ash>
This tries to make the C compiler include the binary of busybox (via the symlink /bin/ash) into the code to be compiled. The average binary is not a valid C source file; this is doomed to failure.
Perhaps you simply need to drop that line — the C compiler stands a better chance of working if it is given header files and source files to compile. Maybe there's more work needed; we don't have enough information to help there.
Many daemons are written as C programs, but a carefully written shell script could be used instead.
Personally, I would like to do this as a script (I've never liked C). But on the device everything in the /usr/sbin folder looks like a C file. So, the conservative coder in me says C is the way to go. I know: ask the guys developed the device — but they're long gone. Right now my daemon is just a test (i.e. printf("Hello World\n"); ). I'm trying to get printf passed to Busybox. But so far I cannot get this file to compile. I just need a simple daemon in C to start.
OK; your C code for that should be just:
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Save it in hw_daemon.c. Compile it using:
gcc -o hw_daemon hw_daemon.c
If that won't compile, then you've not got a workable C development environment for the target machine. If that will compile, you should be able to run it with:
./hw_daemon
and you should see the infamous 'Hello World' message appear.
If that does not work, then you can go with the script version instead, in a file hw_script.sh:
#!/bin/ash
printf "Hello World\n"
You should be able to run that with:
Predicted output — not output observed on a machine.
$ ash hw_script.sh
Hello World
$ chmod +x hw_script.sh
$ ./hw_script.sh
Hello World
$
If neither of those works at all, then you've got major problems on the system (maybe Busybox doesn't provide a printf command workalike, for example, and you need to use echo "Hello World" instead of the printf).

Nothing working when trying to run C programs?

I have been trying to use Cygwin64 to compile and run C programs. I have been trying to run a simple Hello World program as follows:
#include <stdio.h>
int main()
{
printf("Hello world!");
}
And in Cygwin, I have been typing the following command:
gcc -o hello hello.c
Followed by
./hello.exe
After that, there is simply no output, and I receive a new prompt.
Does anyone have a solution to this issue?
Gcc don't give .exe extention for output executable file. You are giving gcc -o hello hello.c command then it generates executable with name hello (not hello.exe). If you don't give any name for output file, it generates executable file with name a.out
I would guess that the prompt printed after the execution of the program is printed by first returning the cursor to column 0. This is then overwriting the "Hello World!" message you've printed. You should probably add a "\r\n" to your printf call as follows:
#include <stdio.h>
int main()
{
printf("Hello world!\r\n");
}
This will move the cursor to the next line before exiting, so that the prompt is printed on the next line instead.
I guess you just need to pause your program.
Can be done with a get(), scanf()
Also you should do a return 0; at the end of your code

syntax error in main simple c program osx using make

This is a n00b question and I've seen an answer that does not help me.
I'm running a simple c program (firsty.c) written in textmate:
#include <stdio.h>
int main()
{
printf("hi world.\n");
return 0;
}
I've entered the following into the terminal with the following results:
$ make firsty.c
make: Nothing to be done for `firsty.c'.
$ ./firsty.c
./firsty.c: line 3: syntax error near unexpected token `('
./firsty.c: line 3: `int main()'
probably something simple, but I don't understand what's wrong.
make firsty.c isn't doing anything at all. Try instead make firsty, and then ./firsty.
You are trying to execute the source file. You need to execute the binary file which was hopefully built by make.
I do not know what your makefile is doing, however if it's something like gcc firsty.c the binary output file will be named a.out by default. Use gcc -o executable_name_here to have differently named output file (http://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options)
Unix (osx at this time) is considering executable file a script, and tries to execute it. On other thing to do would be to remove executable permissions from your source file and then you will not be able to run it.
I think u have not created any Makefile which is used by make command to compile the given source file(s)... so try to write a makefile(http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/) else try to compile as...
gcc firsty.c -o firstly
then u'll get the executable file in the same directory & u can execute it as
./firstly
take care of the '#'. when you excute a source code file, the OS maybe excute it with the shell. So we get the syntax error.
Try make firsty, it will work and will make a executable with a name firstly.
If this oes not work, try make ./firstly.
Please note that while doing a make as such you need to supply the name of file only and not the extension as .c
The output file is created with the name of file and it will search for corresponding .c file to compile.
In your case
make firsty
This will look for firsty.c to be compiled and create an output file with name firsty.

Resources