首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Mac上设置Visual代码,以便在生成期间链接多个文件?

如何在Mac上设置Visual代码,以便在生成期间链接多个文件?
EN

Stack Overflow用户
提问于 2021-10-05 01:02:36
回答 1查看 730关注 0票数 1

我正在学习一个C++,intro类,我们现在开始为OOP实现数据抽象。我需要将类定义隐藏在一个单独的.cpp文件中,并使用一个头文件。到目前为止,我一直在使用Macbook pro中的VSCode中的默认设置来创建单个文件程序,所以我决定创建一个测试程序,以确保在创建整个项目之前编译、包含和链接都能正常工作,但是现在当我试图构建程序时,我收到了一个错误。几天来,我一直试图用潜在的解决办法来解决这个问题,但没有成功。我希望这里有人能把我引向正确的方向。

我已经包含了3个测试文件(test.cpp、test.h和testImp.cpp)、c_cpp_properties.json、launch.json、settings.json和task.json文件的内容以及错误消息的内容。

任何帮助设置VSCode完成,包括头文件将非常感谢,因为我将有更多的项目与这一要求。

谢谢你詹姆斯。

test.cpp

代码语言:javascript
复制
#include <iostream>
#include "test.h"

using namespace std;

int main()
{
    cout << testCalculation(5, 7) << endl;
    cout << endl;
    return 0;
}

test.h

代码语言:javascript
复制
#ifndef test_h
#define test_h

int testCalculation(int, int);

#endif /* test_h */

testImp.cpp

代码语言:javascript
复制
#include "test.h"

int testCalculation(int x, int y) {
int sum = int();
    sum = x + y;
    return sum;
};

c_cpp_properties.json

代码语言:javascript
复制
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${default}",
                "${workspaceFolder}/**"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++11",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}

launch.json

代码语言:javascript
复制
{
    // 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": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: g++ build active file"
        }
    ]
}

setting.json

代码语言:javascript
复制
{
    "files.associations": {
        "iostream": "cpp"
    }
}

task.json

代码语言:javascript
复制
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

错误

代码语言:javascript
复制
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /Users/jamesreal/cpp/cs-2/project-1/test.cpp -o /Users/jamesreal/cpp/cs-2/project-1/test
Undefined symbols for architecture x86_64:
  "testCalculation(int, int)", referenced from:
      _main in test-5cbf98.o
ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)
EN

回答 1

Stack Overflow用户

发布于 2022-01-23 11:01:10

-1.兄弟。

您的配置文件是要构建、编译和调试的Only one file,而不是需要的multiple sources files'

0.第一见

首先,你把"settings.json“命名为”setting.json“是错误的;

此外,将"tasks.json“命名为"task.json”也是错误的。

1.你的计划

正如您的项目体系结构test所显示的,

代码语言:javascript
复制
test

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__test.cpp

|__test.h

|__testImp.cpp

3.解决办法:

设置多个源文件的三种方法

VSCode。

一种是将终端命令写到"settings.json“和

tasks.json+ "launch.json“+ "c_cpp_properties.json";

两种方法是通过make编写“make”文件;

三是通过CMake编写“CMake”来实现这一点。

现在,我只分享第一个解决方案。

4.我在mac上使用VSCode的工作区环境

我只使用Clang9.0.0编译,Code Runner v0.11.6编译

运行程序,CodeLLDB v1.4.5进行调试,C/C++ Clang Command Adopter

v0.2.4(VSCode Extension)在mac上提供VSCode的静态检测。

也许您使用cpptools(cpptools用于调试,这个CPPtools )

CodeLLDB的冲突。),那不重要,只要改变

"launch.json“中从"lldb”到"cppdbg“的"type”。

5.我的解决办法

这是一个关于同一个文件夹下的多个源文件要在mac上使用VSCode编译和调试的问题。

我总是首先配置settings.json。这表明:

"settings.json",小心"code-runner.executorMap“。

注意事项:这里是"settings.json“。不是“setting.json”!

代码语言:javascript
复制
{
    "files.defaultLanguage": "c++",
    "editor.suggest.snippetsPreventQuickSuggestions": false,
    "editor.acceptSuggestionOnEnter": "off",
    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
    // 3.Mutiple sourece C/C++ files under same folder build, compile, debug...
    // if put main.o and debug folder into the current directory "./"
    "c": "cd $dir && clang -std=c11 -stdlib=libc++  $dir/*.c  -o $dir/$fileNameWithoutExt && $dir/$fileNameWithoutExt", // Have changed
    "cpp": "cd $dir && clang++ -std=c++11 -stdlib=libc++  $dir/*.cpp -o $dir/$fileNameWithoutExt && $dir/$fileNameWithoutExt" // Have changed
    },
    "code-runner.saveFileBeforeRun": true,
    "code-runner.preserveFocus": false,
    "code-runner.clearPreviousOutput": false,
    "code-runner.ignoreSelection": true,
    "C_Cpp.clang_format_sortIncludes": true,
    "editor.formatOnType": true,
    "clang.cxxflags": [
        "-std=c++11"
    ],
    "clang.cflags": [
        "-std=c11"
    ],
    "C_Cpp.updateChannel": "Insiders",
    "[makefile]": {
        "editor.insertSpaces": true
    },
    "C_Cpp.default.includePath": [
        "${workspaceFolder}"
    ],
    "clang.completion.enable": true
}

