CodeBender
CodeBender

Reputation: 36610

Undefined symbols for architecture x86_64: when making a call to a C++ function in Xcode 8

I am attempting to create a project in Xcode to access the OpenCV framework for eventual use with a Swift based app.

At a high level, I have the OpenCV framework in C++, which interfaces with a C++ binding. Then, an Objective-C class wraps the binding, so that my Swift code can access it.

However, while everything works fine in the editor, when I go to compile it, I receive the following error for each C++ function I have defined in my binding class.

Undefined symbols for architecture x86_64: "Library::multiply()", referenced from:+[ViewController multiply] in ViewController.o

The following is a an abbreviated version of my project to illustrate the problem.

First, the affected files:

enter image description here

As you can see, I have the proper suffix on my wrapper file ( ViewController.mm ).

ViewController.mm contents:

#import "ViewController.h"
#import "library.hpp"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"Number: %f", Library().multiply());
}

@end

library.hpp contents:

#pragma once
#include <stdio.h>

class Library {
public:
    Library();
    ~Library();
    double multiply();
};

library.cpp contents:

#include "library.hpp"

Library::Library() { }

Library::~Library() { }

double multiply() {
    return 1.0 * 2.0;
}

Build settings related to C++:

enter image description here

** UPDATED **

The answer posted by Harish Gupta is certainly correct, and did fix my issue. However, I did investigate further and came up with what I feel is a better solution for this at scale, which was to create a namespace for my C++ class, and then pull it into my .mm file with a using command.

Upvotes: 1

Views: 1093

Answers (1)

Harish Gupta
Harish Gupta

Reputation: 782

You have not defined multiply() as a class function which is causing this issue. Declare it as follows:

double Library::multiply() {
return 1.0 * 2.0;
}

Upvotes: 4

Related Questions