简体   繁体   中英

Bridging C++ and Swift in XCode, what are the right Build Settings?

When I try to call the functions in my C++ implementation file (marked as C compatible in the header with the #ifdef __cplusplus extern "C" {} trick), I am getting the error:

"use of unresolved identifier, 'helloWorld()'"

I created the bridging header just by adding a cpp and header file to my project and clicking "OK" when it asked if I wanted to create the bridging header. I am #including "test.h" in the bridging header, which has the function declaration inside, but for some reason Swift just cannot see the function.

The weirdest part is, I created a new project and did the EXACT same procedure, and calling helloWorld() works fine, so it's something about the existing project I'm trying to add the cpp file to that isn't working.

I'm assuming then that this is a build-settings problem, and that I must have messed them up somewhere along the lines, probably when I was trying to import a static C/CPP library (Which I've since removed, but some of the build settings I changed for it probably stayed).

So are there any build settings I should know about that will fix this? Or is there a way to just completely reset my build settings to the default (Even if that means I have to recreate the bridging header to get the bridging header build settings back in place)?

Thank you for any and all help!

James

---Edit---

Here is my cpp header:

Test.h

Here is my cpp implementation file:

TEST.CPP

Here is my bridging header:

鹰眼桥接,Header.h

Here is the relevant portion of the Swift project:

viewController.swift中的viewDidLoad

And here is the build setting that sets the bridging header:

相关构建设置

Alright - This has been resolved. The problem is that the bridging header needed to be realized from both the regular target and also the Testing target, which was in my existing project because I'm doing Unit Testing.

I simply went into the build settings for my testing target and added the appropriate Objective-C Bridging Header, and then it all worked fine.

Hope it helps someone!

I had the same experience. Make sure you add a Unit Test Target, then ensure the Unit Test Target has visibility of the Bridging Header file.

Another mistake I made; ensure each Swift Class, Enums used in the test case has Target Membership set correctly.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM