• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Mac OSX Native extension (.dylib) problem [solved]

Mick

Member
Hi! I'm having trouble creating a native extension for Mac OS X 10.9. I'm able to compile the extension just fine but no matter what I try I get a "Error defining an external function" in GMS. I'm tearing my hair out here! The methods I want to expose in the .dylib library can be seen when I use the nm command. The method names have a trailing underscore, but including that when defining the function in GMS doesn't help either. I have created a .dll and an .so for Linux with the same code, and on those platforms everything is working fine. Please help!
 

rwkay

GameMaker Staff
GameMaker Dev.
have you declared the functions as extern "C" ???? in the C++

Russell
 

Mick

Member
have you declared the functions as extern "C" ???? in the C++

Russell
Thanks Russell, but I'm afraid I have. I'm trying to get three dylibs working, one is written in C and the two other in C++. For the one in C I have 'extern FunctionName...' and the ones in C++ I have 'extern "C" FunctionName...'. Same problem with all of them. This is how I compile the C one (have tried various options):

Code:
gcc -fPIC -dynamiclib -Os *.c -o hvl.dylib
I have tried with and without -fPIC, -shared instead of -dynamiclib, with and without -Os ...

Is there something else I could have missed? I have version 1.4.1567 of GMS by the way, if that matters. Thanks again!
 

Mick

Member
Ok, I FINALLY figured this out! :) I tried making the dylib in XCode to no success, but then I asked myself, is the dylib being compiled as a 32-bit binary? I have no idea why I didn't think about this earlier. I now managed to build three different dylibs using XCode and gcc/g++. gcc/g++ generates smaller dylibs so I prefer that method. For future reference, this is what I did:

Option 1: GCC/G++

To force 32-bit in gcc/g++ you can use the -m32 option, here is the full command I used to build the dylibs:

Code:
g++ -m32 -dynamiclib -Os *.cpp -o game_music_emu.dylib
Just replace g++ with gcc if you are making a C library. Remember, like Russell said, that exported functions must be declared like:

Code:
extern "C" FunctionName(...) // C++
extern FunctionName(...) // C
Option 2: XCode

In XCode I created a new C/C++ library project, chose dynamic, and then changed the target architecture option to 32-bit Intel:

 
Top