Fish
Fish

Reputation: 165

Initialize array in gcc, undefined reference to `memcpy'

I'm coding C in Nachos3.4, Centos 6.0, compile by gcc 2.95.3,

the command line I use is gmake all

when I compile this, everything is fine

int main()
{
    char* fname[] = {"c(0)", "c(1)", "c(2)", "c(3)", "c(4)", "c(5)", "c(6)", "c(7)"};
    return 0;
}

but when I do this, it said undefined reference to 'memcpy'

int main()
{
    char* fname[] = {"c(0)", "c(1)", "c(2)", "c(3)", "c(4)", "c(5)", "c(6)", "c(7)", "c(8)"};
    return 0;
}

where is the problem and how can i fix that ?

Upvotes: 4

Views: 3510

Answers (1)

user3710044
user3710044

Reputation: 2334

Your initialisation of the automatic fname array involves the compiler constructing a copy of a large amount of data from a hidden static array to your array on the stack. GCC has several techniques it can use for this, one of it's favourites is to call the C library memcpy routine as this should be nice and quick whatever happens.

In your case you don't seem to have a C library so this is a problem.

You can tell GCC to always use the x86 instructions rather than calling the library like this:

gcc -mstringop-strategy=rep_byte -c -O file.c

or

gcc -mstringop-strategy=loop -c -O file.c

However, I was under the impression that GCC didn't start doing this till somewhere in the mid version 3.x.

Perhaps you're using a 'MIPS' processor, teachers like that processor, in which the required option would be -mno-memcpy.

Upvotes: 2

Related Questions