Dan*_*ple 5 c++ linux ubuntu gcc visual-studio-code
Note that I'm using VS Code on Ubuntu 17.10 and using the GCC Compiler.
I'm having trouble building a simple program which makes use of additional .ccp files. I'm probably missing something obvious here as I'm fairly new to programming but I'll explain what I've done so far. This is something that is stopping me from continuing with a tutorial I'm doing.
I have written a very simple program to demonstrate my point as follows.
#include <iostream>
#include "Cat.h"
using namespace std;
int main ()
{
speak();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#pragma once
void speak();
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include "Cat.h"
using namespace std;
void speak()
{
std::cout << "Meow!!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
This simple program builds in both Codeblocks and Visual Studio Community 2017 no problem and I can't figure out what I need to do to make it run. This error at the bottom indicates that the Cat.ccp file is not being picked up at all. If I was to drop the definition from this Cat.ccp into the header file the program compiles and runs fine but I need to make use of that .ccp file as I understand this is the accepted way of separating code. I'll note that all the files are in the same folder too.
I understand that I may need to tell VS Code where to look for the Cat.ccp file but it's strange to me that it finds the header in the same location. Nevertheless, after looking online I've been reading that I may need to place a directory into the c_cpp_intellisense_properties.json. Here's what mine looks like.
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
},
{
"name": "Linux",
"includePath": [
"/usr/include/c++/7",
"/usr/include/x86_64-linux-gnu/c++/7",
"/usr/include/c++/7/backward",
"/usr/lib/gcc/x86_64-linux-gnu/7/include",
"/usr/local/include",
"/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include",
"/home/danny/Documents/C++_Projects/24_-_Classes/Cat.cpp",
"${workspaceRoot}",
"/home/danny/Documents/C++_Projects/24_-_Classes/",
"/home/danny/Documents/C++_Projects/24_-_Classes/.vscode",
"/home/danny/Documents/C++_Projects/24_-_Classes/.vscode/Cat.cpp"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include/c++/7",
"/usr/include/x86_64-linux-gnu/c++/7",
"/usr/include/c++/7/backward",
"/usr/lib/gcc/x86_64-linux-gnu/7/include",
"/usr/local/include",
"/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include",
"${workspaceRoot}",
"/home/danny/Documents/C++_Projects/24_-_Classes/",
"/home/danny/Documents/C++_Projects/24_-_Classes/.vscode/Cat.cpp"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Win32",
"includePath": [
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
"${workspaceRoot}"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 3
}
Run Code Online (Sandbox Code Playgroud)
At one point I wondered if I need to add a double command in there to tell the compiler to build both .ccp files in the tasks.json but I haven't managed to figure out how to do that, or even if that's the right approach.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "g++ -g /home/danny/Documents/C++_Projects/24_-_Classes/main.cpp -o Classes",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher":"$gcc"
}
]
}
Run Code Online (Sandbox Code Playgroud)
Appreciate any help. And just in case you're wondering, the reason I don't just finish the tutorial in Codeblocks or VS Community is that I like to know what's going on under the hood in most things. Plus I'd like to get VS Code working for me as it's been great so far.
Ann*_*s98 42
在tasks.json中:
"label": "g++.exe build active file",
"args": [
"-g",
"${fileDirname}\\**.cpp",
//"${fileDirname}\\**.h",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
Run Code Online (Sandbox Code Playgroud)
在launch.json中:
"preLaunchTask": "g++.exe build active file"
Run Code Online (Sandbox Code Playgroud)
如果您的来源位于单独的文件夹中,它将起作用
kzs*_*kzs 11
觉得很懒,
这是 linux 发行版 vscode 的 tasks.json,用于编译多个 cpp 文件。
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
Run Code Online (Sandbox Code Playgroud)
}
这是针对同一问题的 Windows 答案:
我也在为此苦苦挣扎,直到我在https://code.visualstudio.com/docs/cpp/config-mingw找到以下答案:
您可以修改 tasks.json 以使用类似参数
"${workspaceFolder}\\*.cpp"
而不是.json 来构建多个 C++ 文件${file}
。这将在>您的当前文件夹中构建所有 .cpp 文件。您还可以通过替换"${fileDirname}\\${fileBasenameNoExtension}.exe"
为硬编码文件名(例如 >example"${workspaceFolder}\\myProgram.exe"
)来修改输出文件名。
请注意,workspaceFolder 中的 F 是大写的。
举个例子,在我的项目中的tasks.json文件中,“args”下括号内的文字原来是这样的:
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
Run Code Online (Sandbox Code Playgroud)
这给了我参考错误,因为它只编译一个而不是我的两个文件。
但是,在将该文本更改为以下内容后,我能够使程序运行:
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
Run Code Online (Sandbox Code Playgroud)
小智 7
如果你有多个文件并且一个文件依赖于cpp
另一个文件,你需要告诉 g++ 编译它,以便链接器可以找到它。最简单的方法是:
$ g++ Cat.cpp main.cpp -o Classes
Run Code Online (Sandbox Code Playgroud)
作为旁注,您可能应该在编译时发出警告,至少-Wall
,可能-Wextra
和可能-Wpedantic
,以便您知道您正在做的事情是否有问题。
小智 5
找到很多解决方案后,我发现这只能工作,安装代码运行器扩展。在设置中,转到右上角的打开设置并将此行粘贴到最后一个右括号末尾之前的setting.json中}
"code-runner.executorMap": {
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
Run Code Online (Sandbox Code Playgroud)
[settings.json --> 文件看起来像]:https://i.stack.imgur.com/pUBYU.png
归档时间: |
|
查看次数: |
6767 次 |
最近记录: |