如果使用gcc/g++,只需将clang/clang++更改为那些。

如果这行代码需要更改或函数,我将添加注释

"// Have changed“。只是注意到那行代码!请!

"tasks.json":你知道,那是“tasks.json”!不是"task.json“。

代码语言:javascript
复制
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "Compile With clang++",
      //"command": "clang++",/usr/bin/clang++
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++11",
        "-stdlib=libc++",
        // My project fitBodyBootCamp were under 
        // /Users/marryme/VSCode/CppProject/fitBodyBootCamp
        // So ${workspcaeFolder} also were
        // /Users/marryme/VSCode/CppProject/fitBodyBootCamp
        // all the *.cpp files were under
        // /Users/marryme/VSCode/CppProject/fitBodyBootCamp
        "${workspaceFolder}/*.cpp", // Have changed
        "-o",
        // Thanks those chiense website bloggers!
        // 1.mac vscode compile c++ multi-directory code demo
        // https://blog.csdn.net/fangfengzhen115/article/details/121496770?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.pc_relevant_default&spm=1001.2101.3001.4242.3&utm_relevant_index=7
        // 2.Compile and debug c++ multi-folder project under VSCode (non-makefile)
        // https://blog.csdn.net/BaiRuichang/article/details/106463035
        // I also put the main.o under my current workspace directory
        // This after "-o"  is the target file test.o or test.out or test
        "${workspaceFolder}/${fileBasenameNoExtension}", // Have changed
        "-I",
        // This after "-I" if the include files directory
        "${workspaceFolder}", // Have changed
        "-Wall",
        "-g"
      ],
      "options": {
        // "cwd" is the source files directory
        "cwd": "${workspaceFolder}" // Have changed
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

"launch.json":

代码语言:javascript
复制
{
  // 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": "Debug With LLDB",
      "type": "lldb",
      "request": "launch",
      // "program" is the target file diretory
      "program": "${workspaceFolder}/${fileBasenameNoExtension}", // Have changed
      "args": [],
      "stopAtEntry": true,
      //"cwd": "${workspaceFolder}/../build",// Have changed
      //"cwd": "${fileDirName}", ${workspaceFolder}/../build
      // Changes the current working directory directive ("cwd") to the folder
      // where main.cpp is.
      // This "cwd" is the same as "cwd" in the tasks.json 
      // That's the source files directory
      "cwd": "${workspaceFolder}", // Have changed
      "environment": [],
      "externalConsole": false,
      "preLaunchTask": "Compile With clang++"
    }
  ]
}

现在,"c_cpp_properties.json“

代码语言:javascript
复制
{
  "configurations": [
    {
      "name": "Mac",
      "includePath": [
        // This is the include files directory
        "${workspaceFolder}/**", // Have Change
        "/Library/Developer/CommandLineTools/usr/include",
        "/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include",
        "/usr/local/include",
        "/Library/Developer/CommandLineTools/usr/include/c++/v1",
        "/usr/include"
      ],
      "defines": [],
      "macFrameworkPath": [
        "/System/Library/Frameworks",
        "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
        "/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++11",
      "intelliSenseMode": "clang-x64"
    }
  ],
  "version": 4
}

告示:

您只需要修改Include path设置,如果程序包括

不在工作区或标准库路径中的头文件。

特别是:"includePath": [ "/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include"]

如果分隔命令行工具的Clang版本为9.0.0,请复制此程序

直接了当。

如果没有,只需使用以下命令签出分隔的Clang版本

命令行工具:

cd /Library/Developer/CommandLineTools/usr/lib/clang/ && ls

可能是这样的:9.0.09.1.010.0.0或X。

分隔命令行工具的Clang版本是9.0.0,或9.0.1

10.0.0X.X.X.只需替换我的9.0.0

如果您没有单独的命令行工具,只需安装分开的命令

根据您的macOS系统的不同,您的Mac的行工具。

我将通过添加文件添加这5个文件。

结束。

最后,

如果需要构建分离的多个源文件,

那种建筑就像:

代码语言:javascript
复制
fitBody

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__build

|__inc

|     |__fitbody.h

|__src

     |__fitbody.cpp
 
     |__main.cpp

请参考手动分离多个CXX VScode(mac)从GitHub吉斯特。

或者,引用来自GitHub的GitHub(Mac)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69443832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档