将GCC 4.2设置为Mac OS X Leopard上的默认编译器

时间:2021-03-26 07:01:04

I'm sure there must be a way to do this. As you are probably aware the latest versions of Xcode (and in fact I think all versions of Xcode) on Leopard come with GCC 4.0.1 and GCC 4.2. GCC 4.0.1 is the default system compiler while GCC 4.2 is an optional compiler you can set in the Xcode project settings.

我敢肯定必须有办法做到这一点。您可能已经知道Leopard的最新版Xcode(事实上我认为Xcode的所有版本)都带有GCC 4.0.1和GCC 4.2。 GCC 4.0.1是默认的系统编译器,而GCC 4.2是可以在Xcode项目设置中设置的可选编译器。

Does anyone know how to set GCC 4.2 as the default compiler for all options? Preferably command line use as well as configure scripts still use GCC 4.0.1 rather than GCC 4.2 no matter what I do in Xcode. I'm assuming it is simply a case of changing a path variable or some such but I am stumped on this one.

有谁知道如何将GCC 4.2设置为所有选项的默认编译器?无论我在Xcode中做什么,最好是命令行使用以及配置脚本仍然使用GCC 4.0.1而不是GCC 4.2。我假设它只是一个改变路径变量或一些这样的情况但我被困在这一个。

Any help is appreciated. Thanks.

任何帮助表示赞赏。谢谢。

6 个解决方案

#1


Command line usage for all configure scripts:

所有配置脚本的命令行用法:

  cd /usr/bin
  rm cc gcc c++ g++
  ln -s gcc-4.2 cc
  ln -s gcc-4.2 gcc
  ln -s c++-4.2 c++
  ln -s g++-4.2 g++

Make a record of the current link targets, so you can restore them if you want to.

记录当前的链接目标,以便您可以根据需要进行恢复。

If you don't want to change the system wide settings, add a directory into PATH before /usr/bin (say, $HOME/bin), and make the symlinks there

如果您不想更改系统范围的设置,请在/ usr / bin(比如$ HOME / bin)之前将目录添加到PATH中,并在那里创建符号链接

I haven't tested whether this affects Xcode projects, since I don't use Xcode (only command line).

我没有测试这是否会影响Xcode项目,因为我不使用Xcode(只有命令行)。

#2


In the Project or Target Info Window set the build setting "C/C++ compiler version" (GCC_VERSION).

在项目或目标信息窗口中,设置构建设置“C / C ++编译器版本”(GCC_VERSION)。

Or in the Target Info Window you can change the "System C rule" to your favorite GCC version.

或者在目标信息窗口中,您可以将“系统C规则”更改为您喜欢的GCC版本。

Update: Regarding the command line I would leave to Leopard the decision of what should be the default compiler. If you want to use a different compiler with tools like Autotools configure you had better to define the CC variable.

更新:关于命令行,我将留给Leopard决定什么应该是默认编译器。如果要使用与Autotools配置等工具不同的编译器,最好定义CC变量。

CC=gcc-4.2 ./configure

or

export CC=gcc-4.2

#3


Since neither Apple nor Darwin Ports have the gcc_select program to change the default version of the System compiler (as exists on GNU/Linux), I would like to be on the safe side with XCode (and the rest of the system) and would recommend to leave the symbolic links as they are and instead setup environment variables that overrides which version of GCC to use.

既然Apple和Darwin Ports都没有gcc_select程序来更改系统编译器的默认版本(如GNU / Linux上那样),我想安全地使用XCode(以及系统的其余部分)并建议保留符号链接,而不是设置环境变量,以覆盖要使用的GCC版本。

In my .profile file I have the following

在我的.profile文件中,我有以下内容

export CC=/usr/bin/gcc-4.2
export CPP=/usr/bin/cpp-4.2
export CXX=/usr/bin/g++-4.2 

And I successfully compiled the following libraries with GCC 4.2 from source.

我从源代码中成功地使用GCC 4.2编译了以下库。

  • OpenSSL
  • libjpeg
  • libpng
  • zlib
  • gst

However... I could not get Boost 1.39 to acknowledge the environment variables, so to compile Boost with GCC 4.2 I needed to change the symbolic links in /usr/bin/ so they pointed to gcc v4.2

但是...我无法获得Boost 1.39来确认环境变量,所以要用GCC 4.2编译Boost我需要更改/ usr / bin /中的符号链接,所以他们指向gcc v4.2

