我有 3 个文件: - main.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.
非常感谢任何关于为什么我的头文件/调用不起作用的解释。
最佳答案
导致 undefined reference 错误的原因有很多。在你的情况下,根据你的描述,最有可能的是你没有编译 other.cpp
您需要将 other.cpp 添加到您的项目/makefile/命令行中。
要获得更详细的帮助,您可能需要解释您是如何构建程序的。代码实际上没有任何问题,问题在于您构建程序的方式。
关于c++跨多个.cpp文件调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19320131/