3

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:

vscode screenshot

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Keval Langalia
  • 1,762
  • 1
  • 16
  • 29
  • 2
    Did you try to work with `swift build` and a `Package.swift`file – Andreas Oetjen Feb 17 '21 at 21:21
  • Previously I thought the same but don't know how to make `package.swift` file and use `swift build`. can you guide me or redirect me towards any specific link. It'd be much helpful. – Keval Langalia Feb 17 '21 at 22:54

1 Answers1

2

Just as a small hint, consider the following file structure:

./Package.swift
./src
./src/classA.swift
./src/main.swift
  • In src, your source files reside
  • Package.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

The manifest

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"
    )
])
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • I'm getting error: `no such module 'PackageDescription'`. I looked into the error and found this link: https://forums.swift.org/t/no-such-module-packagedescription-sourcekitd/29406/2 unfortunately I'm unable to understand how to change the config – Keval Langalia Feb 18 '21 at 18:57
  • Strange, haven't seen this. You might wanna try something like this here: https://forums.swift.org/t/command-line-build-fails-with-mainfest-parse-error-no-such-module-packagedescription/35298/7 – Andreas Oetjen Feb 18 '21 at 20:38
  • appreciate your help, but it seems that the link and setup that is been suggested are above my understanding level :/ I need to educate myself more towards swift builds and toolchains. and also seems like my vscode has broken setup. I'm frustrated.. – Keval Langalia Feb 18 '21 at 22:35
  • I'm upvoting this answer as it helps, but still I'm unable to setup so keeping the thread open. – Keval Langalia Feb 18 '21 at 22:35