After the long while the Boost libraries were finished compiling with GCC 4.2 I restored the symbolic links back to the original System version gcc-4.0.

很长一段时间Boost库完成了使用GCC 4.2的编译后,我将符号链接恢复回原始的系统版本gcc-4.0。

#4


Im my experience (limited), changing CC in .profile does not change Lion's (10.7.2) defaulting to i686-apple-darwin11-llvm-gcc-4.2. I wonder if this has anything to do with Apple's own sym linking: a partial: ls -la /usr/bin | grep .*gcc.* :

我的经验(有限),改变.profile中的CC并没有改变Lion的(10.7.2)默认为i686-apple-darwin11-llvm-gcc-4.2。我想知道这是否与Apple自己的sym链接有关:部分:ls -la / usr / bin | grep。* gcc。*:

lrwxr-xr-x     1 root   wheel        12 25 oct 19:31 cc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   wheel        12 25 oct 19:31 gcc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   admin        32 25 oct 19:31 llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2

I am wary about breaking these and adding my own to usr/bin/gcc-4.2 per Martin v. Löwis's answer.

根据Martinv.Löwis的回答,我对打破这些并将其自己添加到usr / bin / gcc-4.2时我很谨慎。

#5


Since I need to build things where CC env is ignored and I end up switching often, I wrote a simple minded gcc_select in Python. Thought I may as well post it here. Invoke it with arg either 4.0 or 4.2 or no arg to see current symlinks. Would need modification if you have versions other than 4.0 and 4.2:

由于我需要构建忽略CC env的东西并且我最终经常切换,我在Python中编写了一个简单的gcc_select。以为我也可以在这里发布。使用arg 4.0或4.2调用它或不调用arg来查看当前的符号链接。如果您的版本不是4.0和4.2,则需要修改:

#!/usr/bin/python
import sys
import os

os.chdir('/usr/bin')

files = ['cc', 'gcc', 'c++', 'g++']

if '4.0' in sys.argv:
  ver = '4.0'
elif '4.2' in sys.argv:
  ver = '4.2'
else:
  print "Unknown gcc version.  Current setting:"
  os.system('ls -al %s' % ' '.join(files))
  sys.exit(1)

os.system('rm %s' % ' '.join(files))  
for f in files:
  os.system('ln -s %s-%s %s' % (f, ver, f))

print "Changed to gcc version %s" % ver

#6


I might be wrong, but I thought that was what Xcode-select was for?

我可能错了,但我认为那是Xcode-select的用途?

xcode-select --switch /path_to_tool_suite

As I said, I'm not 100% on this, but I believe it will set the default for all programs including Terminal and other apps that calls into the OS to use a compiler.

正如我所说,我不是百分之百,但我相信它会设置所有程序的默认设置,包括终端和其他调用操作系统的应用程序使用编译器。

#1


Command line usage for all configure scripts:

所有配置脚本的命令行用法:

  cd /usr/bin
  rm cc gcc c++ g++
  ln -s gcc-4.2 cc
  ln -s gcc-4.2 gcc
  ln -s c++-4.2 c++
  ln -s g++-4.2 g++

Make a record of the current link targets, so you can restore them if you want to.

记录当前的链接目标,以便您可以根据需要进行恢复。

If you don't want to change the system wide settings, add a directory into PATH before /usr/bin (say, $HOME/bin), and make the symlinks there

如果您不想更改系统范围的设置,请在/ usr / bin(比如$ HOME / bin)之前将目录添加到PATH中,并在那里创建符号链接

I haven't tested whether this affects Xcode projects, since I don't use Xcode (only command line).

我没有测试这是否会影响Xcode项目,因为我不使用Xcode(只有命令行)。

#2


In the Project or Target Info Window set the build setting "C/C++ compiler version" (GCC_VERSION).

在项目或目标信息窗口中,设置构建设置“C / C ++编译器版本”(GCC_VERSION)。

Or in the Target Info Window you can change the "System C rule" to your favorite GCC version.

或者在目标信息窗口中,您可以将“系统C规则”更改为您喜欢的GCC版本。

Update: Regarding the command line I would leave to Leopard the decision of what should be the default compiler. If you want to use a different compiler with tools like Autotools configure you had better to define the CC variable.

