Irritating Error...

Help and discussing programming in C and C++.

Moderator:Moderators

Post Reply
cheesecake
Posts:7
Joined:Sun May 10, 2009 7:57 pm
Irritating Error...

Post by cheesecake » Sun May 10, 2009 8:10 pm

I created a function that draws a title box out of characters and tested it and it worked fine. Then, I put it into the latest version of the OSDev series demo, but every time I try to compile, I get an error from the linker! However, the code works fine if I just stick it in run().

It only generates an error if it is a function...
Here is the code:

Code: Select all

void Title(char* ch)
{
////////////////////////////////////////////////////////////////////////////////
////These are for thick, thin, and medium blocks////////////////////////////////
    int ithin   = 176; char thin   = ithin;
    int imed    = 177; char med    = imed;
    int ithick  = 178; char thick  = ithick;
     // I only knew what the block characters were as integers
     // so I just converted them :)
    int i       = 0;//for all the loops
    int offset  = 0;//to centre the string
    char string[81];//umm... the string...
////////////////////////////////////////////////////////////////////////////////
//Set up the string/////////////////////////////////////////////////////////////
    string[0]=thin; string[1]=med; string[2]=thick;
    for (int t=3; t < 77; t++)
        string[t]=' ';
    string[77]=thick; string[78]=med; string[79]=thin; string[80]='\0';
////////////////////////////////////////////////////////////////////////////////
    for (i=0; i<80; i++)
        DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
    DebugPutc(thin);
    for (i=0; i<78; i++)
        DebugPutc(med);
    DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
    DebugPutc(thin); DebugPutc(med);
    for (i=0; i<76; i++)
        DebugPutc(thick);
    DebugPutc(med); DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
    offset = (74 - strlen(ch)) / 2;
    DebugPutc(thin); DebugPutc(med); DebugPutc(thick);
    for(i=0; i<74; i++)
        DebugPutc(' '); 
    DebugPutc(thick); DebugPutc(med); DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
 !!!THIS IS THE PART CAUSING THE PROBLEM!!!
    for(int n=3; n<strlen(ch)+3; n++)
        string[offset+n] = ch[n-3];
    for(i=0; i<80; i++)
		DebugPutc(string[i]);
////////////////////////////////////////////////////////////////////////////////
    DebugPutc(thin); DebugPutc(med); DebugPutc(thick);
    for(i=0; i<74; i++)
        DebugPutc(' '); 
    DebugPutc(thick); DebugPutc(med); DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
    DebugPutc(thin); DebugPutc(med);
    for (i=0; i<76; i++)
        DebugPutc(thick);
    DebugPutc(med); DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
    DebugPutc(thin);
    for (i=0; i<78; i++)
        DebugPutc(med);
    DebugPutc(thin);
////////////////////////////////////////////////////////////////////////////////
    for (i=0; i<80; i++)
        DebugPutc(thin);
    return; 
}

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Irritating Error...

Post by Andyhhp » Sun May 10, 2009 8:23 pm

can you paste the linker error please - its much more informative than code on its own

~Andrew
Image

cheesecake
Posts:7
Joined:Sun May 10, 2009 7:57 pm

Re: Irritating Error...

Post by cheesecake » Sun May 10, 2009 8:31 pm

error LNK2019: unresolved external symbol _memset referenced in function "void __cdecl Title(char *)" (?Title@@YAXPAD@Z)
A:\KRNL32.EXE : fatal error LNK1120: 1 unresolved externals

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Irritating Error...

Post by Andyhhp » Sun May 10, 2009 8:44 pm

Ahh that makes more sence

What that is doing is the compiler is trying to be cleaver and optimise your code. To do this, its internally calling memset, which you have removed because you are not using the standard C runtime libraries.

What you have to do is turn optimisations off in that area like this:

Code: Select all

#pragma optimize( "", off )
//Some code, in this case your function
#pragma optimize( "", on ) 
That will stop the compiler embedding calls to non-existent functions


I do have a few questions about your code though:

Code: Select all

////These are for thick, thin, and medium blocks////////////////////////////////
    int ithin   = 176; char thin   = ithin;
    int imed    = 177; char med    = imed;
    int ithick  = 178; char thick  = ithick;
What are these doing here? You never use the i_ variables again.

Code: Select all

char string[81];//umm... the string...
Why is this 81 characters long and not 80?

Finally, try and explain whats happens if I call this title funtion with a string of lenght 75 characters

~Andrew
Image

cheesecake
Posts:7
Joined:Sun May 10, 2009 7:57 pm

Re: Irritating Error...

Post by cheesecake » Sun May 10, 2009 8:59 pm

The _i variables were there because I know what the block (filled square) characters are as an integer (176, 177, 178) but I don't know how to initialize a char to a value that will result in the block being displayed. So, basically, the _i's are just there to be converted into chars...

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Irritating Error...

Post by Andyhhp » Sun May 10, 2009 9:05 pm

ints, longs, shorts and chars are all integers

the different names just reflect how big the integer is:

Typically they are:
char - 1 byte
short - 2 bytes
int - 4 bytes
long - 4 bytes

therefore, you can simply do

Code: Select all

////These are for thick, thin, and medium blocks////////////////////////////////
    char thin   = 176;
    char med   = 177;
    char thick  = 178;
That will work in exacly the same way, except you have saved 12 bytes of space on your stack!

~Andrew
Image

cheesecake
Posts:7
Joined:Sun May 10, 2009 7:57 pm

Re: Irritating Error...

Post by cheesecake » Sun May 10, 2009 9:11 pm

Ya, just figured that out. It didn't work the first time I did that because I mistakenly put thin = '176'; med = '177'; thick = '178'; which of course just printed a bunch of 6's, 7's, and 8's :P. Just a bit of a brain fart.

As for your question about strings over 75 chars, it causes Bochs to triple fault... I'll have to fix that...

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Irritating Error...

Post by Andyhhp » Sun May 10, 2009 9:20 pm

fair enough
Image

cheesecake
Posts:7
Joined:Sun May 10, 2009 7:57 pm

Re: Irritating Error...

Post by cheesecake » Sun May 10, 2009 9:23 pm

Wow good thing I posted that here... My computer just froze and the source file got corrupted :x . I'd hate to write that whole thing out again... It definitely isn't easy on the eyes.

cheesecake
Posts:7
Joined:Sun May 10, 2009 7:57 pm

Re: Irritating Error...

Post by cheesecake » Sun May 10, 2009 9:48 pm

Image

The working product! Thanks for the help! :D

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Irritating Error...

Post by Andyhhp » Sun May 10, 2009 10:43 pm

Hehe - No problem

May I plug the importance of keeping backups?

~Andrew
Image

Hoozim
Posts:34
Joined:Sun Nov 21, 2010 6:40 pm

Re: Irritating Error...

Post by Hoozim » Wed Dec 15, 2010 11:31 pm

You need to back up your code after every coding session. I have my OS backed up in several different locations so it is safe.
The routine looks nice, good job. It would be great for a start-up screen. For a great effect, try having it put out block letters for a title.

Post Reply