I want to run a full fledged Swift command line program with multiple classes and multiple files, on Visual Studio Code. I'm using Macbook Air macOS Big Sur v11.2
I have setup VSCode from this tutorial. I am able to run single file swift program but when I try to create new class in another file (in same directory/folder), it doesn't recognize. refer the screenshots:
Consider, in Xcode similar to New project -> macOS -> command line tool. there we can have multiple files and we can define multiple classes in different files and still inherit or create object of those classes in main.swift file.
I'm pretty sure it's possible in VSCode too. May be we just need to structure the code and have a configuration. that is what i am looking for.
just similar to this question but seems no experienced people bother to answer :/
ps- I'm new to swift and using VSCode. but instead of using xCode (12 GB of installation and 25 GB occupied storage just for few command line programs), I want to use VSCode.
Just as a small hint, consider the following file structure:
./Package.swift
./src
./src/classA.swift
./src/main.swift
src
, your source files residePackage.swift
is the so-called manifest file (see below)Then, you can build your project with
swift build
which will create the executable in ./.build/debug/testCli
Here an example Package.swift
file. You might also want to check the official Package Description documentation
// swift-tools-version:5.1
import PackageDescription
let package = Package(name: "testPackage", products: [
.executable(name: "testCli", targets: ["testCli"])
], targets: [
.target(name: "testCli",
path: "src"
)
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With