LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2013, 06:51 PM   #1
malxxxl
LQ Newbie
 
Registered: Feb 2013
Posts: 10

Rep: Reputation: Disabled
EMACS - C program syntax error?


I'm trying to run a simple hello world on emacs and I get a syntax error for some reason. I have linux running on virtual machine, while I have eclipse on windows 7 and running a simple hello world without any syntax error.

Why is emacs not compiling the c program?

I run both on eclipse and emacs and get an error on emacs only.


#include <stdio.h>

int main (void ){

printf("Hello world");
return 0;
}



------------------------------------------------------------------------

when running on emacs I get this error:


Line 3: Syntax error near unexpected token '('
line 3: 'int main (void)'


what can I do to run this on emacs??
 
Old 02-22-2013, 10:30 PM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,645

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
emacs is a text editor (think of it like an advanced Notepad), not an IDE. Just save the file and compile it using gcc on the command line.

How did you make the file? Did you just copy it straight over from Windows? You could have windows line terminators screwing things up.

Last edited by suicidaleggroll; 02-22-2013 at 10:31 PM.
 
1 members found this post helpful.
Old 02-22-2013, 10:57 PM   #3
malxxxl
LQ Newbie
 
Registered: Feb 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
oh ok! I thought emacs was powerful enough to actually compile the program.

I did not copy and paste it from windows. I typed the entire hello world code on emacs. I called it run.c

Now, you mention I should just save the file.

next you say compile using gcc ....

so I would type:

"gcc run.c" on terminal?? or what would be the correct way of compiling it so I can actually see the program run.

thanks for the clarification very helpful.
 
Old 02-23-2013, 03:01 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,256
Blog Entries: 4

Rep: Reputation: 259Reputation: 259Reputation: 259
With gcc run.c, your output compiled will be "a.out"
If you want give a name for your output, try:
Code:
gcc run.c -o name_that_you_want
 
1 members found this post helpful.
Old 02-23-2013, 04:28 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,600

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
You could also try this:

Code:
echo 'CFLAGS = -g -W -Wall -Wextra -Werror' >Makefile
echo 'LDFLAGS = -g' >>Makefile
make run && ./run
 
Old 02-23-2013, 12:26 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
IIRC, EMACS can be used as an IDE. Unfortunately, it's been fifty years now since I last actively used EMACS (on the MIT MULTICS - from which UNIX was derived - operating system running on an IBM 9000 series time-shared mainframe), so I can't offer details. Basically, with your code in a buffer, you invoke the compiler and capture the output in another buffer. All done with the appropriate key combinations.

I just installed emacs on this old laptop, and a <alt>x compile suggested that a make command is available, but I didn't take the time to find out the key combination to which that command was bound.

Running emacs from inside a GUI desktop created a default window and an option to look at the tutorial. You might find reading the tutorial instructive.
 
1 members found this post helpful.
Old 02-23-2013, 06:46 PM   #7
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,645

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
Quote:
Originally Posted by malxxxl View Post
next you say compile using gcc ....

so I would type:

"gcc run.c" on terminal??
Pretty much, yeah. There are a LOT of options you can give gcc though, such as specifying your desired output file name with -o as kooru described, adding warnings (-W, -Wall, -Wextra) and gnu debugging flags (-g) as NevemTeve described, etc.

Code:
$ cat run.c
#include <stdio.h>

int main (void ){

        printf("Hello world\n");
        return 0;
}

$ gcc run.c
$ ./a.out
Hello world
$

Last edited by suicidaleggroll; 02-23-2013 at 06:48 PM.
 
Old 02-23-2013, 11:09 PM   #8
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
It's definitely fair to call Emacs an IDE, once you learn all the stuff it can do to help you code. But anyway...

The compile command is useful for a few reasons. For example:

Code:
M-X compile <RET> gcc myprog.c <RET> (or C-c o)
If you do it this way, emacs will open a second window, which will display the compiling process and any errors. If there are any errors, you can use the C-` command, and emacs will automatically move the cursor to the place in the code where the error occurred.

Use the C-x 1 command to close the compiler window.

In languages like Haskell that have any interpreter shell, you can usually use the C-c C-l command, and it will load the file you are editing into the interpreter, and open up an interpreter shell within emacs itself.

To learn more about all the cool stuff Emacs can do, use the command C-h r.

(I'm using Emacs 24.2.1. You might be using version 23 which has less features.)
 
Old 02-23-2013, 11:11 PM   #9
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
Oh, and just have to say it...

"Emacs, operating system of the future!"

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing calculator program in lex and yacc but getting syntax error Avanti Linux - Newbie 1 10-27-2011 10:27 AM
Help with compiling C program, syntax error MrUmunhum Programming 7 11-26-2008 02:07 PM
Emacs Font error Syntax Highlighting flebber Linux - Software 11 05-21-2007 08:56 AM
Emacs Syntax highlight UltraSoul Linux - Software 1 07-11-2005 09:19 AM
syntax highlighting for php in emacs bm1 Linux - Software 1 01-23-2004 10:32 PM


All times are GMT -5. The time now is 04:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration