cannot run code (error: no such file or directory) but can compile file C++ VSCode

cannot run code (error: no such file or directory) but can compile file C++ VSCode

文件夹 /home/cyan/proj10

中的文件结构
fst
 | -- include
 |     |-- fstlib
 |            |-- fst_reader.h
 |
 | -- lib
       |-- libfst.so

include
 | -- A.h
 | -- B.h

src
 | -- A.cc
 | -- B.cc

main.cc

CMakeLists.txt

fst文件夹是我添加的库

CMakeList.txt

cmake_minimum_required(VERSION 3.0.0)
project(READ_FST VERSION 0.1.0)
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_BUILD_TYPE Debug)

include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/fst/include)
link_directories(${CMAKE_SOURCE_DIR}/fst/lib)
add_executable(READ_FST main.cc src/A.cc src/B.cc)
target_link_libraries(READ_FST ${CMAKE_SOURCE_DIR}/fst/lib/libfst.so)

A.h

#pragma once
#include <B.h>
namespace me {
class A{
public:
    void init();
public:
    double func(double a, double b, double c);
private:
    B b_;
};
}

A.cc

#include <A.h>
namespace me {
void A::init() {
    b_.init(1.0, 2.0, 1.0);
}
double A::func(double a, double b, double c) {
    return b_.func(a, b, c);
}
}

main.cc

#include <A.h>
#include <B.h>
#include <iostream>
#include <./fstlib/fst_reader.h>

int main()
{
    std::cout << "Hello World" << std::endl;
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "defines": [],
            "configurationProvider": "ms-vscode.cmake-tools",
            "includePath": ["${workspaceFolder}/**"],
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                "-g",
                "-Wall",
                "-I${workspaceFolder}/include",
                "-I${workspaceFolder}/fst/include",
                "${file}",
                "-o",
                "${workspaceFolder}/build/${fileBasenameNoExtension}",
            ],
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "group": {
            "kind": "build",
            "isDefault": true
            },
        }
    ],
    "version": "2.0.0"
}

问题

我可以编译活动文件并且智能感知很好。但是每次我点击 Run Code 按钮时,都会出现以下错误:

cyan@machine:~/proj10$ cd "/home/cyan/proj10/" && g++ main.cc -o main && "/home/cyan/proj10/"main
main.cc:1:10: fatal error: A.h: No such file or directory
 #include <A.h>
          ^~~~~
compilation terminated.

好的

第一个

尝试使用 CMake extension

第二

Run Code 做一件简单的事情 - 它编译并运行从您当前的文件中提取程序。

cd "/home/cyan/proj10/" && g++ main.cc -o main && "/home/cyan/proj10/"main

此时的编译对您的 CMake 项目、编译标志、包含路径等一无所知。

如果您尝试以这种方式构建整个 CMake 项目,那将行不通。 您需要准确地构建 CMake 项目,并且 运行 准确地构建已编译的应用程序(最简单的方法是使用第一段中的扩展)。