简体   繁体   中英

Symbol(s) not found for architecture x86_64 when using C/C++ library in Xcode project

I am working on a Xcode project that uses a C/C++ library which I have imported the source code to my project.

I have created the correspondent bridging header and I am using a test method I have recently created to see if the code works. When building the project I get the error

Symbol(s) not found for architecture x86_64

The complete output of the error shown in Xcode is this

错误输出

My deployment target is iOS 11 so no device should use 32 bits but I get errors related to 32 bits architectures.

MyApp-Bridging-Header.h

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "main.h"

Header file

#ifndef main_h
#define main_h

#include <stdio.h>

int my_print();

#endif /* main.h */

As the compiler complains about not finding definition of "_my_print", you could 1) check if the linking libraries include definition of _my_print 2) if you're using XCODE UI to build, try checking if all sources are added in path and libraries are intact

To me it seems straighforward linking issue and mis-configuration

After trying a lot of things that many people claimed it solved similar problems to this I came up with the solution without knowing it.

My previous main.h file was this one

#ifndef main_h
#define main_h

#include <stdio.h>

int my_print();

#endif /* main.h */

Surrounding my function with extern "C" solved it and now I can interact with my C and C++ library using Swift. Now the main.h file looks like this

#ifndef main_h
#define main_h

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
    int my_print();
#ifdef __cplusplus
}
#endif

#endif /* main.h */

I am not 100% sure how this works so if anyone has a reasonable explanation why this works I am more than happy to hear it.

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