swift pm cli uses cpp (cpp interop) - Posted on Jun 4, 2024 - See https://www.swift.org/documentation/cxx-interop - Logs Home - d0018
Swift package manager cli uses c++, (swift package manager).
mkdir swiftcpp cd swiftcpp swift package init --type=executable
Take a look at the manifest file Package.swift,
The file can be modified, but the first line version can not be removed.
Change the coding style (but code is not changed):
(Package.swift)
// swift-tools-version: 5.10 import PackageDescription let package = Package( name: "swiftcpp", targets: [ .executableTarget( name: "swiftcpp" ), ] )
swift build swift run
.executableTarget( name: "swfitcpp", swiftSettings: [.interoperabilityMode(.Cxx)] // Enable c++ interop ),
targets: [ .target( name: "one_cpp_lib" // The c++ library target name is one_cpp_lib ), .executableTarget( name: "swfitcpp", dependencies: ["one_cpp_lib"], // project swiftcpp depends one_cpp_lib swiftSettings: [.interoperabilityMode(.Cxx)] ), ]
Package.swift
will look like this:
The first line
should not be removed.
// swift-tools-version: 5.10
// swift-tools-version: 5.10 import PackageDescription let package = Package( name: "swiftcpp", targets: [ .target( name: "one_cpp_lib" ), .executableTarget( name: "swiftcpp", dependencies: ["one_cpp_lib"], swiftSettings: [.interoperabilityMode(.Cxx)] ), ] )
mkdir ./Sources/swiftcpp mkdir ./Sources/one_cpp_lib mkdir ./Sources/one_cpp_lib/include
In fact, ./Sources/main.swift can be moved to ./Sources/swiftcpp/,
or be removed and put your own swift code at ./Sources/swiftcpp/ .
rm ./Sources/main.swift
Now, the project folder tree is:
$ cd .. $ tree ../swiftcpp swiftcpp/ |---- Package.swift |---- Sources/ |---- one_cpp_lib/ | |---- include/ | |---- swiftcpp/
File: ./Sources/one_cpp_lib/include/any_name_cpp_file.hpp
// Header projection macro must be added here, // otherwise the swift package system might parse the same header twice #ifndef __any_name_cpp_file_hpp__ #define __any_name_cpp_file_hpp__ #include <iostream> class any_cpp_class_name { public: void print() const { std::cout << "Hello, I am c++!" << std::endl; } }; #endif
File: ./Sources/swiftcpp/start_point_any_name.swift
import one_cpp_lib print("Hello I am swift!") var cpp_object = any_cpp_class_name() cpp_object.print()
$ swift build Building for debugging... [8/8] Linking swiftcpp Build complete! (1.17s) $ swift run Building for debugging... [1/1] Write swift-version-32A5E7755150B194.txt Build complete! (0.44s) Hello I am swift! Hello, I am c++!
Upgrade File: ./Sources/one_cpp_lib/include/any_name_cpp_file.hpp
// Header projection macro must be added here, // otherwise the swift package system might parse the same header twice #ifndef __any_name_cpp_file_hpp__ #define __any_name_cpp_file_hpp__ #include "any_name_another.hpp" #endif
New File: ./Sources/one_cpp_lib/include/any_name_another.hpp
#ifndef __any_name_another_hpp__ #define __any_name_another_hpp__ #include <iostream> class any_name_class { public: void print() const { std::cout << "Hello, I am c++ !!!" << std::endl; } int get() const { return 987; } }; #endif
Upgrade File: ./Sources/swiftcpp/start_point_any_name.swift
import one_cpp_lib print("Hello, I am swift !!!") var cpp_object = any_name_class() cpp_object.print() let result = cpp_object.get() print("result:", result)
$ swift build Building for debugging... [8/8] Linking swiftcpp Build complete! (1.28s) $ swift run Building for debugging... [1/1] Write swift-version-32A5E7755150B194.txt Build complete! (0.33s) Hello, I am swift !!! Hello, I am c++ !!! result: 987
Try it youself !
I practiced it, simple source file tree structure is supported, but too complicated source tree will cause swift package manager reporting errors.
c++ std::exception:
std::cout.write(err.data(), err.size());
std::cout << std::endl;
caught:
=================================== # The c++ programming language. # # # # Join c++ Discord: yZcauUAUyC # # Deck # ===================================