Swift"错误:在自动导入中:未能获得模块&foo; foo'来自AST背景:"

时间:2016-11-05 14:25:17

标签: swift debugging swift3 llvm lldb

使用以下设置(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`

main.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行,一切正常。

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

2 个答案:

答案 0 :(得分:2)

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

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

祝你好运! :)

答案 1 :(得分:0)

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