c++跨多个.cpp文件调用函数?

标签 c++ function call header-files

我有 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/

相关文章:

c++ - 将二进制文件读入 vector 元素的地址

c++ - 以多态方式删除对象

javascript - 从字符串名称动态创建 JavaScript 函数

ios - 在 Swift 中调用结构体的方法

c++ - 类中的 const 成员出错

C++ 接口(interface)与模板

c++ - 将字符串文字传递给函数并分配给成员变量

function - Emacs 定义一个将参数设置为 nil 的函数

c++ - 没有匹配函数调用 'ANDingOctets'

javascript - 如何将 JS 函数的结果解析为 SVG 对象?