如何在 c++ 中导入文件?以及如何成功编译它们?
How to import files in c++? and how to compile them successfully?
我正在尝试用c ++将我的代码分解为多个文件。我的主文件是 base.cpp我正在尝试在其中导入 foo.cpp。但它在编译过程中显示以下错误。
建筑x86_64的未定义符号: "foo((",引用自: _main在base-a3f2f3.o中 LD:在建筑x86_64中找不到符号
我的基本.cpp文件是:
#include<bits/stdc++.h>
#include "foo.h"
using namespace std;
int main(){
foo();
cout<<tot<<endl;
cout<<"bye"<<endl;
}
我的foo.cpp是:
#include<bits/stdc++.h>
using namespace std ;
void foo(){
cout<<"hello world"<<endl;
}
int tot = 90;
我的 foo.h 文件是:
#ifndef FOO_H
#define FOO_H
int tot;
void foo();
#endif
我使用以下命令成功编译我的c ++文件: g++ -std=c++14 filename.cpp && ./a.out
我使用Mac。我的代码编辑器是VS Code。
My cpp configuaration is as follows :
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
谢谢。
您必须编译所有 cpp 文件:
g++ -std=c++14 base.cpp foo.cpp && ./a.out
这不是问题的一部分,但您应该避免#include<bits/stdc++.h>
。它是一个实现定义的标头
相关文章:
- VS2017 版本 15.8.3 成功编译内联方法,而不返回所需值
- 如何在 c++ 中导入文件?以及如何成功编译它们?
- 奇怪的语法构造 - 应用于 typedef 的运算符 () 使用 VS2015 成功编译
- "template"关键字限定符是否会导致代码成功编译,但不同?
- 使用不带前缀"std"且不带"using namespace std;"的 std::sort() 成功编译
- 在 C++11 和 C++11 之前成功编译但行为不同的代码示例
- 有没有人在Windows7机器上成功编译过木瓜
- CGAL:如何成功编译和链接CGAL示例(在Mac OS X 10.9 Mavericks上)
- 使用 qmake 成功编译 SSE 指令(但 SSE2 不被识别)
- Socket C++程序在运行时成功编译崩溃
- 在 clang 中显式指定的参数无效,但在 gcc 中成功编译 — 谁错了?
- 为什么这个模板函数能够成功编译
- 无法使用Xcode成功编译boost
- Eclipse可以成功编译,但仍然会出现语义错误
- 成功编译节点模块并"Module did not self-register."
- Eclipse 给出错误"无法解析函数'MessageBoxA'",但仍然成功编译?
- C++ 成功编译,不包含<string>标题
- CGAL4.6 中成功编译的代码的编译错误
- 捕获而不尝试成功编译
- 为什么以下代码在VS2010中成功编译