Linker error: undefined symbol __myfunc

Hi, I am having linker errors while trying to build my own custom ios plugin, note the leading DOUBLE underscore in the function name:

Undefined symbols for architecture armv7:

  "__TPTUTY_update", referenced from:
      RegisterMonoModules() in RegisterMonoModules.o

Now the juicy details:

My plugin comes with a static lib (.a) file which I created and added under Assets/Plugins/iOS, the .a file correctly appears after I build the xcode project from the unity IDE. It is also correctly added to the list of linked libraries of the project.

My static lib was built using an .m file, containing the function with NO leading underscore:

void TPTUTY_update() {
	logdbg_func
}

When listing the symbols inside the .a file, I see the function, with a SINGLE leading underscore:

nm libTROPHiT-Unity.a | grep TPTUTY
00000000 T _TPTUTY_update

My Unity-side C# code looks like this, using a SINGLE leading underscore (or else the unity built process fails, claiming “_TPTUTY_update” is not found):

	[DllImport("__Internal")]
	private static extern void _TPTUTY_update();
... { _TPTUTY_update(); }

Any idea how to solve the linker error?
tntx

remove leading underscore. Short version: for funcs with “c” linkage clang/gcc/maybe some others mangle the name by adding underscore. If you check the linker error you’ll see that you have two underscores in there :wink:
so in your c# file func should simply have same name as in native.

P.S. if you push stuff in mm or cpp file - you will get cpp mangling which is crazy, so just do extern “C” void BLA() - which will force c-style mangling

Strange, I already tried that before and it complained that _TPTUTY_update() was not found… but now I tried it again and it seemed to have worked, except for one linker error:
_UnityGetGLViewController is not found.

The UnityGetGLViewController() function is defined in iPhone_View.h and declared in iPhone_View.m, both created by Unity build process, I declare it in my lib file as an extern symbol, like so, so I can use it at compile time:

extern UIViewController* UnityGetGLViewController();

(also tried without “extern”)
I’m banging my head here…
any ideas?
tnx

EDIT: misread question
again - it is the very same issue, though in different direction. It is declared in MM file, so it uses cpp mangling and _UnityGetGLViewController is c mangling. We can do better, sure, but for now you need or use cpp yourself, or add some helper in MM file with extern “C” linkage to return what you want

correct :slight_smile: here is how it finally worked:

extern "C" {
	// because UnitySendMessage is declared extern "C" by unity
	extern void UnitySendMessage(const char *, const char *, const char *);
}
// because UnityGetGLViewController is declared in .mm (without extern "C") by unity:
extern UIViewController* UnityGetGLViewController();