简体   繁体   中英

Moving away from XCode to GCC on OS X

I have a code base that I build on Snow Leopard, XCode 3.2.6 and targeting the 10.5 SDK 32/64bit, INTEL only. Works fine. I do this in a virtual machine.

Now I have GCC 4.7.1 compiled and working on Mountain Lion, XCode 4.4 tools installed so that I can have all the necessary libraries, dependencies.

I'd like to move away from XCode to GCC targeting the 10.5 SDK still or even the 10.6 SDK would be fine.

Do I have to create a MakeFile? Is there someway to export one from XCode? I dont know a lot about creating a MakeFile from scratch.

I know that I can append -mmacosx-version-min=10.5 to the gcc command and that I have to tell it 64-bit as well.

You're confusing multiple different things.

If you want to switch from Xcode to vim, you can just… do it. If you've installed the Xcode command-line tools, you've got gcc, make, etc. all there on your path, and they'll work fine, and you never have to think about Xcode at all. In fact, you can even install the command-line tools without the rest of Xcode.

If you want to switch from the Xcode toolchain to a different toolchain, or use a hybrid, you can just do that to. You've installed gcc 4.7.1. OK, now use gcc_select , or export CC=/usr/local/bin/gcc-4.7.1 , or hard-code the path in your makefiles, or whatever you want. It's not different than having multiple toolchains on any other POSIX platform.

If you want to build Xcode projects without running Xcode, you can use xcodebuild from the command line.

If you want convert existing projects from Xcode to Makefiles (or cmake or whatever), you'll have to do that manually. Usually it's not all that hard, unless the project is very complex or does a lot of Xcode-specific things. You can see all the actual commands Xcode is running from the build output.

If you want to learn how to create Makefiles, there are lots of good tutorials out there. But the first question is: why? Would it be acceptable to learn something easier, like cmake, or is the whole goal here to learn about make?

If you want to do SDK-based development, pass -arch , and use other Apple-specific extensions to gcc with gcc 4.7.1… well, you can't. Those extensions to gcc haven't been ported to any newer versions. If you want to do the work to port them yourself, I'm sure there are people who'd love it, but that's a lot of work.

And if you want to build other people's code with gcc 4.7.1, there's a good chance that isn't going to work out of the box either. Most Mac-specific open source projects depend on the Apple gcc extensions, and most cross-platform open source projects have autotools setups that will detect that you're on a Mac and have Xcode command-line tools and set themselves up to depend on those extensions too.

So, which of these are you having a problem with?


One more thing:

The macosx-version-min setting is a completely different, and complementary, thing to the SDK.

To set the SDK to, say, 10.5, you have to pass -isystem $PATH_TO_10_5_SDK/usr/include , and possibly additional -isystem flags for other directories under /usr (eg, for C++ stdlib), and sometimes -L flags to the linker, and sometimes -F flags to the compiler and/or linker.

You also have to have the SDK. The only way to get a 10.5 SDK is with Xcode 3.2.6 or older—which I don't think you can even install on Mountain Lion. Also, the 10.5 SDK from Xcode 3.2.6 doesn't know how to work on build systems later than 10.6, so you have to hack it up manually (mainly creating a bunch of symlinks that cause it to treat 10.7 and 10.8 the same as 10.6). And if you're using a non-Apple gcc, it will also get confused trying to find various things like the compiler-specific headers (especially for C++), so you need to hack it up even more.

Why is this so hard? Because you almost never need to do it, and Apple specifically recommends that you don't. It's left over from the way they used to recommend doing things back in the 10.4 days—but just because it's there doesn't mean you should use it.

The right way to build code that runs on 10.5 is the -macosx-version-min=10.5 . You can, and in fact should, use it with the SDK that matches your OS (or possible even a newer SDK, if you want to be able to use features in the next OS version). Again, this flag may only be supported with clang and Apple-extended gcc. But it makes everything in the SDK magically work in a backward-compatible mode. Except, of course, for new features that were added in 10.6 or later—if you try to go to fullscreen mode on a 10.5 machine, you'll just get an exception or crash. Apple has pretty good documentation on which APIs were added with which version, and on the right way to check for each kind of API (Cocoa, CoreFoundation, and POSIX). And of course you'll need a real 10.5 machine (or VM) to test on. Also, there are a few bugs with the C++ stdlib (10.5 only), and a handful of libs like OpenSSL where Apple no longer recommends anyone build against the system copy for distribution. But it's still much easier than trying to build with the 10.5 SDK. And it's what Apple explicitly recommends.

如果要避免在Xcode中工作,而又想以相同的方式构建事物,则可以使用命令行工具xcodebuild ,它将以与Xcode相同的方式构建Xcode项目。

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