优化源代码结构的c++项目
Optimize source structure for C++ projects
我想创建一个小项目,它分为多个文件。main.cpp:
#include <cstdlib>
#include <iostream>
#include sc_hpp
using namespace std;
int main(int argc, char *argv[])
{
add(3,4);
system("PAUSE");
return EXIT_SUCCESS;
}
sc.hpp:
#ifndef "sc.hpp"
#define sc_hpp
int add(int a, int b);
#endif
function.cpp:
#include "sc.hpp"
int add(int a, int b)
{
return(a + b);
}
但是它不起作用。错误:
`add' undeclared (first use this function)
我第一次尝试在多个文件中制作程序,所以我认为这个问题一定很容易解决。
你有两个明显的错误:
在你的main
:
#include <cstdlib>
#include <iostream>
// the included header file needs to be enclosed in " "
// and it needs a suffix, i.e.: `.h`
#include "sc_hpp.h"
using namespace std;
int main(int argc, char *argv[])
{
}
In sc.hpp:
// the include guards doesn't have to be enclosed in " "
// the suffix's dot:'.' is replaced with underscore: '_'
// header name in uppercase letters
#ifndef SC_HPP_H
#define SC_HPP_H
int add(int a, int b);
// included .cpp files with function implementation here
#include "sc.hpp"
#endif
关于如何组织代码文件的更多信息,请点击这里。
一般来说,预处理器指令#include
扩展了它后面的文件中包含的代码,所以你在main
中的代码看起来像这样:
#include <cstdlib>
#include <iostream>
// #include "sc_hpp.h" replaced with
int add(int a, int b);
// the #include "sc.cpp" nested within the "sc_hpp.h" is replaced with
int add(int a, int b)
{
return(a + b);
}
using namespace std;
int main(int argc, char *argv[])
{
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 错误-我无法在VS2019中打开新的Qt项目
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