Visual Studio代码与C++; 我在卡塔利娜上安装了Mac的VisualStudio代码,以便学习C++。已安装的扩展C/C++,C/C++扩展包,C++Intellisense,CMake工具和代码运行程序
要测试VSCode,我尝试运行以下代码: 再见.cpp:Visual Studio代码与C++; 我在卡塔利娜上安装了Mac的VisualStudio代码,以便学习C++。已安装的扩展C/C++,C/C++扩展包,C++Intellisense,CMake工具和代码运行程序,c++,visual-studio-code,linker-errors,C++,Visual Studio Code,Linker Errors,要测试VSCode,我尝试运行以下代码: 再见.cpp: #include <iostream> void tryMe(int s) { std::cout << "ok"; } hello.cpp: #include <iostream> #include "bye.h" int main() { tryMe(3); return 0; } 我理解问题发生的原因:编译没有包含bye.cp
#include <iostream>
void tryMe(int s) {
std::cout << "ok";
}
hello.cpp:
#include <iostream>
#include "bye.h"
int main() {
tryMe(3);
return 0;
}
我理解问题发生的原因:编译没有包含bye.cpp
文件,因此无法识别函数。如果我使用g++hello.cpp bye.cpp-o hello
通过终端进行编译,它编译良好并按预期运行
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
我已经搜索并看到了一些提到“任务”文件的文章,但无法理解如何实现它,或者它来自何处。这是否回答了您的问题?编译器本身只处理单个源文件及其包含的所有头文件。它不知道其他源文件,您必须显式生成并链接所有源文件。一旦您的项目中有多个源文件,我建议您使用某种项目或生成系统,以便正确处理所有涉及的源文件。现在很流行。关于如何将CMake及其生成的生成文件集成到Visual Studio代码中,有大量的在线教程和示例。@某个程序员说,在我的项目中,VSCode上没有编译和运行多个文件的选项,我必须从外部执行吗?我找不到控制VSCode上编译参数的方法。Visual Studio代码在其最基本的级别上只是一个纯文本编辑器。如果您想要内置项目管理和处理多个源文件,我可以建议您使用完整的IDE,例如Visual Studio社区吗?如果您愿意的话,还有其他免费的开源IDE使用MinGW。
$ cd "/Users/x/Workspace/LearnCPP/" && g++ hello.cpp -o hello && "/Users/x/Workspace/LearnCPP/"hello
Undefined symbols for architecture x86_64:
"tryMe(int)", referenced from:
_main in hello-ef5e99.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4