Home > Back-end >  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

Time:03-17

File structure in folder /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 folder is a library I added.

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"
}

Problem

I can compile active file and intellisense is fine. But every time I click the Run Code button, it gives the following error:

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.

CodePudding user response:

Okay

First

Try use CMake extension

Second

Run Code does one simple thing - it compiles and runs the program from your current file.

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

Compilation at this point knows nothing about your CMake project, compilation flags, include paths, etc.

If you're trying to build your entire CMake project this way, it won't work. You need to build exactly the CMake project, and run exactly the compiled application (the easiest way to do this is with the extension from the first paragraph).

  • Related