Thread: Compiling help

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Compiling help


    Here's the code I am trying to compile:
    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    main()
    {
    	int len;
    	int max;
    	char line[MAXLINE];
    	char longest[MAXLINE];
    	
    	max = 0;
    	while ((len = getline(line, MAXLINE)) > 0)
    		if (len > max) {
    			max = len;
    			copy(longest, line);
    			}
    		if (max > 0)
    		printf("%s", longest);
    	return 0;
    }
    
    int getline(char s[], int lim)
    {
    	int c, i;
    	
    	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c !='\n'; ++i)
    		s[i] = c;
    	if (c == '\n') {
    		s[i] = c;
    		++i;
    	}
    	s[i] = '\0';
    	return i;
    }
    
    void copy(char to[], char from[])
    {
    	int i;
    	i = 0;
    	while ((to[i] = from[i]) != '\0')
    		++i;
    }
    When I use DevC++, compilation is successful. But when I use gcc from linux, it shows some errors:
    Code:
    root@kali:~# gcc chararray.c -o chararray
    chararray.c:4:5: error: conflicting types for ‘getline’
    In file included from chararray.c:1:0:
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
    chararray.c:25:5: error: conflicting types for ‘getline’
    In file included from chararray.c:1:0:
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
    I don't understand what's wrong. I would be grateful if someone looked into the problem! :)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    getline is the name of a POSIX function.
    Originally Posted by manual page
    NAME
    getline, getdelim - delimited string input

    SYNOPSIS
    #include <stdio.h>

    ssize_t getline(char **lineptr, size_t *n, FILE *stream);

    ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);

    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

    getline(), getdelim():
    Since glibc 2.10:
    _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
    Before glibc 2.10:
    _GNU_SOURCE
    So either rename your function to be something else, or compile with the -ansi flag, like so.
    Code:
    $ gcc bar.c
    bar.c:4:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
    bar.c:25:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
    $ gcc -ansi bar.c
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Smile


    Originally Posted by salem
    getline is the name of a POSIX function.


    So either rename your function to be something else, or compile with the -ansi flag, like so.
    Code:
    $ gcc bar.c
    bar.c:4:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
    bar.c:25:5: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
    $ gcc -ansi bar.c
    $
    ahh I see! Using the -ansi flag worked! Thanks a lot! :)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Change the name of your function to something different, like get_line or getLine.

    In stdio, there's a library function called getline which is declared differently. So after having parsed that prototype in stdio.h, suddenly you gave the compiler a new and different prototype for the "same function". So to fix that, give your function a different name.

    A man page for getline is at http://linux.die.net/man/3/getline. It was added to the standard library with libc 4.6.27, the date of which I'm not sure, but I think that it must postdate the version that Dev-C++'s MinGW gcc compiler was based on, which is why Dev-C++ doesn't have that library function.

    By the way, getline is a safer and easier alternative to gets, fgets, and scanf("%100s"), in that it will create an input buffer that is large enough to contain the entire input string. Using it, you not only do not need to worry about limiting the number of characters being input, but you also don't have to handle the reading in of the rest of an input line that was too big. But, again, it's not available in all C compilers.

    PS
    The clue that it was already declared inside the stdio.h header file was given to you here:
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here

    For when something similar happens in the future.
    Last edited by dwise1_aol; June 18th, 2013 at 02:47 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by dwise1_aol
    Change the name of your function to something different, like get_line or getLine.

    In stdio, there's a library function called getline which is declared differently. So after having parsed that prototype in stdio.h, suddenly you gave the compiler a new and different prototype for the "same function". So to fix that, give your function a different name.

    A man page for getline is at http://linux.die.net/man/3/getline. It was added to the standard library with libc 4.6.27, the date of which I'm not sure, but I think that it must postdate the version that Dev-C++'s MinGW gcc compiler was based on, which is why Dev-C++ doesn't have that library function.

    By the way, getline is a safer and easier alternative to gets, fgets, and scanf("%100s"), in that it will create an input buffer that is large enough to contain the entire input string. Using it, you not only do not need to worry about limiting the number of characters being input, but you also don't have to handle the reading in of the rest of an input line that was too big. But, again, it's not available in all C compilers.

    PS
    The clue that it was already declared inside the stdio.h header file was given to you here:
    /usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here

    For when something similar happens in the future.
    Oh okay. I overlooked that error description. Thanks for your analysis. :)

IMN logo majestic logo threadwatch logo seochat tools logo