Al2O3
Al2O3

Reputation: 3203

the use of '"#include" in c and this link error?

why can't I use #include "getline.c" or strindex.c in vc 6.0?

                              -----test30src.c------

#include <stdio.h>
#include "getline.c"
#include "strindex.c"

#define MAXLINE 1000

char pattern[] = "ould";

int main()
{
    char line[MAXLINE];
    int found = 0;
    while(getline(line, MAXLINE) > 0)
        if(strindex(line, pattern) >= 0){
            printf("%s", line);
            found++;
        }

    return found;
}

                                ------getline.c------
#include <stdio.h>

int getline(char s[], int lim)
{
    int c, i;
    i = 0;

    while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
        s[i++] = c;
    if(c=='\n')
        s[i++] = c;
    s[i] = '\0';
    return i;
}
                               -----strindex.c-----
int strindex(char s[], char t[])
{
    int i, j, k;

    for(i = 0; s[i] != '\0'; i++){
        for(j = i, k = 0; s[j] == t[k]; j++, k++)
            ;
        if(k > 0 && t[k] == '\0')
            return j;
    }

    return -1;

}

Error:

--------------------Configuration: test30 - Win32 Debug--------------------
Linking...
getline.obj : error LNK2005: _getline already defined in test30src.obj
strindex.obj : error LNK2005: _strindex already defined in test30src.obj
Debug/test30.exe : fatal error LNK1169: one or more multiply defined symbols found

Upvotes: 2

Views: 258

Answers (4)

Gaurav
Gaurav

Reputation: 5

you can include c files like this but make sure to put those files in the same directory or else give full path.

#include "complete_path/getline.c"

this will work

Upvotes: -1

MikeySec
MikeySec

Reputation: 81

Most IDEs will compile all .c files by default. Here, it will compile once getline.c and strindex.c by themselves, and a second time when it compiles testsrc30.c, which include the two other files. Remember that the #include directive simply copy the contents of the included file.

At the time of linking, some symbols are found twice, and the linker can't handle the ambiguity-

The standard way of using #include's is with header (.h) files containing the declarations for your functions.

Example

//getline.h

#ifndef GETLINE_H  //Header guard - avoid multiple inclusion
#define GETLINE_H
#include <stdio.h>

int getline(char s[], int lim); //function declaration

#endif // GETLINE_H

.

//getline.c

#include "getline.h"

int getline(char s[], int lim)  //declaration
{
    // Implement whatever your function does
}

.

// test30src.c
#include "getline.h"

int main(void)
{
    // Put your code here
}

In some cases, it might be tolerable to include .c files. But in this case, you should make sure that these .c files are not compiled by themselves and linked. See this related question : Including one C source file in another?

Upvotes: 1

user694733
user694733

Reputation: 16033

You must not include files which contain definitions, or you will end up with multiple instances of the same functions. Create header file which contains only declarations.

getline.c (definitions)

#include "getline.h"
int getline(char s[], int lim) { ... }

getline.h (only declarations)

#ifndef GETLINE_H    
#define GETLINE_H
int getline(char s[], int lim);
#endif

main.c (include only header)

#include "getline.h"

Upvotes: 1

Tam&#225;s Zahola
Tam&#225;s Zahola

Reputation: 9321

You should include string.h instead of getline.c

Upvotes: 1

Related Questions