简体   繁体   中英

Emacs shell has different behavior from Terminal with GCC

I'm working on a C++ project that requires GCC 4.7 or later. When I call make on the command line in Terminal, it works fine, but it fails when I run Mx compile in Emacs.

When I run gcc --version on the command line, it says

gcc (MacPorts gcc47 4.7.3_0+universal) 4.7.3

But when I run gcc --version from the shell within Emacs, it says

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

I did a little more investigation, and found another difference between Emacs and Terminal. I installed GCC 4.7 using MacPorts on the Terminal; but when I run port install foo in Emacs, it says port: command not found .

Why does this happen, and how can I get Emacs to use the same version of GCC as Terminal?

Mx shell starts a new shell. For me, it's a bash shell, but it could be csh or zsh or some other shell. When emacs launches a new shell, it runs ~/.emacs_SHELL where SHELL is bash or csh or something. So you can copy your shell startup file to that file and it should hopefully get your path and other environment variables set up correctly.

In my case, I'm a bash user, so I copied by .profile to .emacs_bash and it worked. You might have a .bash_profile or .bashrc instead of a .profile. But the general idea is the same: put emacs shell startup commands in a .bash_WHATEVER file.

Whether you launch gcc in Terminal or via Mx compile , the location of gcc executable is determined by PATH environment variable (unless it is configured in the Makefile - but if it were the case, you wouldn't have the problem you are describing). So, first of all check it in the Terminal:

echo $PATH

You can double-check the location of the executable using

which gcc

Then, in emacs,

(getenv "PATH")

That is what make will see when emacs launches it via Mx compile . Change the value to what you got in Terminal using setenv . You can also change emacs's own variable exec-path to be in sync with your PATH . That variable is a list of strings (=paths), add paths to it using add-to-list .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM