syntax error in main simple c program osx using make - c

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.

Related

exe file not found after gcc creates output

Hello I am learning c and I have created a simple script. I ran it with gcc simplify.c -o simplify.
Here is the c script
#include<stdio.h>
int main(){
int age = 3;
printf("%i\n", age);
return 0;
}
when I try to run it
$simplify.exe
bash: run: command not found
I get the result shown. What am I doing wrong
Whenever you use a filename as a command, bash will search for it in directorys like /usr/bin. Imagine the situation, in which someone put a executable called ls somewhere unprotected on your computer. Like #some programmer dude pointed out, you habe to explicitly specify the path to your executable. This path can be relative (./simplify.exe) or absolute (/home/username/projects/simplify/simplify.exe).
By the way, on linux systems it is umcommon to use a file ending, espacially ".exe". If you want to use one, I recommend ".elf", which stands for "executable linkable file". (You can do so much more than EXEcuting a file - and down we go the rabbit hole)

How to use Makefile for .c program which needs to be imported to Makefile

I was trying to execute Makefile and wanted to execute a C program with it. First, how can I include test.c file for makefile?
I've placed makefile in root directly as there will be other .c files later added.
Can anyone hep me executing this?
File structure:
Makefile code so far not working (it will work if I place it inside src still not getting the output of file.)
# -o : object file
Test: test.c
gcc -o Test test.c
Glad if anyone can help or suggest anything!
I don't usually follow links but I was curious so I did so. Your problem isn't related to make or makefiles or how your code is built. As best as can be determined from the screenshots, all that is fine.
The problem is that when you try to run the program, it's not found.
When the shell tries to run a program it looks in the directories contained in the PATH environment variable, and only there. It won't look in the current directory, unless the current directory is on the PATH.
So when you type the name of a program without a pathname to tell the shell where to find it, such as Test (by the way it's not a good idea to call your program Test because there is a system program named test on POSIX systems and it can cause confusion), it will search the directories on PATH for Test, and if the program is not found there it will fail.
If you don't want to rely on PATH you need to give the shell the pathname of the program you want to run. So you can run .\Test instead (on POSIX systems it would be ./Test), to tell the shell that you want to run Test from the current directory.

Syntax error near unexpected token '('

As a beginner, I am trying to write a simple c program to learn and execute the "write" function.
I am trying to execute a simple c program simple_write.c
#include <unistd.h>
#include <stdlib.h>
int main()
{
if ((write(1, “Here is some data\n”, 18)) != 18)
write(2, “A write error has occurred on file descriptor 1\n”,46);
exit(0);
}
I also execute chmod +x simple_write.c
But when i execute ./simple_write.c, it gives me syntax error near unexpected token '('
Couldn't figure out why this happens ??
P.S: The expected output is:-
$ ./simple_write
Here is some data
$
You did
$ chmod +x simple_write.c
$ ./simple_write.c
when you should have done
$ cc simple_write.c -o simple_write
$ chmod +x simple_write # On second thought, you probably don’t need this.
$ ./simple_write
In words: compile the program to create an executable simple_write
(without .c) file, and then run that. 
What you did was attempt to execute your C source code file
as a shell script.
Notes:
The simple_write file will be a binary file. 
Do not look at it with tools meant for text files
(e.g., cat, less, or text editors such as gedit).
cc is the historical name for the C compiler. 
If you get cc: not found (or something equivalent),
try the command again with gcc (GNU C compiler). 
If that doesn’t work,
If you’re on a shared system (e.g., school or library),
ask a system administrator how to compile a C program.
If you’re on your personal computer (i.e., you’re the administrator),
you will need to install the compiler yourself (or get a friend to do it). 
There’s lots of guidance written about this; just search for it.
When you get to writing more complicated programs,
you are going to want to use
make simple_write
which has the advantages of
being able to orchestrate a multi-step build,
which is typical for complex programs, and
it knows the standard ways of compiling programs on that system
(for example, it will probably “know” whether to use cc or gcc).
And, in fact, you should be able to use the above command now. 
This may (or may not) simplify your life.
P.S. Now that this question is on Stack Overflow,
I’m allowed to talk about the programming aspect of it. 
It looks to me like it should compile, but
The first write line has more parentheses than it needs.
if (write(1, "Here is some data\n", 18) != 18)
should work.
In the second write line,
I count the string as being 48 characters long, not 46.
By the way, do you know how to make the first write fail,
so the second one will execute?  Try
./simple_write >&-
You cannot execute C source code in Linux (or other systems) directly.
C is a language that requires compilation to binary format.
You need to install C compiler (the actual procedure differs depending on your system), compile your program and only then you can execute it.
Currently it was interpreted by shell. The first two lines starting with # were ignored as comments. The third line caused a syntax error.
Ok,
I got what i was doing wrong.
These are the steps that I took to get my problem corrected:-
$ gedit simple_write.c
Write the code into this file and save it (with .c extension).
$ make simple_write
$ ./simple_write
And I got the desired output.
Thanks!!

error in compile file in C with gcc in linux terminal

i write a simple program in c like this in the picture , it's a small code to print hello world i change the permission of this file but i don't get any good result
i get an error when i want to compile my code with gcc in my terminal i have tried to change the code but i don't get any good result
this the error
That's because you are trying to execute the source code instead of exec_file.
Try
./exec_file
instead of
./foo11.c
You run the wrong program. You compile to exec_file, but you try to run the C file.
You should do:
~$ gcc foo11.c -o exec_file
~$ ./exec_file
The -o option means that the compiler write the resulting binary into exec_file.
What a compiler (in your case gcc) does, is create a binary, which is a executable file created from your source code file.

"Too few arguments" error trying to run my compiled program

I'm trying to code to refresh my memory preparing myself for a course.
int main(){
int x;
for( x = 0;x < 10; x++){
printf("Hello world\n");
}
return 0;
}
But when I tried to run this I get Too few arguments
I compiled the code above using gcc -o repeat file.c Then to run this I just type repeat
Sorry if this was a stupid question, it has been a while since I took the introduction class.
When you type
filename
at a prompt, your OS searches the path. By default, Linux doesn't include the current directory in the path, so you end up running something like /bin/filename, which complains because it wants arguments. To find out what file you actually ran, try
which filename
To run the filename file gcc created in the working directory, use
./filename
Your code compiles fine. Try:
gcc -o helloworld file.c
./helloworld
UPDATE :
Based on more recent comments, the problem is that the executable is named repeat, and you're using csh or tcsh, so repeat is a built-in command.
Type ./repeat rather than repeat.
And when asking questions, don't omit details like that; copy-and-paste your source code, any commands you typed, and any messages you received.
The executable is named file, which is also a command.
To run your own program, type
./file
EDIT :
The above was an educated guess, based on the assumption that:
The actual compilation command was gcc file.c -o file or gcc -o file file.c; and
The predefined file command (man file for information) would produce that error message if you invoke it without arguments.
The question originally said that the compilation command was gcc file.c; now it says gcc -o filename file.c. (And the file command prints a different error message if you run it without arguments).
The correct way to do this is:
gcc file.c -o filename && ./filename
(I'd usually call the executable file to match the name of the source file, but you can do it either way.)
The gcc command, if it succeeds, gives you an executable file in your current directory named filename. The && says to execute the second command only if the first one succeeds (no point in trying to run your program if it didn't compile). ./filename explicitly says to run the filename executable that's in the current directory (.); otherwise it will search your $PATH for it.
If you get an error message Too few arguments, it's not coming from your program; you won't see that message unless something prints it explicitly. The explanation must be that you're running some other program. Perhaps there's already a command on your system called filename.
So try doing this:
gcc file.c -o filename && ./filename
and see what happens; it should run your program. If that works, try typing just
filename
and see what that does. If that doesn't run your program, then type
type -a filename
or
which filename
to see what you're actually executing.
And just to avoid situations like this, cultivate the habit of using ./whatever to execute a program in the current directory.

Resources