0

Project “app” gets build err: "Undefined symbol: run_mobile_session()"

Proj app includes ObjC class lib project hub_lib.

Proj hub_lib has folder with main.cpp file that has function "run_mobile_session()" that is undefined in app build

  1. I build without error the hub_lib project and did ctrl-drag of its .h and .a from its Xcode folders pane to a folder within project app.
  2. I dragged .h & .a from folder within app to folder pane of app. (ie. imported)
  3. I selected target to be my provisioned iPhone.
  4. Built app, got error.

I ran nm on and get this...

DOUGs-MacBook-Pro:mobile_sys_hub_lib dbell$ nm libmobile_sys_hub_lib.a | grep run_mobile_session
                 U __Z18run_mobile_sessionv
DOUGs-MacBook-Pro:mobile_sys_hub_lib  

The U at the front of the line is "undefined". nm thinks you have a reference to the symbol but no implementation. – Phillip Mills

Doug: main.cpp seems to be getting compiled in the class lib: I put junk "zzzzzz" within run_mobile_session() and get build error.

THE CODE...

------------------------------------------- ObjC app project...

//app.h ...........................

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end


// ViewController.mm  ....................


#import <UIKit/UIKit.h>
#import "hub_lib.h"

@interface ViewController ()    
@end

@implementation ViewController

. . .  

- (IBAction)start_session:(id)sender
{
    hub_lib* _lib = [hub_lib    new];

    NSLog(@"app:     %d", [ _lib    start_session]);
}
@end





---------------------------------------   lib project...
// hub_lib.h ....................

#import <Foundation/Foundation.h>

@interface hub_lib : NSObject
@end



// hub_lib.mm  ......................................  

#import "hub_lib.h"
#import <UIKit/UIKit.h>

@implementation hub_lib

extern void run_mobile_session(void);

. . .  

-(int)start_session
{
        run_mobile_session();

  return 0;
}

@end


---------------------------------------   C++ folder within lib project...
// main.cpp .....................

#include <stdio.h>
void run_mobile_session(void);

. . .

void run_mobile_session(void)
{
   . . .
}
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • Can you run `nm` on your library from a terminal session? Checking the output for that symbol name would tell you whether the problem is in the library's build or in the main app's linking. – Phillip Mills Oct 24 '20 at 14:55
  • run_mobile_session is in lib. I added the output above. – Doug Null Oct 24 '20 at 15:00
  • The U at the front of the line is "undefined". `nm` thinks you have a reference to the symbol but no implementation. – Phillip Mills Oct 24 '20 at 15:07
  • For the answer, see https://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work Your problem is that the Objective-C code expects a C function, but you wrote a C++ function which looks the same, but is a totally different function. – gnasher729 Oct 24 '20 at 15:24
  • if it would be recognized as C instead of C++ it would be not undefined first but syntax errors would occur. And there is no objC, here is only objC++ shown. Even if, if its placed and called in an .mm, which is objC++ that is the right way to access C++. Your hub_lib.h interface needs method definition of -(int)start_session; and main.cpp does expose its headers into the project in which way? – Ol Sen Oct 25 '20 at 02:19
  • gnasher729: But there is no Objective-C code because it's in a .mm file, -- ObjC++, yes? – Doug Null Oct 27 '20 at 17:43

1 Answers1

0

Solution was to move the extern from hub_lib.mm to hub_lib.h and mod to be C++: extern "C++" void run_mobile_session(void);

Thanks for solution, gnasher729.

Doug Null
  • 7,989
  • 15
  • 69
  • 148