仅在使用MinGW / GCC编译器时出现“未定义的引用”,这是一个模棱两可的问题吗?

如何解决仅在使用MinGW / GCC编译器时出现“未定义的引用”,这是一个模棱两可的问题吗?

仅当使用MinGW / GCC编译器时,我才有一个模棱两可的问题,在Visual Studio 2019上效果很好。

首先,错误消息:

  • undefined reference to Binary::Length()
  • undefined reference to Binary::operator[](unsigned long long)

第二,代码:

(错误消息指向的代码部分(与“ File”类有关))

bool File::AppendBinary(const String filePath,Binary& binaryData) {
    std::ofstream WFile(std::filesystem::path(filePath.ToCString()),std::ios_base::binary | std::ios_base::app);
    if (!WFile.is_open()) return false;
    WFile.write((char*)&binaryData[0],binaryData.Length());  // <<--- "The error messages point to this line"
    WFile.close();
    return true;
}

(二进制类)

binary.h

class Binary {
    std::vector<unsigned char> m_binary;
public:
    Binary() = default;
    Binary(const uintmax_t containerSize);
    Binary(unsigned char binary);
    Binary(::std::initializer_list<unsigned char> binaryList);
    ~Binary() = default;
    uintmax_t Length();
    unsigned char& operator[](uintmax_t index);
};

binary.cpp

Binary::Binary(const uintmax_t containerSize) {
    m_binary.resize(containerSize);
}

Binary::Binary(unsigned char binary) {
    m_binary.push_back(binary);
}

Binary::Binary(std::initializer_list<unsigned char> binaryList) {
    m_binary.assign(binaryList.begin(),binaryList.end());
}

uintmax_t Binary::Length() {
    return m_binary.size();
}

unsigned char& Binary::operator[](uintmax_t index) {
    if (index > m_binary.size() - 1)
        return m_binary[0];
    return m_binary[index];
}

使用时:

File file ("C:\\ff.txt");

仅在使用构造函数而不使用方法时才发生该问题。 另外,令我抓狂的是直接使用二进制类时不会发生问题,例如:

Binary bin ((uintmax_t)5);
bin = {0x10,0x20,0x30,0x40};
for(int i=0;i<bin.Length();i++){
    cout << hex << (unsigned short) bin[i] << "\n";
}

先前的代码运行良好,因此,编译的二进制类没有任何问题,那又是什么问题?


这是作为日志文件的编译器输出: compiler_log
请注意,这里有3个项目(2个静态库,1个可执行测试应用程序)


我找到了解决方案。我之前说过,必须将2个静态库与可执行应用程序链接,而我所做的只是更改了库的顺序(第一个是库)。最近,“核心”库是第一个,“文件”库是第二个,但是我将顺序(链接)改为相反的顺序,然后发现问题已解决ScreenShot库的顺序真的有问题吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-