相关疑难解决方法(0)

在 macOS 上使用 clang++ 在 Visual Studio Code 中编译多个 .cpp 文件

我正在尝试在 macOS 上使用 clang++ 构建一个包含多个 *.cpp 文件的简单项目。

任务.json:

    {
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-Wall",
                "-Wextra",
                "-Weffc++",
                "-Wconversion",
                "-pedantic-errors",
                "-stdlib=libc++",
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

结果命令如下所示:

/usr/bin/clang++ -std=c++17 -Wall -Wextra -Weffc++ -Wconversion -pedantic-errors -stdlib=libc++ -g '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp' -o '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/main' 
Run Code Online (Sandbox Code Playgroud)

但编译器会抛出错误:

clang: error: no such file or …
Run Code Online (Sandbox Code Playgroud)

c++ macos visual-studio-code

7
推荐指数
2
解决办法
6605
查看次数

Visual Studio Code 与 C++ 的链接错误

为了学习 C++,我在 Mac 上安装了带有 Catalina 的 Visual Studio Code。安装的扩展C/C++C/C++ Extension PackC++ IntellisenseCMake ToolsCode Runner

为了测试 VSCode,我尝试运行以下代码:

再见.cpp:

#include <iostream>

void tryMe(int s) {
    std::cout << "ok";
}
Run Code Online (Sandbox Code Playgroud)

再见.h:

void tryMe(int s);
Run Code Online (Sandbox Code Playgroud)

你好.cpp:

#include <iostream>
#include "bye.h"

int main() {
    tryMe(3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但它不会运行,因为它会导致编译错误:

$ cd "/Users/x/Workspace/LearnCPP/" && g++ hello.cpp -o hello && "/Users/x/Workspace/LearnCPP/"hello
Undefined symbols for architecture x86_64:
  "tryMe(int)", referenced from:
      _main in hello-ef5e99.o
ld: symbol(s) not found for architecture …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors visual-studio-code

5
推荐指数
1
解决办法
5264
查看次数

如何使用visual studio代码编译多cpp文件?

我已经按照一些说明构建了Visual Studio Code C/C++编译和调试环境。但是g++编译器只能编译选定的cpp文件,因此无法编译与cpp文件相关联的.h文件。然后终端显示“架构 x86_64 的未定义符号”错误。代码如下:

.ah 文件

    int func();
Run Code Online (Sandbox Code Playgroud)

a.cpp 文件

    include <iostream>
    include "a.h"
    using namespace std;
    int func(){
        return 111;
    }
Run Code Online (Sandbox Code Playgroud)

main.cpp 文件

    include "a.h"
    using namespace std;
    int main()
    {
        int b = func();
        cout << b << endl;
    }
Run Code Online (Sandbox Code Playgroud)

Visual Studio 代码将使用如下命令

     g++ directory/main.cpp -o directory/main.out -g -Wall -fcolor-        diagnostics -std=c++11
Run Code Online (Sandbox Code Playgroud)

此命令将引发“体系结构 x86_64 的未定义符号”错误我可以使用此新命令修复它

    g++ main.cpp a.cpp -o main.out.
Run Code Online (Sandbox Code Playgroud)

所以问题是如何配置这些 json 文件来修复 g++ 编译问题。当我想使用某些库(例如 FFMpeg)时,如何正确链接 FFMpeg .h 文件。

c++ g++ visual-studio-code

4
推荐指数
4
解决办法
1万
查看次数

vscode g++ 找不到 .cpp 定义文件

我正在尝试使用多个 .cpp 和 .hpp 文件编译 C++ 示例,但 g++ 找不到任何成员函数定义。


主要.cpp:

#include <iostream>
#include "Person.hpp"

int main()
{
    std::cout << "HELL!\n";
    
    Person a{"Jiraya"};
    std::cout << a.getName() << "\n";
    a.setName("Niko");
    a.do_smt();
}
Run Code Online (Sandbox Code Playgroud)

人.hpp:

#pragma once

#include <string>

using std::string;

class Person
{
private:
    string name;

public:
    Person();
    Person(const string &n);
    void do_smt();
    string getName(){return name;}
    void setName(const string& n);
Run Code Online (Sandbox Code Playgroud)

人.cpp:

    #pragma once
    #include <iostream>
    #include "Person.hpp"
    
    Person::Person(const string &n) : name{n}
    {
    }
    
    void Person::setName(const string &n)
    {
        name = n;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ g++ visual-studio-code

1
推荐指数
1
解决办法
1466
查看次数

标签 统计

c++ ×4

visual-studio-code ×4

g++ ×2

linker-errors ×1

macos ×1