首页 > 解决方案 > "undefined reference to" just when using MinGW/GCC compiler, an ambiguous problem?

问题描述

I have an ambiguous problem only when using MinGW/GCC compiler, on Visual Studio 2019 works well.

Firstly, The error messages:

Secondly, The code:

(The part of code that error messages point to (related to "File" class))

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;
}

(The Binary class)

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];
}

When using:

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

That problem occurs just when using the constructor without using the method the cause of the problem. Also, what makes me crazy is when using the binary class directly the problem does not happen, for example:

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";
}

The previous code works well, so, the binary class compiled without any problem, then what is the problem?


This is the compiler output as a log file: compiler_log
Note that there are 3 projects (2 static libraries, 1 executable test application)


I found the solution. I said previously there are 2 static libraries must be linked with the executable application, what I did is just changed the order of libraries (which one is the first). recently, the "core" library was the first one and the "files" library was the second but I have changed the order (Linking) to the opposite and then I found the problem solved ScreenShot. Is the order of libraries really a problem?

标签: c++compiler-errors

解决方案


推荐阅读