更新:关于命令行,我将留给Leopard决定什么应该是默认编译器。如果要使用与Autotools配置等工具不同的编译器,最好定义CC变量。

CC=gcc-4.2 ./configure

or

export CC=gcc-4.2

#3


Since neither Apple nor Darwin Ports have the gcc_select program to change the default version of the System compiler (as exists on GNU/Linux), I would like to be on the safe side with XCode (and the rest of the system) and would recommend to leave the symbolic links as they are and instead setup environment variables that overrides which version of GCC to use.

既然Apple和Darwin Ports都没有gcc_select程序来更改系统编译器的默认版本(如GNU / Linux上那样),我想安全地使用XCode(以及系统的其余部分)并建议保留符号链接,而不是设置环境变量,以覆盖要使用的GCC版本。

In my .profile file I have the following

在我的.profile文件中,我有以下内容

export CC=/usr/bin/gcc-4.2
export CPP=/usr/bin/cpp-4.2
export CXX=/usr/bin/g++-4.2 

And I successfully compiled the following libraries with GCC 4.2 from source.

我从源代码中成功地使用GCC 4.2编译了以下库。

  • OpenSSL
  • libjpeg
  • libpng
  • zlib
  • gst

However... I could not get Boost 1.39 to acknowledge the environment variables, so to compile Boost with GCC 4.2 I needed to change the symbolic links in /usr/bin/ so they pointed to gcc v4.2

但是...我无法获得Boost 1.39来确认环境变量,所以要用GCC 4.2编译Boost我需要更改/ usr / bin /中的符号链接,所以他们指向gcc v4.2

After the long while the Boost libraries were finished compiling with GCC 4.2 I restored the symbolic links back to the original System version gcc-4.0.

很长一段时间Boost库完成了使用GCC 4.2的编译后,我将符号链接恢复回原始的系统版本gcc-4.0。

#4


Im my experience (limited), changing CC in .profile does not change Lion's (10.7.2) defaulting to i686-apple-darwin11-llvm-gcc-4.2. I wonder if this has anything to do with Apple's own sym linking: a partial: ls -la /usr/bin | grep .*gcc.* :

我的经验(有限),改变.profile中的CC并没有改变Lion的(10.7.2)默认为i686-apple-darwin11-llvm-gcc-4.2。我想知道这是否与Apple自己的sym链接有关:部分:ls -la / usr / bin | grep。* gcc。*:

lrwxr-xr-x     1 root   wheel        12 25 oct 19:31 cc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   wheel        12 25 oct 19:31 gcc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   admin        32 25 oct 19:31 llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2

I am wary about breaking these and adding my own to usr/bin/gcc-4.2 per Martin v. Löwis's answer.

根据Martinv.Löwis的回答,我对打破这些并将其自己添加到usr / bin / gcc-4.2时我很谨慎。

#5


Since I need to build things where CC env is ignored and I end up switching often, I wrote a simple minded gcc_select in Python. Thought I may as well post it here. Invoke it with arg either 4.0 or 4.2 or no arg to see current symlinks. Would need modification if you have versions other than 4.0 and 4.2:

由于我需要构建忽略CC env的东西并且我最终经常切换,我在Python中编写了一个简单的gcc_select。以为我也可以在这里发布。使用arg 4.0或4.2调用它或不调用arg来查看当前的符号链接。如果您的版本不是4.0和4.2,则需要修改:

#!/usr/bin/python
import sys
import os

os.chdir('/usr/bin')

files = ['cc', 'gcc', 'c++', 'g++']

if '4.0' in sys.argv:
  ver = '4.0'
elif '4.2' in sys.argv:
  ver = '4.2'
else:
  print "Unknown gcc version.  Current setting:"
  os.system('ls -al %s' % ' '.join(files))
  sys.exit(1)

os.system('rm %s' % ' '.join(files))  
for f in files:
  os.system('ln -s %s-%s %s' % (f, ver, f))

print "Changed to gcc version %s" % ver

#6


I might be wrong, but I thought that was what Xcode-select was for?

我可能错了,但我认为那是Xcode-select的用途?

xcode-select --switch /path_to_tool_suite

As I said, I'm not 100% on this, but I believe it will set the default for all programs including Terminal and other apps that calls into the OS to use a compiler.

正如我所说,我不是百分之百,但我相信它会设置所有程序的默认设置,包括终端和其他调用操作系统的应用程序使用编译器。