我正在尝试在 macOS 上使用 clang++ 构建一个包含多个 *.cpp 文件的简单项目。
任务.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-Wall",
"-Wextra",
"-Weffc++",
"-Wconversion",
"-pedantic-errors",
"-stdlib=libc++",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
结果命令如下所示:
/usr/bin/clang++ -std=c++17 -Wall -Wextra -Weffc++ -Wconversion -pedantic-errors -stdlib=libc++ -g '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp' -o '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/main'
Run Code Online (Sandbox Code Playgroud)
但编译器会抛出错误:
clang: error: no such file or …Run Code Online (Sandbox Code Playgroud) 为了学习 C++,我在 Mac 上安装了带有 Catalina 的 Visual Studio Code。安装的扩展C/C++、C/C++ Extension Pack、C++ Intellisense和CMake Tools。Code Runner
为了测试 VSCode,我尝试运行以下代码:
再见.cpp:
#include <iostream>
void tryMe(int s) {
std::cout << "ok";
}
Run Code Online (Sandbox Code Playgroud)
再见.h:
void tryMe(int s);
Run Code Online (Sandbox Code Playgroud)
你好.cpp:
#include <iostream>
#include "bye.h"
int main() {
tryMe(3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它不会运行,因为它会导致编译错误:
$ 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 …Run Code Online (Sandbox Code Playgroud) 我已经按照一些说明构建了Visual Studio Code C/C++编译和调试环境。但是g++编译器只能编译选定的cpp文件,因此无法编译与cpp文件相关联的.h文件。然后终端显示“架构 x86_64 的未定义符号”错误。代码如下:
int func();
Run Code Online (Sandbox Code Playgroud)
include <iostream>
include "a.h"
using namespace std;
int func(){
return 111;
}
Run Code Online (Sandbox Code Playgroud)
include "a.h"
using namespace std;
int main()
{
int b = func();
cout << b << endl;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 代码将使用如下命令
g++ directory/main.cpp -o directory/main.out -g -Wall -fcolor- diagnostics -std=c++11
Run Code Online (Sandbox Code Playgroud)
此命令将引发“体系结构 x86_64 的未定义符号”错误我可以使用此新命令修复它
g++ main.cpp a.cpp -o main.out.
Run Code Online (Sandbox Code Playgroud)
所以问题是如何配置这些 json 文件来修复 g++ 编译问题。当我想使用某些库(例如 FFMpeg)时,如何正确链接 FFMpeg .h 文件。
主要.cpp:
#include <iostream>
#include "Person.hpp"
int main()
{
std::cout << "HELL!\n";
Person a{"Jiraya"};
std::cout << a.getName() << "\n";
a.setName("Niko");
a.do_smt();
}
Run Code Online (Sandbox Code Playgroud)
人.hpp:
#pragma once
#include <string>
using std::string;
class Person
{
private:
string name;
public:
Person();
Person(const string &n);
void do_smt();
string getName(){return name;}
void setName(const string& n);
Run Code Online (Sandbox Code Playgroud)
人.cpp:
#pragma once
#include <iostream>
#include "Person.hpp"
Person::Person(const string &n) : name{n}
{
}
void Person::setName(const string &n)
{
name = n;
} …Run Code Online (Sandbox Code Playgroud)