c++ 跨多个.cpp文件调用函数
c++ Calling functions across multiple .cpp files?
我有 3 个文件:- 主要.cpp- 其他.cpp- 其他.h
我正在尝试从main.cpp调用在other.h中声明的函数,代码是用other编写的.cpp
我的代码:
主.cpp:
//#Allmyincludes
#include "other.h"
int main(int argc, const char *argv[])
{
const char *file = argv[1];
read(file);
return 0;
}
其他.cpp:
//#Allmyincludes
#include "other.h"
void read(const char *file)
{
//code
}
其他.h:
void read(const char *file);
我得到的错误是:
main.cpp:(.text+0x44): undefined reference to 'read(char const*)'
在 read()
中,我正在使用 main::variableName
从 main 内部访问变量(没有错误(,但是我只是无法让函数调用工作。如果我尝试做other::read(file);
那也不起作用::
因为它用于函数而不是文件。它给了我错误:'other' has not been declared.
非常感谢关于为什么我的头文件/调用不起作用的任何解释。
导致未定义引用错误的原因有很多。就您而言,根据您所描述的内容,最有可能的是您没有编译其他.cpp
您需要将 other.cpp 添加到您的项目/makefile/命令行中。
有关更详细的帮助,您可能需要解释如何构建程序。代码实际上没有任何问题,问题在于您构建程序的方式。
试试
gcc -墙主.cpp其他.cpp -o 我的主
相关文章:
- 编译多个C++文件.调用二进制文件以运行代码
- 无法从 SO 文件调用 SO 文件的函数 - C++生成文件
- 如何从另一个文件调用类构造函数?
- 调用函数一次用于动态链接库,一次从可执行文件调用函数
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 从 Android 中的 C 文件调用C++函数
- 从外部文件C++调用函数
- 如何从多个 cpp 文件调用函数
- 从用户定义的头文件调用函数时出现未定义的引用错误,其实现位于.cpp文件中
- cmake:生成批处理文件调用cl.exe
- 在文件中生成随机数,然后从文件调用它们,然后将它们打印出来
- main.cpp从 .h 文件调用公共虚拟方法
- 如何从 python (py) 文件调用 C++ 函数
- 如何从 cpp 文件调用 setRequestOrientation()
- 调试Visual Studio中可执行文件调用的C 文件
- 可以使用不同的头文件调用相同的函数吗?
- 从 Android 中的原生 c++ 文件调用本机 opencv
- 使用对象文件调用Winapi功能
- 从头文件调用字符串getter函数
- 方法从头文件调用