1

In an iOS Swift project I'm including a set of libraries built using C++ (HikVision/hcnetsdk.h) and while I've managed to get the frameworks to build, the header file won't compile and Xcode is giving me multiple 'C does not support default arguments'.

For example on this line:

NET_DVR_API BOOL __stdcall NET_DVR_SetConnectTime(DWORD dwWaitTime = 3000, DWORD dwTryTimes = 3);

I've checked the build settings of the project and as far as I can tell everything is set to use C++, so I'm confused as to why Swift is compiling this as C, and complaining about default arguments.

My bridging header file is as follows:

#import "ldapTest.h"
#import "hcnetsdk.h"
#import "HikDec.h"

I'm fairly sure it's a build setting somewhere but which one I don't know.

AndyDunn
  • 1,074
  • 1
  • 12
  • 19
  • Swift *cannot* import C++ code. The bridging header file is always treated as (Objective-)C. – Martin R Dec 19 '18 at 07:44
  • @MartinR That's an answer I was hoping I wouldn't hear. Does this mean it's impossible to include this header file and subsequent library using Swift? – AndyDunn Dec 19 '18 at 07:53
  • See for example https://stackoverflow.com/a/32554229/1187415: *" When a .h **does not contain a single C++ keyword,** like class, it can be added to the ...Bridging-Header.h"* – Martin R Dec 19 '18 at 07:58
  • 2
    ... and https://stackoverflow.com/a/24042893/1187415: *"You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code."* – Martin R Dec 19 '18 at 07:59

1 Answers1

0

So it turns out you can't include a C++ header file which contains C++ specific keywords into a Swift bridging header file.

What I did manage to do for my specific situation was find a "C" replacement of the "C++" header which worked with a few minor tweaks.

This is the "C" header file for the HikVision HCNetSDK.h which I found online: https://github.com/shulianghe/new-project/blob/master/pro/hcSrc/incCn/HCNetSDK.h

If anyone has a similar problem with other C++ header files, you can copy some of the #ifdef __cplusplus statements in the link above to see how it's done.

AndyDunn
  • 1,074
  • 1
  • 12
  • 19