29

I am using Emacs 23 and php-mode.el 1.5.0. When I have this in my .emacs:

(require 'php-mode)

I get this error message when Emacs starts:

Warning (initialization): An error occurred while loading `/Users/kdj/.emacs':

error: `c-lang-defconst' must be used in a file

To ensure normal operation, you should investigate and remove the cause of the error in your initialization file. Start Emacs with the `--debug-init' option to view a complete error backtrace.

If I evaluate (require 'php-mode) after Emacs starts, I don't get any error messages.

I found a blog entry which indicates that this problem is specific to Emacs 23 (that is, there is no error with Emacs 22.x), but it doesn't give any solutions.

I don't know if this matters, but I'm using Mac OS X, and I built Emacs from the current CVS sources, using ./configure --with-ns.

What's going on here, and/or how I can fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302

2 Answers2

51

I ran into the same problem when trying to get the csharp-mode up and running. I finally found the solution when digging into the actual Emacs Lisp file for csharp-mode:

;;   This code doesn't seem to work when you compile it, then
;;   load/require in the Emacs file. You will get an error (error
;;   "`c-lang-defconst' must be used in a file") which happens because
;;   cc-mode doesn't think it is in a buffer while loading directly
;;   from the init. However, if you call it based on a file extension,
;;   it works properly. Interestingly enough, this doesn't happen if
;;   you don't byte-compile cc-mode.

So, the quick and dirty fix to put in your .emacs is to auto load on extension and not put (require 'php-mode) or (load "php-mode") in there. Without further ado,

(autoload 'php-mode "php-mode" "Major mode for editing php code." t)
(add-to-list 'auto-mode-alist '("\\.php$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))

I hope this helps! Now I just need to get the PHP/HTML mode switching stuff working. Wish me luck.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Late to the party, but thanks so much for this. I don't have a problem on Linux, but on Windows, I was getting this same error, since nxhtml autoloads a lot of stuff on its own. I deleted my cc-*.elc files, and the errors went away. – monksp Sep 26 '10 at 14:39
  • Thank you. I had the same problem with proto-mode (http://code.google.com/p/protobuf/source/browse/trunk/editors/protobuf-mode.el), and this fixes it. – Anne Jun 27 '11 at 12:23
1

It works fine with http://mewde.googlecode.com/files/php-mode-new.el.

Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94