Message Boards Message Boards

3
|
5690 Views
|
0 Replies
|
3 Total Likes
View groups...
Share
Share this post:

The Missing Documentation of Building MathLink project with XCode(4.6.2)

Posted 11 years ago
Because the XCode's interface has been modified drastically since version 3, I could not use the current documentation in version 9 to construct my project. I am writing down this documentation for your reference. Feel free to let me know if you find any error or think improvements can be added.

Goal: Create a C/C++ project that use the Mathematica Kernel

Platform: Mac OSX 10.8+

IDE: XCode 4.6.3

Prerequests: 
  • Mathematica 8+ is installed on your machine and activated. Normally under /Applications.
  • Create a symbolic link to Mathematica Kernel,  e.g.
    sudo ln -s /Applications/Mathematica.app/Contents/MacOS/MathKernel /usr/local/bin/math 
  • You have XCode 4.6.3 installed and Command Line Tools is istalled (gcc is required)
Now buckle up and we are going to start build our very first project in the latest XCode
  1. Launch XCode and choose "create a new XCode project"  from the welcoming window. Or you can do this from Menu bar: File -> New -> Project
  2. Then choose OSX/Application -> Command Line Tool. Hit "Next"
  3.  You just type your project name such as "mathlink" and other info in the input bars and Choose "Type" -> C for our project. The check box underneath does not matter. Leave it checked would be OK. Hit "Next" button.
  4. hoose a directory to save your project. Just for convenience here, you can just save it on your desktop.
Before next step is just to check out an example and some important files that we will use. 
In /Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86-64/CompilerAdditions
  • Copy "mathlink.h" into /usr/include
  • Copy "mathlink.framework" into /Library/Frameworks
  • Copy libMLi3.a to /usr/lib or you can just leave it here.
In /Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86-64/MathLinkExamples
  • Open "factor.m" file and copy everything inside and paste it into the main.c in your project. You should have everything in the original main.c gets replaced by the new conent. 
  1. You probably would see an error message about missing mathlink.h. You just need to add them aforementioned filed into the build path
  • Add /usr/include. Click mathlink project -> Targets/mathlink and search for "Other C Flags". Then you can add "-I /usr/include"
  • add libMLi3.a. If you haven't not move this file, you can just drag it into the project or drag it into build phase ( the latter just copy the path instead of file). Click mathlink project -> Targets/mathlink and choose "Build Phases".
 
  • Finally you need to modify the "Other Linker Flags" in "Build Settings"


  1. Now if you run the project, you should see the "Build Success". In the target output, you will have a prompt in the Target Output dialog (bottom right)

 You might wonder that your prompt asking you to create a link instead of input. This is because XCode just launchs the binary (Target file: mathlink) directly without a specific linkname so it is waiting for you to connect it to a running Mathematica kernel. To set a default link, you can just add some argument vector into XCode.
  1. Stop the runtime and Click Product in the menu bar. Choose "Edit Scheme". 
  1. Choose "Run " -> Arguments Passed On Launch and add "-linkname /usr/local/bin/math". Click OK.
  1.  Now you can run this project again. In the prompt, you can just put a number and it will return the prime factors
POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract