Linux xctest "undefined reference" when trying to run unittest

Hello,

I'm working on an executable and I'm trying to setup some unittests using XCTest. I have the default folder structure ( swift package init --type executable) and when I run swift test in my terminal the unit tests work. However, When I try to import a struct in my Tests folder I get the error: undefined reference and a clang-7: error: linker command failed with exit code 1 (use -v to see invocation). I have no clue why it doesn't work. There are no errors in the struct. Does anyone knows how to fix this?

P.S. at the top of test script I already do: @testable import myProject

System: Ubuntu 20.04
swift version: Swift version 5.2.4 (swift-5.2.4-RELEASE)
clang version 7.0.0

Can you provide the full error output?

Can you show me your Package.swift please? (Please don't use images, but copy and paste the text)

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "swifty",
    dependencies: [.package(url: "https://github.com/mxcl/Path.swift.git", from: "1.0.0"),
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    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: "swifty",  dependencies: [.product(name: "Path", package: "Path.swift")]),
        .testTarget(name: "swiftyTests",dependencies: ["swifty"]),
    ]
)

SwiftPM cannot test executable targets. It provides terrible diagnostics on this, but this is what you're hitting.

I recommend adding a library target that contains your code. This would mean changing your Package.swift to look like this:

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "swifty",
    dependencies: [.package(url: "https://github.com/mxcl/Path.swift.git", from: "1.0.0"),
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    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: "swifty", dependencies: ["libswifty"]),
        .target(name: "libswifty",  dependencies: [.product(name: "Path", package: "Path.swift")]),
        .testTarget(name: "swiftyTests",dependencies: ["libswifty"]),
    ]
)
1 Like