自定义静态库链接错误与MinGW和QT Creator

时间:2012-09-09 06:19:49

标签: c++ qt visual-c++ mingw static-libraries

我在MSVC 2008中创建了一个静态库Win32(它实际上并不包含Win32代码)库,我目前正在尝试在QT Creator中链接到它。但每当我编译时,我都会收到错误:

C:\Users\Snowball\Documents\QT Creator\Libraries\FontSystem\main.cpp:-1: error: undefined reference to `NGUI::CFont::CFont()'

该库是一个字体系统,使用FreeImage加载PNG,然后将其“剪切”成单个符号,然后将图像数据传递给gluBuild2DMipMaps(),然后创建一个OpenGL纹理,以便稍后使用绘制字符串时我定义了所有类方法,整个类都是名为NGUI的命名空间的一部分。这样,如果由于某种原因使用了两个字体系统,则字体系统不会与另一个混淆。要链接此库,我只需将以下代码添加到我的.pro文件中:LIBS += FontSystem.lib

现在应用程序中唯一的文件就是:

#include "fontsystem.h"
using namespace NGUI;

int main(int argc, char *argv[])
{
    cout<< "Starting the FontSystem..."<< endl;

    CFont *cFont = new CFont();
    cout<< "FontSystem Started!"<< endl;

    system("sleep 1");
    return 0;
}

文件fontsystem.h如下所示:

#ifndef FONTSYSTEM_H
#define FONTSYSTEM_H

// Include the Basic C++ headers.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <assert.h>
#include <limits>
using namespace std;

// Include the OpenGL and Image Headers.
#include <GL/gl.h>
#include <GL/glu.h>
#include "utilities.h"

// Macros.
#define DIM(x) (sizeof(x)/sizeof(*(x)))

/*
    The Font System works by loading all the font images (max image size 32px^2) into memory and storing
  the OpenGL texture ID's into an array that can be access at all times. The DrawString() functions will
  search through the string for the specified character requested to draw and then it will draw a quad
  and paint the texture on it. Since all the images are pre-loaded, no loading of the images at load time
  is necessary, this is memory consuming but efficiant for the CPU. ALL functions WILL return a character
  string specifing errors or success. A function will work as long as it can and when an error happens,
  unless the error is fatal, the functions will NOT rollback changes! This ensures that during testing, a
  very certain bug can be spotted.
*/

namespace NGUI // This namespace is used to ensure no confusion happens. This font system paints 2D fonts for GUIs.
{
    class CFont
    {
    public:
        CFont();
        ~CFont();

        template<typename tChar> char* DrawString(tChar *apString, int aiSize, int aiX, int aiY);
        template<typename tNum> char* DrawString(tNum anNumber, int aiSize, int aiX, int aiY);

    private:
        char* SetupFont(); // This function will load as many images as possible into memory.

        GLuint miTextIDs[36];
        int miDrawIDs[1024];
    };
}

#endif // FONTSYSTEM_H

编辑: 这是fontsystem.h的实现文件

    #include "fontsystem.h"

#include "fontsystem.h"

namespace NGUI
{
    CFont::CFont()
    {
        SetupFont();
    }

    CFont::~CFont() {}

    template<typename tChar>
    char* CFont::DrawString(tChar *apString, int aiSize, int aiX, int aiY)
    {
        // Search the string from most significant character to least significant.
        int iSelectIndex = 0;
        for(size_t i = 0; apString[i] != NULL; ++i)
        {
            iSelectIndex = apString[i] >= '0' && apString[i] <= '9' ? (apString[i] - '0') :
                           apString[i] >= 'A' && apString[i] <= 'Z' ? (apString[i] - 'A' + 10) :
                           apString[i] >= 'a' && apString[i] <= 'z' ? (apString[i] - 'a' + 10) :
                           apString[i] == ' ' ? 36 : // This is a special case, This see's if the current character is a space or not.
                           -1;

            if(iSelectIndex == -1)
            {
                return "The String Is Corrupt! Aborting!";
            }

            // Add the current selected character to the drawing array.
            miDrawIDs[i] = iSelectIndex;
        }

        // Go through and draw each and every character.
        for(size_t i = 0; i < DIM(miDrawIDs); ++i)
        {
            // Paint each qaud with the X,Y coordinates. After each quad has been successfully drawn,
            // Add the size to the X coordinate. NOTE: Each character is square!!!

            if(miDrawIDs[i] != 36)
            {
                glBindTexture(GL_TEXTURE_2D, miDrawIDs[i]);
            }

            // The font color is always white.
            glColor4f(1.0, 1.0, 1.0, 0.0); // The alpha argument in the function call is set to 0 to allow color only where image data is present.

            glBegin(GL_QUADS);
                glTexCoord2i(0, 0);
                glVertex2i(aiX, aiY);

                glTexCoord2i(1, 0);
                glVertex2i(aiX + aiSize, aiY);

                glTexCoord2i(1, 1);
                glVertex2i(aiX + aiSize, aiY + aiSize);

                glTexCoord2i(0, 1);
                glVertex2i(aiX, aiY + aiSize);
            glEnd();

            // Now, increase the X position by the size.
            aiX += aiSize;
        }

        return "Successful Drawing of String!";
    }

    template<typename tNum>
    char* CFont::DrawString(tNum anNumber, int aiSize, int aiX, int aiY)
    {
        // Convert the supplied number to a character string via snprintf().
        char *vTempString = new char[1024];
        snprintf(vTempString, 1024, "%f", anNumber);

        // Next, run DrawString().
        return DrawString<char>(vTempString, aiSize, aiX, aiY);
    }

    char* CFont::SetupFont()
    {
        // First Load The PNG file holding the font.
        FreeImage_Initialise(false);

        FIBITMAP *spBitmap = FreeImage_Load(FIF_PNG, "Font.png", BMP_DEFAULT);

        if(!spBitmap)
        {
            return "Was Unable To Open/Decode Bitmap!";
        }

        // Do an image sanity check.
        if(!FreeImage_HasPixels(spBitmap))
        {
            return "The Image doesn't contain any pixel data! Aborting!";
        }

        // The Image will have the red and blue channel reversed, so we need to correct them.
        SwapRedBlue32(spBitmap);

        // Retrieve all the image data from FreeImage.
        unsigned char *pData = FreeImage_GetBits(spBitmap);
        int iWidth = FreeImage_GetWidth(spBitmap);

        // Cutup the PNG.
        int iFontElementSize = (32*32)*4; // The first two numbers, are the dimensions fo the element, the last number (4) is the number of color channels (Red Green Blue and Alpha)
        bool bDone = false; // This bit is only set when the entire image has been loaded.
        unsigned char *pElemBuff = new unsigned char[iFontElementSize]; // The temporary element buffer.
        int iDataSeek = 4; // Start with an offset of 4 because the first byte of image data starts there.
        int iTexIdx = 0; // This is an offset specifing which texture to create/bind to.

        // Create all 36 OpenGL texures. 0-9 and A-Z and finally space (' ')
        glGenTextures(37, miTextIDs);

        while(!bDone)
        {
            // Now load the an element into the buffer.
            for(int i = 0, iXCount = 0, iYCount = 0;
                i < iFontElementSize; ++i, ++iXCount)
            {
                if(iXCount >= (32*4))
                {
                    iXCount = 0; // Reset the column offset.
                    ++iYCount; // Move down 1 row.
                    iDataSeek += ((iWidth * 4) - (32*4)); // Set the data seek to the next corrosponding piece of image data.
                }

                if(pData[iDataSeek] == NULL)
                {
                    break;
                }

                pElemBuff[i] = pData[iDataSeek];
            }

            // Check to see if we are done loading to prevent memory corruption and leakage.
            if(bDone || iTexIdx >= 37)
            {
                break;
            }

            // Create The OpenGL Texture with the current Element.
            glBindTexture(GL_TEXTURE_2D, miTextIDs[iTexIdx]);
            gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, 32, 32, GL_RGBA, GL_UNSIGNED_BYTE, pElemBuff);

            // Create the correct texture envirnment to the current texture.
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        }

        // Do a little house cleaning!
        delete[] pElemBuff;
        delete pData;
        FreeImage_Unload(spBitmap);
        FreeImage_DeInitialise();
    }
}

请注意:此代码尚未经过测试,但编译正常(根据MSVC 2008)

我忘了将system("sleep 1");更改为system("PAUSE");,而我调用该命令的原因是因为我最初是在linux中构建它。

编辑2:我已更新实施代码以反映该文件。

0 个答案:

没有答案