2 Replies
      Latest reply on Feb 17, 2020 9:18 AM by MatteCarra
      MatteCarra Level 1 Level 1 (0 points)

        I'm trying to include a C++ Swift Package into an iOS app.

        I'm completely new to xcode and I'm having a lot of trouble making my project build.

         

        My code relies on c++14 features, and I depend on pthreads, security framework and some static libraries that I have already built (openssl).

        This is my Package.swift:

        // swift-tools-version:5.1
        // The swift-tools-version declares the minimum version of Swift required to build this package.
        
        import PackageDescription
        
        let package = Package(
            name: "MyLibrary",
            products: [
                // Products define the executables and libraries produced by a package, and make them visible to other packages.
                .library(
                    name: "MyLibrary",
                    targets: ["MyLibrary"]),
            ],
            dependencies: [],
            targets: [
                // Targets are the basic building blocks of a package. A target can define a module or a test suite.
                // Targets can depend on other targets in this package, and on products in packages which this package depends on.
                .target(
                    name: "MyLibrary",
                    dependencies: [],
                    path: "Sources/MyLibrary",
                    cxxSettings: [
                        .headerSearchPath("Dependencies/include/")
                    ],
                    linkerSettings: [
                        .linkedFramework("Security"),
                        .linkedLibrary("pthread"),
                        .linkedLibrary("/Users/matteo/Documents/Workspace/C++/MyLibrary/Sources/MyLibrary/Dependencies/lib/libcrypto.a"),
                        .linkedLibrary("/Users/matteo/Documents/Workspace/C++/MyLibrary/Sources/MyLibrary/Dependencies/lib/libssl.a"),
                    ]),
            ],
            cxxLanguageStandard: CXXLanguageStandard.cxx14
        )

         

        The real problem begins when I try to build it.

        I get hundreds of error like:

        In file included from /Users/matteo/Documents/Workspace/C++/MyLibrary/Sources/MyLibrary/utils/SessionService.cpp:6:
        In file included from /Users/matteo/Documents/Workspace/C++/MyLibrary/Sources/MyLibrary/include/protocol/utils/HttpClient.h:9:
        In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:480:
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:73:35: error: expected ';' at end of declaration
        __tree_is_left_child(_NodePtr __x) _NOEXCEPT
                                          ^
        error: C++ requires a type specifier for all declarations
        __tree_is_left_child(_NodePtr __x) _NOEXCEPT
                                           ^
        note: expanded from macro '_NOEXCEPT'
        #  define _NOEXCEPT noexcept
                            ^

         

        or

        In file included from /Users/matteo/Documents/Workspace/C++/MyLibrary/Sources/MyLibrary/protocol/data/LPrefixedString.cpp:8:
        In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/codecvt:59:
        In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:18:
        In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/mutex:191:
        In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__mutex_base:15:
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/chrono:932:67: error: expected ';' at end of declaration list
            _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() _NOEXCEPT {return _Rep(0);}

         

         

         

         

        I have no idea the reason and if I messed up anything in the Package.swift

        I followed the documentation at Target - Swift Package Manager | Apple Developer Documentation

         

        Thanks

        • Re: Toolchain library errors when building C++ Swift Package
          john daniel Level 4 Level 4 (640 points)

          C++ is not compatible with Swift. If you want to create a Swift package from C++, you will need an additional target to wrap the C++ in either C or Objective-C++. Then that wrapper interface can be exposed via Swift.

          • Re: Toolchain library errors when building C++ Swift Package
            MatteCarra Level 1 Level 1 (0 points)

            I'm still not sure about the reason of the issue, but I discovered that you can't include binaries in a Swift Package.

             

            This is the correct Package.swift:

            // swift-tools-version:5.1
            // The swift-tools-version declares the minimum version of Swift required to build this package.
            
            import PackageDescription
            
            let package = Package(
                name: "MyLibrary",
                products: [
                    // Products define the executables and libraries produced by a package, and make them visible to other packages.
                    .library(
                        name: "MyLibrary",
                        targets: ["MyLibrary"]),
                ],
                dependencies: [],
                targets: [
                    // Targets are the basic building blocks of a package. A target can define a module or a test suite.
                    // Targets can depend on other targets in this package, and on products in packages which this package depends on.
                    .target(
                        name: "MyLibrary",
                        dependencies: [],
                        path: "Sources/MyLibrary",
                        linkerSettings: [
                            .linkedFramework("Security"),
                            .linkedLibrary("pthread"),
                            .linkedLibrary("crypto"),
                            .linkedLibrary("ssl")
                        ]),
                ],
                cxxLanguageStandard: CXXLanguageStandard.cxx14
            )
            
            

             

            I then placed the binaries in a directory and added the directory to the 'Library search paths' in Project->Build Settings