Question

I only use #include <stdio.h> in

#include <stdio.h>

void strcat(char* s, char* t);

int main()
{
    char str1[12] = "hello";
    char str2[] = ",world";
    strcat(str1, str2);
    printf("%s\n", str1);
    return 0;
}

void strcat(char* s, char* t)
{
    int i = 0, j = 0;
    while(*s != '\0')
        i++;

    while((*(s + i++) = *(t + j++)) != '\0');

}

But when I build it, the console outputs:

--------------------Configuration: test16 - Win32 Debug--------------------
Linking...
LIBCD.lib(strcat.obj) : error LNK2005: _strcat already defined in test16src.obj

Why? Does strcat exist in stdio.h?

PS: Can you explain the link error by describing the progress when it is linked with other files?

Was it helpful?

Solution

strcat is a C library function. See man strcat or POSIX (or here fo windows) or in string.h (POSIX).

So the symbol strcat is used in the standard C libray (LIBCD here) which is linked against the program automagically during the link phase, where the error you show occurs.

The error informs you that the symbol strcat as defined by the object file test16src.o as created by compiling your sources already exists in the standard library (LIBCD) linked to test16src.o to create the final executable.

The get around this symbol name clash use a different name for your implementation, like for example my_strcat().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top