Hi I’m almost done with a project I have for class. I need to sort the priority of people in an airline based on a few factors. This is my project description:
«An airline company uses the formula shown below to determine the priority of passengers on the
waiting list for overbooked flights.
Priority Number = (A / 1000) + B — C
where
A is the customer’s total mileage in the past year
B is the number of years in his or her frequent flier program
C is a sequence number representing the customer’s arrival position when he or she booked the
flight.
Given a file of overbooked customers as shown in the table below, write a program that reads the
file and determines each customer’s priority number. The program then builds a priority queue
using the priority number and prints a list of waiting customers in priority sequence.»
I feel my code encompasses the main idea, but I am having issues with getline. When I compile the code (shown below) it gives me an error:
«|24|warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]|»
Please help me fix this so it will compile. I’ve tried a lot of things and nothing works.
Here is the code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE * fp; //create file pointer to be read later
char * line = NULL;
int temp,count=0,len=0,mileage[100],read,years[100],sequence[100],priority[100];
char name[100][100],tempst[100];
fp = fopen("customers.txt", "r"); //opening the file
if (fp == NULL)
exit(EXIT_FAILURE);
int i;
while ((read = getline(&line, &len, fp)) != -1) { //start reading file and recording data
i=0;
while(line[i]!=' ' || (line[i+1]>='A' && line[i+1]<='z'))
{
name[count][i]=line[i];
i++;
}
name[count][i++]='';
mileage[count]=0;
while(line[i]!=' ')
{
mileage[count]=mileage[count]*10+(line[i]-'0');
i++;
}
i++;
years[count]=0;
while(line[i]!=' ')
{
years[count]=years[count]*10+(line[i]-'0');
i++;
}
i++;
sequence[count]=0;
while(line[i]!='')
{
sequence[count]=sequence[count]*10+(line[i]-'0');
i++;
}
priority[count]=(mileage[count]/1000)+years[count]-sequence[count];
count++;
}
for( i=0;i<count-1;i++) // calculate priority
{
for(int j=i+1;j<count;j++)
{
if(priority[i]<priority[j])
{
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;
strcpy(tempst,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],tempst);
}
}
}
for( i=0;i<count;i++) //print priority
{
printf("%s %dn",name[i],priority[i]);
}
return 0;
}
-
01-26-2006
#1
Registered User
- Join Date
- Jan 2006
- Posts
- 13
Implicit declaration of function …???
Hi,
I keep getting the warning of:Code:
Implicit declaration of funciton getline()
whenever i use getline() function
What does this warning mean actually ?
Is this a serious warning, or just ignorable.Cheer!!
-
01-26-2006
#2
Registered User
- Join Date
- Aug 2005
- Posts
- 1,267
getline() is c++ not C. There is no standard C getline() function.
-
01-26-2006
#3
Registered Luser
- Join Date
- Jul 2005
- Location
- Sydney, Australia
- Posts
- 869
getline() is also a GNU provided extension available under #include <stdio.h>. It will not appear if you use the -ansi flag, because the -ansi flag excludes all GNU extensions. It’s not part of standard C, as Ancient Dragon said.
-
01-26-2006
#4
Registered User
- Join Date
- Aug 2005
- Posts
- 1,267
replace getline() with fgets() and your program will (maybe) work.
-
01-27-2006
#5
Registered User
- Join Date
- Jun 2005
- Posts
- 6,815
What’s happening is that you have tried to use a function called getline() that hasn’t been declared anywhere (eg in a header file) and the compiler takes your attempt to call it as an implicit declaration.
As other have sort of said, your real problem is probably that you’re calling a function that doesn’t exist and you need to work out what function you really intend to call. If the function is one you actually know you’re trying to call, find the corresponding header file and #include it.
-
01-28-2006
#6
Frequently Quite Prolix
- Join Date
- Apr 2005
- Location
- Canada
- Posts
- 8,057
Code:
$ grep -n getline /usr/include/*.h
If you’re trying to use the C++ getline, then you need <iosteam> etc.
dwk
Seek and ye shall find. quaere et invenies.
«Simplicity does not precede complexity, but follows it.» — Alan Perlis
«Testing can only prove the presence of bugs, not their absence.» — Edsger Dijkstra
«The only real mistake is the one from which we learn nothing.» — John PowellOther boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.netMy website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
-
08-07-2006
#7
Registered User
- Join Date
- Aug 2006
- Posts
- 12
Sorry if I am a topic digging here, but I came across this thread while searching for the answer for exactly this problem and I noticed noone here gave the right answer.
The answer turned out pretty simple:
getline() IS a valid C function which is defined in stdio.h. As some people rightfully suggested it is not a function that is defined in the ANSI standard, but it is a GNU extension. In order to correctly include these extensions you have to add a define:Code:
#define _GNU_SOURCE #include <stdio.h> /* functions like getline() and getdelim() should be defined now */
Ofcourse this only works if you are actually using glibc (e.g. on Linux or cygwin).
Hope this clarifies the issue.
— LT
-
08-07-2006
#8
Registered User
- Join Date
- Jun 2005
- Posts
- 6,815
Originally Posted by Lord Thunder
Sorry if I am a topic digging here, but I came across this thread while searching for the answer for exactly this problem and I noticed noone here gave the right answer.
Depends on your perspective. I consider your post to be incorrect and misleading, as you make some statements early on which are compiler/library specific but you state them as generalities. Yes you correct them later, sort of, but the information you give is not generally true.
Originally Posted by Lord Thunder
The answer turned out pretty simple:
getline() IS a valid C function which is defined in stdio.h.No. The functions that are declared in <stdio.h> are specified in the ISO C standard. They are not required to be defined in stdio.h.
Reliance on stdio.h declaring any function that is not specified in the C standard is invalid, unless you are prepared to constrain yourself to only one compiler and it’s library (and possibly to a particular version of that compiler and library).
Originally Posted by Lord Thunder
As some people rightfully suggested it is not a function that is defined in the ANSI standard, but it is a GNU extension.
Exactly my point. There are a few commonly used compilers that are produced by vendors not associated with GNU. Most of those compilers do not support GNU extensions and are not bunded with glibc.
Originally Posted by Lord Thunder
In order to correctly include these extensions you have to add a define:
Code:
#define _GNU_SOURCE #include <stdio.h> /* functions like getline() and getdelim() should be defined now */
Ofcourse this only works if you are actually using glibc (e.g. on Linux or cygwin).
Sort of. glibc, in practical terms, often relies on using a correct version of gcc (the GNU compiler collection). Several versions of glibc only work with particular versions of gcc. This is the reason that the gnu compiler and glibc are often bundled together, particularly with recent versions. Both the gnu compiler and library are supported on more systems than linux; cygwin is just one of several windows ports.
-
08-07-2006
#9
…
- Join Date
- Jan 2003
- Posts
- 1,534
Originally Posted by Lord Thunder
Sorry if I am a topic digging here, but I came across this thread while searching for the answer for exactly this problem and I noticed noone here gave the right answer.
The answer turned out pretty simple:
getline() IS a valid C function which is defined in stdio.h. As some people rightfully suggested it is not a function that is defined in the ANSI standard, but it is a GNU extension. In order to correctly include these extensions you have to add a define:Code:
#define _GNU_SOURCE #include <stdio.h> /* functions like getline() and getdelim() should be defined now */
Ofcourse this only works if you are actually using glibc (e.g. on Linux or cygwin).
Hope this clarifies the issue.
— LTWhy is it exactly that you are dredging up a 7 month old thread? Was the urge so strong to blabber that you could not resist? Give me a break.
Forum Guidelines. Read before posting:
5. Don’t bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
Any questions?
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Closed
ryandesign opened this issue
Jan 28, 2016
· 5 comments
Comments
It’s probably not worth fixing. Snow Leopard’s support ended two years
ago.
Copy link
Contributor
Author
I’ve taken over as maintainer of pianobar in MacPorts, so I’m coming back to this issue now.
It would be nice if you would reconsider your decision. There are lots of old Macs that cannot be upgraded to Lion or newer, but which would be perfectly adequate as music players. There are probably old versions of other operating systems that also lack getline
. It is probably a simple matter to add a getline
compatibility function; lots of other projects do so.
Here’s a fresh build log:
https://build.macports.org/builders/ports-10.6_x86_64_legacy-builder/builds/8638/steps/install-port/logs/stdio
Copy link
Contributor
Author
Thank you, yes, that does allow pianobar to work on Mac OS X 10.6, and I confirmed it still works with that patch on 10.11 as well.
PromyLOPh
added a commit
that referenced
this issue
Oct 29, 2016
Mac OS X 10.6 compatibility, fixes #572.
thedmd
pushed a commit
to thedmd/pianobar-windows
that referenced
this issue
May 17, 2017
Mac OS X 10.6 compatibility, fixes PromyLOPh#572.
# Conflicts:
# src/settings.c
2 participants
TL;DR: POSIX says getline()
should be on my system, but when I use -std=c89
on GNU/Linux I get «implicit declaration» and when I do the same on OpenBSD 6.5, it works with without any error or warning. Why?
Here is a tiny program using getline()
and ssize_t
:
#include <stdio.h>
int main() {
char *s = NULL;
size_t len = 0;
ssize_t n;
n = getline(&s, &len, stdin);
printf("bytes read: %dn", (int) n);
printf("%s", s);
return 0;
}
(sure, it is flawed, but that is not the point)
Now, if I compile it on my OpenBSD 6.5 with both gcc
(4.2.1 20070719) and clang
(7.0.1) with various standards, it always works.
$ clang -Wall -Wextra -pedantic -std=c89 -o gl gl.c
$ clang -Wall -Wextra -pedantic -std=c99 -o gl gl.c
$ gcc -Wall -Wextra -pedantic -std=c89 -o gl gl.c
$ gcc -Wall -Wextra -pedantic -std=c99 -o gl gl.c
(no errors or warnings)
Now, if I try the same on GNU/Linux (Archlinux).
With GCC 8.3.0, I get this (both c89 and c99):
$ gcc -Wall -Wextra -pedantic -std=c89 -o gl gl.c
gl.c: In function ‘main’:
gl.c:6:9: error: unknown type name ‘ssize_t’; did you mean ‘size_t’?
ssize_t n;
^~~~~~~
size_t
gl.c:7:13: warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]
n = getline(&s, &len, stdin);
and on clang (8.0.0) I get this
$ clang -Wall -Wextra -pedantic -std=c99 -o gl gl.c
gl.c:6:9: error: unknown type name 'ssize_t'; did you mean 'size_t'?
ssize_t n;
^~~~~~~
size_t
/usr/lib/clang/8.0.0/include/stddef.h:62:23: note: 'size_t' declared here
typedef __SIZE_TYPE__ size_t;
^
gl.c:7:13: warning: implicit declaration of function 'getline' is invalid in C99
[-Wimplicit-function-declaration]
n = getline(&s, &len, stdin);
^
1 warning and 1 error generated.
$ gcc -Wall -Wextra -pedantic -std=c89 -o gl gl.c
gl.c: In function ‘main’:
gl.c:6:9: error: unknown type name ‘ssize_t’; did you mean ‘size_t’?
ssize_t n;
^~~~~~~
size_t
gl.c:7:13: warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]
n = getline(&s, &len, stdin);
^~~~~~~
If I use -std=gnu89
, both clang and GCC produce no errors or warnings.
Now, the Linux man pages (5.01, http://man7.org/linux/man-pages/index.html) say about getline()
$ man getline | grep -A5 ^CONFORMING
CONFORMING TO
Both getline() and getdelim() were originally GNU extensions. They
were standardized in POSIX.1-2008.
EXAMPLE
#define _GNU_SOURCE
$
The OpenBSD 6.5 manpage says
$ man getline | grep -A1 ^STANDARDS
STANDARDS
The getdelim() and getline() functions conform to IEEE Std 1003.1-2008 (“POSIX.1”).
And when it comes to ssize_t
, the POSIX.1 [0] also say the sys/types.h
shall define it. If I explicitly include the header, it does indeed resolve the ssize_t
problem.
$ ed gl.c
228
1a
#include <sys/types.h>
.
wq
251
$ gcc -Wall -Wextra -pedantic -std=c89 -o gl gl.c
gl.c: In function ‘main’:
gl.c:8:13: warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]
n = getline(&s, &len, stdin);
^~~~~~~
$
When POSIX says there has to be a function (getline()
) and my system is (or partially is) compliant to POSIX, I would expect the function to be available, regardless of what C standard I use. But that seems to be different on GNU/Linux and OpenBSD. So which system does do it right? Should the range of symbols available be restricted to the C standard used or it should be enriched with what POSIX says should be available?
Also, why is the error saying the declaration is implicit? I included the stdio.h
, just as the (Linux) man page says.
Thank you for all the answers!
[0] IEEE Std 1003.1-2008 (“POSIX.1”): http://pubs.opengroup.org/onlinepubs/9699919799/
13 Years Ago
Hi all,
First time poster here, I’ve referenced posts here on daniweb in the past and it has always been helpful, thanks! But I can’t find the answer to this particular problem here on the forums.
I need to write a basic string parser as part of a coding assignment here at penn state. I have completed the assignment using getc, but bonus points are offered for a solution using getline.
The getline() man page (http://linux.die.net/man/3/getline) requires stdio.h and stdlib.h for getline to compile.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//welcome to main!
//we need our input values!
char inputstr[128];
while(1)
{
if(feof(stdin) != 0)
{ break; }
getline(&inputstr, 128, NULL);
//did that work!?
printf("input string is : %sn", inputstr);
}
getc(stdin);
return 0;
}
This code wont even compile!!
Here is the compiler error and log, from Dev-C++(set for standard C)
LOG:
Compiler: Default compiler
Executing gcc.exe...
gcc.exe "E:pr1proj1.c" -o "E:pr1proj1.exe" -I"E:Dev-Cppinclude" -L"E:Dev-Cpplib"
C:DOCUME~1xxxxxxLOCALS~1Temp/ccwrbaaa.o(.text+0x61):proj1.c: undefined reference to `getline'
collect2: ld returned 1 exit status
Execution terminated
ERROR:
[Linker error] undefined reference to `getline'
ld returned 1 exit status
Unfortunately, my go-to book, C:ARM(C a reference manual) doesnt even have this function!
Thanks for any help or insight you can provide!
-Tav
EDIT:
I realize that getline is not a standard C function, but even when I compile using
-std=c99
I get the same error!
Edited
13 Years Ago
by taverasme because:
n/a
Recommended Answers
Try something like below:
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char inputstr[128]; char *cptr = &inputstr[0]; size_t thesize = 128; while(1) { if(feof(stdin) != 0) { break; } getline(&cptr, &thesize, stdin); printf("input string is : %sn", inputstr); } getc(stdin); return 0; }
Jump to Post
Or you could:
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *cptr = NULL; size_t thesize = 0; while(1) { if(feof(stdin) != 0) { break; } getline(&cptr, &thesize, stdin); printf("input string is : %sn", cptr); free(cptr); cptr = NULL; } getc(stdin); return 0; …
Jump to Post
All 8 Replies
13 Years Ago
Try something like below:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char inputstr[128];
char *cptr = &inputstr[0];
size_t thesize = 128;
while(1)
{
if(feof(stdin) != 0)
{ break; }
getline(&cptr, &thesize, stdin);
printf("input string is : %sn", inputstr);
}
getc(stdin);
return 0;
}
13 Years Ago
Or you could:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *cptr = NULL;
size_t thesize = 0;
while(1)
{
if(feof(stdin) != 0)
{ break; }
getline(&cptr, &thesize, stdin);
printf("input string is : %sn", cptr);
free(cptr);
cptr = NULL;
}
getc(stdin);
return 0;
}
Edited
13 Years Ago
by gerard4143 because:
n/a
13 Years Ago
Try something like below:
With the MinGW version of gcc:
main.c
main.c: In function `main’:
main.c:18: warning: implicit declaration of function `getline’
Linking…
Debug/main.o: In function `main’:
C:/Users/Dave/Projects/scratch/main.c:18: undefined reference to `_getline’
collect2: ld returned 1 exit status
*** Errors occurred during this build ***
I need to write a basic string parser as part of a coding assignment here at penn state. I have completed the assignment using getc, but bonus points are offered for a solution using getline.
Are they expecting you to then be using a *nux platform?
I realize that getline is not a standard C function, but even when I compile using
-std=c99
I get the same error!
Yes. It’s not standard in either C90 or C99. ?
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
13 Years Ago
>>my go-to book, C:ARM(C a reference manual) doesnt even have this function!
because getline() is not a standard C function. Its a standard c++ function, not C. If your compiler implements a C getline() function than ok, but don’t expect to read about it in any text book.
13 Years Ago
Linux man states
CONFORMING TO
Both getline() and getdelim() are GNU extensions. They are available
since libc 4.6.27.
Edited
13 Years Ago
by gerard4143 because:
n/a
11 Years Ago
Hi,i need help please,when i compile cpp file it give me error
main.cpp:158: error: ‘getline’ was not declared in this scope
i am newbie and i dont know what to do…i find some info about problems with getline on mac os x….the program run without problem in ubuntu but on os x i have this error.
Thanks.
11 Years Ago
First question, are you writing a C++ program or a C program? I ask because this is the C section and your error indicates a .cpp file.
Edited
11 Years Ago
by gerard4143 because:
n/a
8 Years Ago
Hello! How are can «free(cptr);» If you dosen’t used to dynamic memorys?
You will helped -** man getline** — in this scope you could see how use that function.
Edited
8 Years Ago
by Nikolas9896
Reply to this topic
Be a part of the DaniWeb community
We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.
#6646
closed
enhancement
(fixed)
Reported by: | Owned by: | axeld | |
---|---|---|---|
Priority: | normal | Milestone: | R1 |
Component: | System/POSIX | Version: | R1/Development |
Keywords: | getline getdelim posix | Cc: | |
Blocked By: | Blocking: | ||
Platform: | All |
VLC media player compilation reportedly fails due to getline() not being
defined.
make[4]: Entering directory `/boot/home/src/vlc/src' CC config/libvlccore_la-dirs_xdg.lo | config/dirs_xdg.c: In function 'config_GetTypeDir': config/dirs_xdg.c:141: | error: implicit declaration of function 'getline'
Haiku have not getline().
getline() and getdelim() are specified by POSIX here: http://www.opengroup.org/onlinepubs/9699919799/functions/getline.html
How-To-Repeat:
C-compile the following test case:
#include <sys/types.h> #include <stdio.h> #include <stdlib.h> int main(void) { char *line = NULL; size_t linesize = 0; ssize_t linelen; while ((linelen = getline(&line, &linesize, stdin)) !=-1) fwrite(line, 1, linelen, stdout); free(line); if (ferror(stdin)) { perror("Standard input"); return 1; } return 0; }
Change History
(3)
Component: | — General → System/POSIX |
---|---|
Keywords: | getline getdelim posix added |
Type: | bug → enhancement |
Version: | R1/alpha2 → R1/Development |
Owner: | changed from nobody to axeld |
---|---|
Status: | new → in-progress |
Resolution: | → fixed |
---|---|
Status: | in-progress → closed |
Note:
See TracTickets
for help on using tickets.