在Linux系统上运行cpp文件会创建另一个不执行的文件吗? 我正在学习C++,但是我不能运行任何代码。 #include <iostream> int main() { std::cout << "Hello world!"; return 0; }
这是任务文件:在Linux系统上运行cpp文件会创建另一个不执行的文件吗? 我正在学习C++,但是我不能运行任何代码。 #include <iostream> int main() { std::cout << "Hello world!"; return 0; },c++,visual-studio-code,C++,Visual Studio Code,这是任务文件: { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++ build active file", "command": "/usr/bin/g++", "args": [ "-g", "${file}",
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
settings.json文件为空
无论是在没有调试的情况下运行,还是在没有调试的情况下运行,都会产生相同的错误。我怎样才能解决这个问题
尝试的方法:
1) 尝试重新启动应用程序和计算机,但不起作用
2) 尝试使用终端编译、运行和调试代码它可以工作,但是有没有一种方法可以在VS代码上实现这一点?(我应该为VS代码再问一个问题吗?)
感谢大卫·C·兰金的帮助。关于这个问题的评论中有更多信息
3) 使用CMake(由dboy建议)。我没能做到这一点。在输出部分显示生成后,我无法找到运行HelloWorld应用程序的文件。(图3显示了这些文件)
CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.10.2)
project("HelloWorld")
set(SOURCE Helloworld.cpp)
add_executable("HelloWorldBuild" HelloWorld.cpp)
编译时没有显示任何问题。CMake现在对我来说可能是高级的。你已经展示了你的启动配置,但是现在你是如何编译文件的。@rustyx很好。这个对话框在我看来很像Windows10,我错了。用户在Linux上,但是使用VSCode如果你在Linux上运行VSCode,那么在对抗任何IDE时都要小心。只需打开任何xterm,更改到包含源文件的目录,然后
g++-Wall-Wextra-pedantic-Wshadow-std=c++11-g-o nameforexe source.cpp
(根据需要更改标准)。然后可以运行/nameofex
或在gdb./nameofex
中运行它进行调试。调试完成后,将-g
替换为-O3
(即dash Oh 3
)。生成任务似乎未通过预处理。您还需要发布生成任务。您已经展示了启动配置,但现在您可以编译文件了。@rustyx说得对。这个对话框在我看来很像Windows10,我错了。用户在Linux上,但是使用VSCode如果你在Linux上运行VSCode,那么在对抗任何IDE时都要小心。只需打开任何xterm,更改到包含源文件的目录,然后g++-Wall-Wextra-pedantic-Wshadow-std=c++11-g-o nameforexe source.cpp
(根据需要更改标准)。然后可以运行/nameofex
或在gdb./nameofex
中运行它进行调试。调试完成后,将-g
替换为-O3
(即dash Oh 3
)。生成任务似乎未通过预处理。您还需要发布构建任务。
# First, change your directory to the one that you want to use
# Run the following to compile in the same directory:
g++ -Wall -Wextra -pedantic -Wshadow -std=c++11 -g -o nameforexe source.cpp
# Change -std=c++11 to whatever version of c++ you want to use, and also the file names.
# To run it after this normally do this:
./nameforexe
# To debug, do:
gdb ./nameforexe
# After gdb opens, write "run" without " to run the file.
# If there is a problem, it will tell you. If there isn't it will run normally.
cmake_minimum_required(VERSION 3.10.2)
project("HelloWorld")
set(SOURCE Helloworld.cpp)
add_executable("HelloWorldBuild" HelloWorld.cpp)