Difference between revisions of "Compiling in MacOS X"

From OSUPDOCS
Jump to navigation Jump to search
Line 66: Line 66:
== MacOS X Lion or Newer Command Line Tools ==
== MacOS X Lion or Newer Command Line Tools ==


Apple choose not to install command line tools on Lion (or newer systems such as Mountain Lion) and makes it hard to access some very useful folders (such as your home &quot;Library&quot; folder). With these changes, you cannot compile [[#Using XCode|<tt>nairn-fea-mpm</tt> code engines]] or [[#Compiling Xerces on MacOS X|<tt>xerces</tt>]] or until you install command line tools. For Lion (10.7) and newern, you can do the following:
Apple choose not to install command line tools on Lion (or newer systems such as Mountain Lion) and makes it hard to access some very useful folders (such as your home &quot;Library&quot; folder). With these changes, you cannot compile [[#Using XCode|<tt>nairn-fea-mpm</tt> code engines]] or [[#Compiling Xerces on MacOS X|<tt>xerces</tt>]] or until you install command line tools. For Lion (10.7) and newer, you can do the following:


# You will need to install XCode (even if you do not plan to use it) and in addition install command line tools. These steps keep getting harder with each new MacOS update. For up to date information on installing XCode and command lines tools, see [http://guide.macports.org/#installing.xcode MacPorts] instructions. You can also get the command line tools from the [https://developer.apple.com/downloads/index.action Apple developer website] and search for command line tools. You may even need to be signed up as an Apple developer.
# You will need to install XCode (even if you do not plan to use it) and in addition install command line tools. These steps keep getting harder with each new MacOS update. For up to date information on installing XCode and command lines tools, see [http://guide.macports.org/#installing.xcode MacPorts] instructions. You can also get the command line tools from the [https://developer.apple.com/downloads/index.action Apple developer website] and search for command line tools. You may even need to be signed up as an Apple developer.

Revision as of 12:17, 25 February 2015

This page explains several methods to compile code engines using MacOS X.

Using XCode

Most development of NairnMPM and NairnFEA is done in MacOS X and thus compiling on MacOS X is easy. The preferred method is to use XCode (but first you must fix its compiler problem). A complete XCode project is located at

nairn-mpm-fea/Common/Projects/NairnMPM.xcodeproj

This project is called NairnMPM, but it includes all MPM and FEA source code in two targets named NairnMPM and NairnFEA. Once xerces is installed, open the NairnMPM.xcodeproj to compile the code by:

  1. Select the target to compile - either NairnMPM or NairnFEA
  2. Choose build and they will compile and be saved in XCode's derived data folder.

If a linking error occurs, you might have conflicting architectures between the XCode settings and the xerces library you installed. To fix this problem edit both the project and target settings and under the "Architectures" section, set the "Architectures" option to match the architecture you used when installing xerces or recompile xerces if needed.

Compiling Xerces on MacOS X

Before you can compile and run the project, however, you will need an installed version of the xerces library and a copy of the xerces header files. These compiling instructions assume they are installed in the default locations for MacOS X or at:

/usr/local/lib/libxerces-c.dylib

for the library and at

/usr/local/include

for the header files. These can be changed if needed by editing the project and target settings. Since revision 274 of the nairn-mpm-fea project, this library must be xerces 3.0 or newer; prior to that revision, it had be xerces 2.8 or older. These libraries can be obtained by downloading the xerces source code from the Apache Software Foundation web site and then building and installing xerces with the following steps (after seeing potential startup issues on Lion or newer systems):

  1. Open Terminal app and navigate to the xerces source folder expanded from the downloaded file.
  2. Configure the code with the command:
    ./configure CFLAGS="-arch x86_64" CXXFLAGS="-arch x86_64"
    where the provided arch (or architecture) is the desired option for your machine. The main Mac options are i386 for 32 bit Intel processors, x86_64 for 64 bit Intel processors, ppc for 32 bit PowerPC chips, or ppc64 for 64 bit PowerPC64 chips. Although i386 will work on any Intel processor, you should use x86_64 if you have a 64 bit processor, because the code will run faster. To determine how many bits your Intel chip has, chose "About This Mac" from the Apple menu, click "More Info..." button, and look up the processor name in the hardware section. The "Intel Core Solo" and "Intel Core Duo" are 32 bit chips and most others are 64 bit chips.
  3. When the configuration is done, use the following commands:
    cd src
    make
    make install
    These commands make the library (but not the unneeded xerces examples). The final install command after make is done, installs both the library and the header files at the default locations listed above. If the make install shows a permission error, you can solve that by using sudo make install instead and provide your administrator password.
  4. When working with nairn-mpm-fea on a new Mac, the xerces installation only needs to be done once. The only reason to repeat it is when a new xerces version is available and/or the project requires a new version for compatibility.

MacOS X Command Line Compiling

It is easy to compile on MacOS X using a command line approach (after seeing potential startup issues on Lion or newer systems and after updating the compiler). First (and first time only) install xerces and its header files as explained above and make sure they are in the specified default locations. Once xerces is ready, you can compile NairnMPM using:

cd nairn-mpm-fea/NairnMPM/build
make SYSTEM=mac

and compile NairnFEA using:

cd nairn-mpm-fea/NairnFEA/build
make SYSTEM=mac

All source code will be compiled and the executables will be installed in nairn-mpm-fea/NairnMPM/input or nairn-mpm-fea/NairnFEA/input, respectively. You can use an additional make install command to copy each compiled executable to your ~/bin folder if desired.

If the command-line compile does not work, the most likely explanation is a problem with the xerces installation. You either have to install it as specified above or edit the makefile to recognize your custom installation. The process is documented in the makefile and involves editing the xercesSo and headersPath variables for your different settings.

You can pass additional parameters to the make command to alter the compilation process. See comments in the makefile for all the latest options.

Using Eclipse IDE

Because the Eclipse IDE can run on multiple platforms, you can use it on Mac OS X for compiling as well. For details, see help on compiling using Eclipse IDE.

MacOS X Lion or Newer Command Line Tools

Apple choose not to install command line tools on Lion (or newer systems such as Mountain Lion) and makes it hard to access some very useful folders (such as your home "Library" folder). With these changes, you cannot compile nairn-fea-mpm code engines or xerces or until you install command line tools. For Lion (10.7) and newer, you can do the following:

  1. You will need to install XCode (even if you do not plan to use it) and in addition install command line tools. These steps keep getting harder with each new MacOS update. For up to date information on installing XCode and command lines tools, see MacPorts instructions. You can also get the command line tools from the Apple developer website and search for command line tools. You may even need to be signed up as an Apple developer.
  2. You may need to agree to XCode license by using Terminal app to enter
  3. Most tools should now be available. You should be able to make and install the xerces project and then use the provided XCode project to build OSParticulas, NairnMPM, and NairnFEA.

With each now system, Apple seems to creating road blocks to keep you from using you computer for interesting programming. For now, by using a few tricks it is still possible to get around their road blocks. Hopefully these tricks will continue to work in the future.

One issue in the XCode project is that it might not find your xerces library, even if it is in the standard location. If it is not found, the library will be in red in the "External Frameworks and Libraries" folder. You have to select the library, view its properties, and then click to set the path on your computer. The problem is that you cannot navigate to the /usr/local/lib folder anymore (another new Apple "feature"). Here is a trick to get there:

  1. In the Finder, use the "Go to Folder..." menu command and enter "/usr/local".
  2. After it opens, choose the "Add To Sidebar" menu command. This folder will now appear in all file selection boxes.
  3. Go back to XCode and enter box to select the path the the xerces library.
  4. Go through your new "local" folder and select the xerces library file in /usr/local/lib.

MacOS X Compiler

The "Command Line Tools" installed by XCode include only Apple-approved compilers. Unfortunately, it seems none of their recent compilers support OpenMP used to make the code engines parallel. On some Macs, code compiled with the default compiler will randomly crash when run in parallel. On others, the code will not even compile (getting a load area due to OpenMP code). The two solutions are to compile without OpenMP (and lose all advantages of multiprocessor execution) or to install and unapproved GCC compiler.

To compile without OpemMP, simply comment on the line

#include <OpenMP>

in the MPMPrefix.hpp file and change the compile to an Apple-approved compiler.

The preferred solution is to switch to a compiler that supports OpenMP such as GCC 4.8 or newer. Using and unsupported compiler in Xcode, however, requires a hack. The following steps work throughMountain Lion (10.8) with XCode 4.x. They were not tested in Mavericks (10.9) and I cannot get them to work in Yosemite (10.10) with XCode 6.x. The steps are:

  • Install newer version of GCC, such as GCC 4.8. A good way is to install using MacPorts. Once that is tool is installed, the installation of GCC is easy, as explained in this blog. In brief, all you need is to open Terminal app and enter
    sudo port selfupdate
    sudo port install gcc48
    
    The first command above is only need to make sure your MacPorts is up to date.
  • The previous step will install the compiler at
    /opt/local/bin/g++-mp-4.8
    
    For command line compiling, the first step is enough. The makefiles in the build folders assumes this path when command-line compiling on a Mac.
  • To use this new compiler in XCode, you have to hack XCode to allow it. This hack means creating a new XCode plug-in defining your newly installed GCC compiler. The simplest approach is to use the plug we created by following thes simple steps:
    1. Download this GCC 4.8 XCode plug in.
    2. Expand and copy the file to the folder /Library/Application Support/Developer/Shared/Xcode/Plug-ins/. But, if that folder is not there, you have to create it first in Terminal app with:
      sudo mkdir -p "/Library/Application Support/Developer/Shared/Xcode/Plug-ins/"
      
    3. In your Xcode project under "Other Warning Flags" remove the -Wmost option (if needed).
    4. You should now be able to select GCC 4.8 from the compiler pop-up menu and compile code using that compiler.
  • If the above download and copy does not work (e.g., you are using a different version of GCC or you installed it at a different location), you can fairly easily create your own hacked plug in. The process is explained on this blog. Here are the steps from that blog (see the blog if you need more detail) done using the Terminal app:
    1. Go to XCode's plug-ins folder, which is in the application package:
      cd /Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents\
      /SharedSupport/Developer/Library/Xcode/Plug-ins
      
    2. Create a copy of "GCC 4.2.xcplugin", put it in the Xcode plugin folder (create that folder if needed), and go to its contents:
      sudo mkdir -p "/Library/Application Support/Developer/Shared/Xcode/Plug-ins/"
      sudo cp -r "GCC 4.2.xcplugin" "/Library/Application Support/Developer/Shared\
      /Xcode/Plug-ins/GCC 4.8.xcplugin"
      cd "/Library/Application Support/Developer/Shared/Xcode/Plug-ins/GCC 4.8.xcplugin/Contents"
      
    3. Convert the binary plist into text xml and then open for editing (done with vi here, but could use another tool):
      sudo plutil -convert xml1 Info.plist
      sudo vi Info.plist
      
    4. Make the following changes:
      "com.apple.xcode.compilers.gcc.42" -> "com.apple.xcode.compilers.gcc.48"
      "GCC 4.2 Compiler Xcode Plug-in" -> "GCC 4.8 Compiler Xcode Plug-in"
      
    5. Save and convert Info.plist back to binary:
      sudo plutil -convert binary1 Info.plist
      
    6. In the "Resources" folder rename two files:
      cd Resources/
      sudo mv GCC\ 4.2.xcspec GCC\ 4.8.xcspec
      cd English.lproj/
      sudo mv GCC\ 4.2.strings GCC\ 4.8.strings
      
    7. Open the "GCC 4.8.xcspec" for editing (e.g., using sudo vi) and make the changes:
      Identifier = "com.apple.compilers.gcc.4_8";
      Name = "GCC 4.8";
      Description = "GNU C/C++ Compiler 4.8";
      Version = "4.8";
      ExecPath = "gcc-mp-4.8";
      ShowInCompilerSelectionPopup = YES;
      IsNoLongerSupported = NO;
      
    8. Down further in that file, make the following changes:
      under Name = "GCC_ENABLE_PASCAL_STRINGS"; set DefaultValue = NO;
      under Name = "GCC_CW_ASM_SYNTAX"; set DefaultValue = NO;
      
    9. In your Xcode project under "Other Warning Flags" remove the -Wmost option (if needed).
    10. You should now be able to select GCC 4.8 from the compiler pop-up menu and compile code using that compiler.