未定义的引用;类公共函数在链接时不可访问
undefined reference to; class public function not accessible at link time
我正在尝试构建一个简单的程序,其中我定义了一个类并将其标头包含在 Main 中。在链接时,Linker 抱怨从类访问任何成员函数:
: undefined reference to voxel::anyFunction
即使函数是公共的并且包含标头。
最初我在创建体素对象时发现了这个问题 - 我重载了默认构造函数,但我发现体素类中的任何函数都存在问题。
以下是一些代码摘录:
voxel.hpp
class voxel
{
public:
//here defined some member variables
//ommited the constructor
void fillMemberValuesWithDummy();//sets all members to some dummy value
};
体素.cpp
#include "voxel.hpp"
void voxel::fillMemberValuesWithDummy()
{
//does the assignment to member variables
}
主.cpp
#include <iostream>
#include <fstream>
using namespace std;
#include "voxel.hpp"
{
voxel someVoxel;
somevoxel.fillMemberValuesWithDummy();
}
我认为这是我在这里(不是)做的事情非常愚蠢,但你能告诉我什么吗?
您需要链接所有目标文件才能获取可执行文件。当你只有两个源文件时,你可以直接编译它们:
g++ -o myprog.exe Main.cpp voxel.cpp
当你想划分编译和链接并这样做时:
g++ -c -o Main.o Main.cpp
g++ -c -o voxel.o voxel.cpp
g++ -o myprog.exe Main.o voxel.o
随意创建一个适当的生成文件来生成此类命令。
如果您的操作系统不需要.exe
,请将其删除。
相关文章:
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- 链接 boost 库时"Error while loading shared libraries"引发的,除了我无法使用 root 访问权限来修复它
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 如何合并两个双重链接列表(访问下一个链接)
- 读取访问违规当前为0xCDCDCDCD,链接列表窗口
- 链接列表节点的访问违规:C
- 静态成员函数访问静态私有变量时的链接器错误
- 在删除双重链接列表中的最后一个元素时,请阅读访问违规行为
- 访问链接的列表项目从参考这些项目的参考文献中
- 如何在链接列表中访问下一个C
- 发生 JNI 调用 c++ dll "不满意链接错误: 对内存位置的无效访问"
- 访问注册表数据以设置链接器和包含路径
- 链接时突然无法访问私有方法
- 在单个链接列表中间删除一个节点,仅给定对该节点的访问
- 链接的不同类的列表,如何使用指针访问特定类功能
- Java/Python在C/C++中访问的动态链接库
- 未定义的引用;类公共函数在链接时不可访问
- 创建链接列表时发生访问冲突错误
- 动态链接 ~ 限制 DLL 的系统访问