【问题标题】:VSCode doesnt build and debug pthreads codeVSCode 不构建和调试 pthreads 代码
【发布时间】:2020-10-27 10:50:53
【问题描述】:

我从https://computing.llnl.gov/tutorials/pthreads/ 获得了一些代码,我试图使用 VSCode 调试器来尝试单步调试它们,但它似乎不起作用。
使用任务(ctrl+shift+B)我可以很好地构建它(我已经添加了 -pthread 标志)但是当我尝试调试它时(F5)我得到了这个错误:

> Executing task: C/C++: gcc build active file <

Starting build...
Build finished with error:
/usr/bin/ld: /tmp/cc5vG56K.o: in function `main':
/home/xristosp59/Documents/Programming/condvar.c:98: undefined reference to `pthread_create'
/usr/bin/ld: /home/xristosp59/Documents/Programming/condvar.c:99: undefined reference to `pthread_create'
/usr/bin/ld: /home/xristosp59/Documents/Programming/condvar.c:100: undefined reference to `pthread_create'
/usr/bin/ld: /home/xristosp59/Documents/Programming/condvar.c:104: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status 
The terminal process failed to launch (exit code: -1).

我已经在我的 tasks.json 的不同位置尝试了 -pthread 和 -lpthread 标志,但似乎都不起作用,我总是收到这个错误。
这是我当前的tasks.json:(这与任务构建良好)

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "-pthread",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Generated task by Debugger"
        }
    ],
    "version": "2.0.0"
}

如果这很重要,我正在使用 pop_os 20.10。

【问题讨论】:

  • 尝试显式链接 pthread 库(即添加 -lpthread 参数)。
  • 正如我所说我也尝试过 -lpthread,我现在重试只是为了确保但我仍然得到同样的错误
  • 图书馆的顺序很重要。您是否放置了-lpthread 选项last
  • @Someprogrammerdude 是的
  • 不仅作为最后一个选项,而且在文件名之后?

标签: c visual-studio-code gdb pthreads vscode-debugger


【解决方案1】:

好吧,显然vscode,当你第一次尝试调试一个c程序时,它会创建一个launch.json和tasks.json,launch.json有一个"preLaunchTask": "C/C++: gcc build active file"选项,而tasks.json有一个"label": "C/C++: gcc build active file"选项,这匹配,但我猜是因为C/C++: gcc build active file 已经是 vscode 中的一个任务,它不使用任务中的那个(如果我错了,请纠正我)。我更改了两者的标签,现在它可以工作了。

【讨论】:

  • 仅在我重新启动 VS Code 后才向 tasks.json 添加“-pthread”。
  • @xristosp,你到底做了什么改变?
猜你喜欢
  • 2019-07-18
  • 1970-01-01
  • 2021-08-07
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
相关资源
最近更新 更多