【问题标题】:Cannot compile my c++ program using g++ in linux无法在 linux 中使用 g++ 编译我的 c++ 程序
【发布时间】:2019-07-21 20:28:58
【问题描述】:

我正在使用 C++ 开发一个小型迷宫游戏。我的问题:我喜欢 Visual Studio Code for Linux,当我运行我的程序时,我收到以下错误:

main.cpp:2:10:fatal error: Player.h: No such file or directory

包括“Player.h”

我使用以下文件夹结构设置这个项目:

Game
|-------include
         |----------GameMap.h
         |----------Map.h
         |----------MapCell.h
         |----------Player.h
|-------src
         |----------GameMap.cpp
         |----------Map.cpp
         |----------MapCell.cpp
         |----------Player.cpp
|-------main.cpp

这个项目在我使用的另一台计算机上运行,​​当我使用代码块时它也运行。但是因为我喜欢继续使用可视化代码,所以我希望能够在使用 Visual Studio 代码时编译和运行我的 C++ 代码。

g++ -o main main.cpp

编辑:

c_cpp_properties.json

{  
   "configurations":[  
      {  
         "name":"Linux",
         "includePath":[  
            "${workspaceFolder}/**",
            "/home/roger/Documents/01.Curso_de_CPP/Projecto/Game_02/Headers/include/**"
         ],
         "defines":[  
            "_DEBUG"
         ],
         "forcedInclude":[  
            "/home/roger/Documents/01.Curso_de_CPP/Projecto/Game_02/Headers/include/Player.h"
         ],
         "compilerPath":"/usr/bin/gcc",
         "cStandard":"c11",
         "cppStandard":"c++17",
         "intelliSenseMode":"clang-x64"
      }
   ],
   "version":4
}

【问题讨论】:

  • 你设置了包含路径吗?
  • 对不起。我是 C++ 新手,不知道该怎么做。我使用代码块作为编辑器启动了这个项目,它在目录中找到了头文件。我以前没有这个链接器问题。但我不知道如何在可视代码中设置我的项目,以便我的 main.cpp 编译。
  • 也许可以读一下:C/C++ for Visual Studio Code
  • undefined reference to ‘Player::Player()‘ 表示它可以编译,但不能正确链接。 Player 的目标文件未链接到可执行文件。

标签: compilation header g++


【解决方案1】:

虽然我还是 我不明白链接器的问题,我可以用这个来解决它:

g++ -o main main.cpp bin/Player.o -std=c++0x -Iinclude

通过阅读这篇文章的答案: What's the point of including -std=c++0x in a G++ compile command?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多