Basic c++xcodeproj call to swift code

I have c++ macOs app(Xcode +14) and I try to add call to swift code. I can't find any simple c++ xcodeproj call to swift code. I create new simple project and fail to build it with error when I try to include #include <SwiftMixTester/SwiftMixTester-Swift.h>: main.m:9:10: error: 'SwiftMixTester/SwiftMixTester-Swift.h' file not found (in target 'CppCallSwift' from project 'CppCallSwift') note: Did not find header 'SwiftMixTester-Swift.h' in framework 'SwiftMixTester' (loaded from '/Users/yanivsmacm4/Library/Developer/Xcode/DerivedData/CppCallSwift-exdxjvwdcczqntbkksebulvfdolq/Build/Products/Debug') .

Please help.

Answered by DTS Engineer in 863958022

I’m not sure what’s going on in your case, but I’ve been meaning to play around with C++ interoperability for a while so I sat down to test this today. I started out by testing the case where Swift calls C++:

  1. In Xcode 26.0.1, I created a new project from the macOS > Command Line template, selecting Swift as the language.

  2. I changed the C++ and Objective-C Interoperability build setting to C++ / Objective-C++.

  3. I created a new file and its header from the macOS > C++ File template.

  4. That asked me whether I want to create a bridging header, which I agreed to.

  5. In that bridging header I added:

    #include "CppSide.hpp"
    
  6. In CppSide.hpp I added a prototype of a function:

    extern int cppAdd(int a, int b);
    

    and in CppSide.cpp I added the implementation:

    extern int cppAdd(int a, int b) {
        return a + b;
    }
    
  7. In main.swift, I added code to call that:

    let c = cppAdd(40, 2)
    print("Hello, World! \(42)")
    
  8. I built and ran the tool. It printed:

    Hello, World! 42
    

Next I testing the other direction:

  1. I created a new file from the macOS > Swift template.

  2. I populated it like so:

    public class SwiftSide {
        public static func swiftAdd(_ a: CInt, _ b: CInt) -> CInt {
            return a + b
        }
    }
    
  3. In CppSide.cpp, I added an include of the Swift header:

    #include "Test805433-Swift.h"
    

    then modified cppAdd(…) to call Swift:

    extern int cppAdd(int a, int b) {
        return Test805433::SwiftSide::swiftAdd(a, b);
    }
    

    Note that Test805433 is the C++ namespace associated with my Swift module, and because this is a command-line tool the module name is the name of my tool target.

  4. I re-ran the tool and it produced the same result.

Nice!

Please repeat these steps at your end and see if I can make it work. If so, you have a working example that you can crib from for your real project.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I use Xcode 26. I like to use the modern mix from 2023 of c++ & swift. there is the fibonacci example that is swift app call to c++. here is the CppCallSwiftCommandLine check that fails:

I’m not sure what’s going on in your case, but I’ve been meaning to play around with C++ interoperability for a while so I sat down to test this today. I started out by testing the case where Swift calls C++:

  1. In Xcode 26.0.1, I created a new project from the macOS > Command Line template, selecting Swift as the language.

  2. I changed the C++ and Objective-C Interoperability build setting to C++ / Objective-C++.

  3. I created a new file and its header from the macOS > C++ File template.

  4. That asked me whether I want to create a bridging header, which I agreed to.

  5. In that bridging header I added:

    #include "CppSide.hpp"
    
  6. In CppSide.hpp I added a prototype of a function:

    extern int cppAdd(int a, int b);
    

    and in CppSide.cpp I added the implementation:

    extern int cppAdd(int a, int b) {
        return a + b;
    }
    
  7. In main.swift, I added code to call that:

    let c = cppAdd(40, 2)
    print("Hello, World! \(42)")
    
  8. I built and ran the tool. It printed:

    Hello, World! 42
    

Next I testing the other direction:

  1. I created a new file from the macOS > Swift template.

  2. I populated it like so:

    public class SwiftSide {
        public static func swiftAdd(_ a: CInt, _ b: CInt) -> CInt {
            return a + b
        }
    }
    
  3. In CppSide.cpp, I added an include of the Swift header:

    #include "Test805433-Swift.h"
    

    then modified cppAdd(…) to call Swift:

    extern int cppAdd(int a, int b) {
        return Test805433::SwiftSide::swiftAdd(a, b);
    }
    

    Note that Test805433 is the C++ namespace associated with my Swift module, and because this is a command-line tool the module name is the name of my tool target.

  4. I re-ran the tool and it produced the same result.

Nice!

Please repeat these steps at your end and see if I can make it work. If so, you have a working example that you can crib from for your real project.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer
  1. keep swift code in different target(framework type)
  2. set swift target build settings:

2.1. SWIFT_INSTALL_OBJC_HEADER to yes. 2.2. C++ and Objective-C Interoperability to C++ / Objective-C++

  1. add swift target as Embed in c++ target
  2. include <swift target name>-Swift.h where you need to use. and use it.
  3. set c++ target :: build settings:: HEADER_SEARCH_PATHS to $(BUILT_PRODUCTS_DIR)/<Swift target name>.framework/Headers

I not see how to update my prevue comment. so I fix it.

  1. keep swift code in different target(framework type)
  2. set swift target build settings:

2.1. SWIFT_INSTALL_OBJC_HEADER to yes. 2.2. C++ and Objective-C Interoperability to C++ / Objective-C++ 3. add swift target as Embed in c++ target(this automatically add it to be linked) 4. include <swift target name>-Swift.h where you need to use. and use it. 5. set c++ target :: build settings:: HEADER_SEARCH_PATHS to $(BUILT_PRODUCTS_DIR)/<Swift target name>.framework/Headers

abut part 5, I sure someone can give better path or way, but this work.

I not see how to update my prevue comment. so I fix it.

  1. keep swift code in different target(framework type)
  2. set swift target build settings:
    1. SWIFT_INSTALL_OBJC_HEADER to yes.
    2. C++ and Objective-C Interoperability to C++ / Objective-C++
  3. add swift target as Embed in c++ target(this automatically add it to be linked)
  4. include <swift target name>-Swift.h where you need to use. and use it.
  5. set c++ target :: build settings:: HEADER_SEARCH_PATHS to $(BUILT_PRODUCTS_DIR)/<Swift target name>.framework/Headers

abut part 5, I sure someone can give better path or way, but this work.

Basic c&#43;&#43;xcodeproj call to swift code
 
 
Q