如何将代码划分为多个.cpp文件 C++.

  • 本文关键字:cpp 文件 C++ 代码 划分 c++
  • 更新时间 :
  • 英文 :


我想在main.cpp文件之外创建一个函数 我尝试创建一个头文件,但它不起作用:

主.cpp:

#include "other.h"
int main() {
MyFunc();
}

其他.cpp

#include <iostream>
#include "other.h"
void MyFunc() {
std::cout << "Ohai from another .cpp file!";
std::cin.get();
}

其他.h

#include <iostream>
#include "other.cpp"
void MyFunc();

也不是CPP,G ++,GCC编译器工作

海湾合作委员会编译错误 错误由 vs 代码显示

必须包含头文件而不是C++文件。

因此,您需要删除:

#include "other.cpp"

other.h并使用以下命令行进行编译:

g++ -o output main.cpp other.cpp

你会把它链接起来,然后编译,然后一切都应该工作正常。

您必须删除头文件中 #include"other.cpp"。

删除 other.h 中的"#include "other.cpp"行,你会没事的......

编辑:您还需要一个标题护罩...

最新更新