【问题标题】:Swift "error: in auto-import: failed to get module 'foo' from AST context:"Swift“错误:在自动导入中:未能从 AST 上下文中获取模块 'foo':”
【发布时间】:2017-03-19 06:28:14
【问题描述】:

使用以下设置 (gist):

Package.swift:

import PackageDescription

let package = Package(
    name: "foo",
    dependencies: [
        .Package(url: "https://github.com/rxwei/LLVM_C", majorVersion: 1, minor: 0)
    ]
)

生成文件:

all:
    @swift build \
        -Xcc -I`llvm-config --includedir` \
        -Xlinker -L`llvm-config --libdir` \
        -Xlinker -rpath -Xlinker `llvm-config --libdir`

ma​​in.swift:

import LLVM_C.Core

func foo(_ a: Int) {
    let b = a * a
    print(b)
}

foo(4)

使用make 编译可执行文件并在调试器中启动可执行文件后,我无法打印任何变量的值:

(lldb) b foo
Breakpoint 1: where = foo`foo.foo (Swift.Int) -> () + 12 at main.swift:4, address = 0x00000001000014dc
(lldb) r
Process 14376 launched: '/Users/emlai/Code/foo/.build/debug/foo' (x86_64)
Process 14376 stopped
* thread #1: tid = 0x226d5, 0x00000001000014dc foo`foo(a=4) -> () + 12 at main.swift:4, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001000014dc foo`foo(a=4) -> () + 12 at main.swift:4
   1    import LLVM_C.Core
   2    
   3    func foo(_ a: Int) {
-> 4        let b = a * a
   5        print(b)
   6    }
   7    
(lldb) p a
error: in auto-import:
failed to get module 'foo' from AST context:

(lldb) p a
Shared Swift state for foo has developed fatal errors and is being discarded.
REPL definitions and persistent names/types will be lost.
warning: Swift error in module foo.
Debug info from this module will be unavailable in the debugger.

error: in auto-import:
failed to get module 'foo' from AST context

如果我注释掉 import LLVM_C.Core 行,一切正常。

这使我无法调试我的项目并取得进展。我该如何解决这个问题?

【问题讨论】:

    标签: swift debugging swift3 llvm lldb


    【解决方案1】:

    在网上搜索此问题只会导致“它是一个 lldb 错误”。 我发现调试的唯一方法是调试测试。但是 AFAIK 你不能用自己的 main.swift 脚本来运行测试。这导致multiple definition of 'main'

    所以只需按照此处的说明https://swift.org/getting-started/#using-the-package-manager 为带有测试的包创建所需的文件层次结构(使用swift package init 或手动),编写一些测试,运行swift test,最后运行lldb .build/debug/fooPackageTests.xctest<binary name>PackageTests.xctest 是二进制用于运行测试的文件)。我想它在某些方面的编译方式不同,而不是普通的二进制文件。至少在我的情况下有效:)

    祝调试顺利! :)

    【讨论】:

      【解决方案2】:

      也许你可以尝试在 xcode 中清理和构建项目

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-22
        • 2014-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多