Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift command line program with multiple files in VSCode on Mac

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.

like image 490
Keval Langalia Avatar asked Aug 31 '25 03:08

Keval Langalia


1 Answers

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"
    )
])
like image 118
Andreas Oetjen Avatar answered Sep 02 '25 16:09

Andreas Oetjen