铛:错误:链接器命令失败,退出代码为1体系结构x86_64的未定义符号

问题描述

让我的程序在构建后就可以运行了。尝试将lldb链接到我的项目,以便调试程序。每当我尝试启动调试器时,都会为我弹出此通知

enter image description here

我认为我没有正确设置Launch.json,但是无法推断出问题所在。

以下内容将打印到终端:


Undefined symbols for architecture x86_64:
  "write_color(std::__1::basic_ostream<char,std::__1::char_traits<char> >&,Vec3)",referenced from:
      _main in main-1e06e3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)
The terminal process "/bin/zsh '-c','/usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/n3vdawg/gameDevStudies/ray_tracer_definitive/src/*.cpp -o /Users/n3vdawg/gameDevStudies/ray_tracer_definitive/ray_tracer'" terminated with exit code: 1.

Terminal will be reused by tasks,press any key to close it.

我正在使用VSCode,并且我的项目是用C ++编写的。我相信这是我的launch.json和tasks.json prelaunchTask是正确的。我不知道如何正确格式化launch.json,这使我无法调试。

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information,visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0","configurations": [
        {
            "name": "clang++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${workspaceFolder}/ray_tracer","args": [],"stopAtEntry": true,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "lldb","prelaunchTask": "C/C++: clang++ build active file"
        }
    ]
}

tasks.json

{
    "version": "2.0.0","tasks": [
        {
            "type": "shell","label": "C/C++: clang++ build active file","command": "/usr/bin/clang++","args": [
                "-std=c++17","-stdlib=libc++","-g","${workspaceFolder}/src/*.cpp","-o","${workspaceFolder}/ray_tracer"
              ],"options": {
                "cwd": "${workspaceFolder}"
            },"problemmatcher": [
                "$gcc"
            ],"group": {
                "kind": "build","isDefault": true
            }
        }
    ]
}

包括我的文件结构:

enter image description here

添加write_color函数

Color.cpp

#include <iostream>
#include <fstream>
#include "Color.h"

void write_color(std::ofstream &out,Color pixel_color) {
    // Write the translated [0,255] value of each color component
    out << static_cast<int>(255.999 * pixel_color.x()) << ' '
        << static_cast<int>(255.999 * pixel_color.y()) << ' '
        << static_cast<int>(255.999 * pixel_color.z()) << '\n';
}

Color.h

#ifndef _COLOR_H_
#define _COLOR_H_

#include "Vec3.h"

#include <iostream>

void write_color(std::ostream &out,Color pixel_color);

#endif

意识到我需要包括fstream并且函数def应该期望std :: ofstream

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)