c++ - 使用命名空间 C++ 的 undefined reference

标签 c++ namespaces undefined-reference

当我想运行代码时,编译器说 undefined reference to math::calc我在 StackOverflow 上阅读了有关此问题的问答,但它并不能帮助我解决问题。
Comp.h

#include <utility>
namespace math {
    typedef std::pair<double,double> Comp;
    double getFirst(Comp a);
    ...
}

压缩文件
#include "comp.h"
namespace math {
    double getFirst(Comp a) {
        return a.first;
    }
    ...
}
Comp文件:每个函数返回 Compdouble .我从 cal.cpp 调用函数多次归档
校准时间
#include "comp.h"
#include "helper.h"
namespace math {
    Comp calc(const string& str);
    ...
}
调用程序
#include "eval.h"
namespace math {
    Comp calc(const string& str) {
        ...
    }
}
Cal文件:一些函数返回 comp类型,而不仅仅是 cal功能。
helper.h
namespace math {
    ...
}
helper.cpp
#include "helper.h"
namespace math {
    ...
}
helper文件只包含我从 cal.cpp 调用的几个函数文件。每个函数都会调用多次。
主程序
#include "calc.h"
int main() {
    string str = "  ";
    pair<double,double> res = math::calc(str);
    cout << res.first << " " << res.second << endl;
    return 0;
}
在整个项目中,我没有使用任何类。
我包含了我调用的每个文件,除了 c++ 原始文件。
我在每个文件中使用 std 命名空间,但我没有在这里写。
我完全不知道我的代码可能有什么问题。
如果我还包括 cal.cppmain.cpp代码编辑器说 undefined reference到我从 helper.h 调用的每个文件.我不想包括 cal.cppmain我刚刚提到的文件

最佳答案

您必须编译所有项目的 *.cpp 文件,然后正确链接它们(编译结果) - 链接过程取决于您使用的环境/IDE,只需查找即可。如果您没有正确链接它,最终的可执行文件将不会具有所需的所有函数定义,因此会获得 undefined reference 。

关于c++ - 使用命名空间 C++ 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65813516/

相关文章:

c++ - 如何让 vc++ 和 g++ 在依赖限定类型的类型名方面表现相同?

Javascript 命名空间和私有(private)模块

symfony - 最佳实践 Composer 和 Symfony 包

c++ - 初始化命名空间中的 const 对象

c - 未定义对 'function' 的引用,但包含 header

c++ - 对父类中方法的 undefined reference

c# - 从 C# 在 .lib 文件中创建 C++ 类的实例

c++ - 使用 gdb 进行核心转储分析

c++ - C++ 中的 VisualState 示例?

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?