Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:36:25+00:00 2026-06-01T12:36:25+00:00

I am trying to set up Emacs so that when I choose Compile.. in

  • 0

I am trying to set up Emacs so that when I choose “Compile..” in the menu, Emacs executes “make filename” on the command line. I am trying something like:

(setq compile-command (concat "make " (file-name-sans-extension
                                       buffer-file-name)))

but it doesn’t work – it looks like Emacs is is looking for a file-name for the *scratch* buffer, which doesn’t have one. Does anyone know how to fix this?

Thanks.

UPDATE: as suggested by Cheeso, a hook fixes the problem. Here is a version that works for me:

(defun cur-file ()
  (file-name-sans-extension
   (file-name-nondirectory (buffer-file-name (current-buffer)))))

(add-hook 'c++-mode-hook
          (lambda()
            (set (make-local-variable 'compile-command)
                 (concat "make " (cur-file)))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:36:25+00:00Added an answer on June 1, 2026 at 12:36 pm

    Yes – a couple options for you.

    • Simple: define a file-local variable. In the header comment of your file just include something like

       // -*- compile-command: "gcc -Wall -O3 -o f file.c" -*-
      

      For more details, see:
      https://stackoverflow.com/a/4540977/48082

    • More elaborate – there is a module called smarter-compile.el available on the marmalade repo. It allows you to define some rules for guessing the compile-command to use for a particular buffer. Like, if there’s a makefile, use MAKE; otherwise, if the file is a .c file, use gcc; etc.

      This is nice if you don’t want to insert comments into every file, or if you have a myriad of different kinds of files and projects that you work on.

    ps: the reason your simple setq isn’t working, I’d guess, is that you are not evaluating it in the buffer that is being edited. Probably you want to put that into your personal xxxx-mode-hook-fn, where xxx is the mode of the file you are editing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set [Ctrl]-[;] as a keybinding in my .emacs like this
I'm trying to enable auto complete for the command line in emacs mode. When
I am trying to set up .emacs so that any file which begins with
I'm trying to set autosave to use a directory in ./emacs.d/autosaves. I previously used
I want to have a command in Emacs to make it as opaque/transparent as
I'm a clojure noob trying to get emacs & slime set up. There doesn't
I am trying to make shortcuts for portable emacs linking to a portable LaTeX
I'm trying to write a new emacs mode for a new template c-like language,
I'm trying to set up emacs 24 to use with clojure on osx. I've
I'm trying to make emacs' delete-file function delete files with exclamation marks in their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.