Dynamic Libraries dylib for Office using XCode 5
This is a quick and dirty procedure. It assumes the reader is fairly intuitive and doesn’t require much hand holding.
Open XCode
File->New Project…
Under OS X, select Framework & Library
Next select STL C++ Library, click Next
Enter a Product Name, click Next
Click Create
You’ll need to set your build for 32-bit Intel Architectures.
Click
Architectures -> Architectures, 2nd column, Select 32-bit Intel (i386)
Valid Architectures can remain i386 x86_64
Paste this into your .cp file
int add( int a, int b ) { return a + b; } int subtract( int a, int b ) { return a - b; } int multiply( int a, int b ) { return a * b; } double divide( double a, double b ) { return a / b; }
And paste this into your .h file, not *Priv.h
/* The classes below are exported */ #pragma GCC visibility push(default) int add( int a, int b ); int subtract( int a, int b ); int multiply( int a, int b ); double divide( double a, double b ); #pragma GCC visibility pop
Command-B to build.
Open a terminal and navigate to your projects */Build/Products/Debug/*.dylib fil
Run the command
nm -g (your projectname).dylib
You should see something like this:
00000f20 T __Z3addii 00000f80 T __Z6dividedd 00000f60 T __Z8multiplyii 00000f40 T __Z8subtractii U dyld_stub_binder
_Z3addii is the function name you’ll call. nm’s output will reflect two underscores, remove the first.The two trailing characters are the two parameter types. i for integer, d for double
In Office’s VBA Editor (Alt-F11), Module1… Use COLONS and not the SLASH for paths. ByVal must be included.
Option Explicit Private Declare Function myAdd Lib "Users:dave:Documents:Word:lib:BCLib.dylib" Alias "_Z3addii" (ByVal a As Integer, ByVal b As Integer) As Integer Private Declare Function myDivide Lib "Users:dave:Documents:Word:lib:BCLib.dylib" Alias "_Z6dividedd" (ByVal a As Integer, ByVal b As Integer) As Integer Private Declare Function myMultiply Lib "Users:dave:Documents:Word:lib:BCLib.dylib" Alias "_Z8multiplyii" (ByVal a As Integer, ByVal b As Integer) As Integer Private Declare Function mySubtract Lib "Users:dave:Documents:Word:lib:BCLib.dylib" Alias "_Z8subtractii" (ByVal a As Integer, ByVal b As Integer) As Integer Function Addition(a As Integer, b As Integer) As Integer Addition = myAdd(a, b) End Function Function Subtract(a As Integer, b As Integer) As Integer Subtract = mySubtract(a, b) End Function Function Multiply(a As Integer, b As Integer) As Integer Multiply = myMultiply(a, b) End Function Function Divide(a As Double, b As Double) As Double Divide = myDivide(a, b) End Function
Test in the VBE Intermediate window (Control-Command-G)
type
?Add(5,3) 8 ?Subtract(5,3) 2 ?Multiply(5,3) 15 ?Divide(5,3) -28800
Leave a Reply