0

I wrote a very simple program like: ( sorry, I typed the code in the right way, but the display is wired. How could I fix it?)

#include <stdio.h>
int main( void )
{
   int i;

   for ( i = 0; i <= 10; i++ ) {
      printf( "%d hello!\n", i);
   }
   return 0;
}

Usually, I compile c program in terminal with the command

cc -o xxx xxx.c

So in Emacs, when I type M-x compile, I change make -k to cc -o. But I got error like

cc: argument to '-o' is missing

What's the problem?

If I use make, then I still got error

No targets specified and no makefiles found.

Finally, if the above problem is fixed, how could I define a custom hotkey for compile? I have already know how to do something like

global-set-key [f8] 'goto-line

But I don't know to set a hotkey for an action only for c-mode.

user565739
  • 1,302
  • 4
  • 23
  • 46
  • thanks hilal, could you tell me how to fix the display problem? I checked it, but it seems almost the same as I typed. – user565739 Jan 07 '11 at 06:52
  • http://stackoverflow.com/editing-help (if you're not blocking javascript from googleapis.com, then there's a bright orange question mark at the top right of the editing form which links to this). – phils Jan 08 '11 at 23:17

4 Answers4

2

You need to supply the entire cc line.

"make -k" assumes that you have a Makefile supplying the commands to make things.

So, replace make -k with

cc -o xxx xxx.c

As for the emacs binding:

(global-set-key [f6] 'compile)
Hack Saw
  • 2,741
  • 1
  • 18
  • 33
  • thanks. it works great. although it's not necessary, but curious that is it possible to do that: set default compile command to cc -o (I have seen this); press f6, then directly compile the current file (without typing xxx.c) and gives the output file as xxx (don't need to specify the output filename)? ( or how to use make to do similar thing?) – user565739 Jan 07 '11 at 08:27
  • Emacs has a full featured LISP interpetter, so it's possible, and prolly not hard. You might ask about it, or better yet spend some time learning elisp. I can't say off the top of my head, it's been a couple of years since I looked at customization in emacs. – Hack Saw Feb 07 '11 at 21:39
1

A nice way to build simple one file programs like this is to add values for the compile-command variable at the top of the file, like this:

// -*- compile-command:"g++ helloworld.cpp -g -o helloworld.exe"; -*-

There's more detail about this in my blog post "Writing quick C++ programs in emacs without a makefile"

justinhj
  • 11,147
  • 11
  • 58
  • 104
0

try changing make -k to make xxx.

fangzhzh
  • 2,172
  • 21
  • 25
0

I think a good way of doing it is like it is documented for the variable compile-command (which is the one used by compile, normally containing make -k).

Do C-h v compile-command to see the documentation for the variable. It contains some code that you can change to your liking.

Kjell Andreassen
  • 753
  • 6
  • 10