implicit declaration of function 'getline' even thought it is - c

I'm currently having some problems with getline() in c.
I know it's not a standard C function, however I am using the proper resources according to what I've seen online.
I have both:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
However I am still having problems compiling, it gives me this output:
main.c: In function 'read-file':
main.c:46:17: warning: implicit declaration of function 'getline' [-Wimplicit-function-declaration]
C:\Users\Miguel\AppData\Local\Temp\cc6aYESe.o:main.c:(.text+0x85): undefined reference to `getline'
collect2.exe: error: ld returned 1 exit status
I can't seem to figure out what's wrong, I've seen several problems like this, however no solution seems to apply.

try with the following preprocessor sentence:
#define _POSIX_C_SOURCE 200809L
"It allows you to use functions that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard. Using the macros described in feature_test_macros allows you to control the definitions exposed by the system header files."
ref: What does #define _POSIX_SOURCE mean?

Related

clang linking error: undefined reference to 'qsort'

Despite the fact that i included '#include ' to my code, when i use built-in qsort function, clang gives me the error:
schedule.o: In function `chooseTicket':
schedule.c:(.text+0x16d): undefined reference to `qsort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
start of the file (schedule.c) is like that:
#include "sched.h"
#include "schedproc.h"
#include <assert.h>
#include <minix/com.h>
#include <machine/archtypes.h>
#include <stdlib.h>
#include <lib.h>
#include <string.h>
#include <time.h>
and here is the function in which i used qsort built-in function
int chooseTicket(int* ticketList,int length,int totalTicket){
int randomValue;
int temp=0,prevTemp=0,selectedTicket=0,selectedIndex = 0;
time_t t;
struct schedproc *rmp;
int* sortedTicketList = malloc(length*sizeof(int));
memcpy(sortedTicketList,ticketList,length);
srandom((unsigned)time(&t));
randomValue = (random() % totalTicket);
qsort(sortedTicketList,length,sizeof(int),cmpFunc);//this line
note: Same errors also occured for 'rand()' and 'srand()' function and instead i have used 'random()' and 'srandom()', then the problem was solved. I don't understand despite the fact that 'rand()' and 'srand()' is generally accepted functions and header file contains these functions, why clang gives me linking errors while i am using 'rand()' and 'srand().
First, qsort is not a built-in, but part of the C standard library (formally, for hosted environments.)
Second, you need to learn that #include only allows access to the declarations of the functions in any given library. You need to link with the library, for your program to actually perform the call to the functionnality. Since you are getting a linker error here, no #include are going to help.
I guess you are writing a MINIX service, hence linking with libminc rather than with the full standard library ("libc"); in other words, this is a freestanding environment. And it happens qsort() is not in the restricted set of C functions included in libminc.
Either link with qsort.(c|o) specifically; or expand your own local version of libminc to include qsort(); or eat the whole cake and link with full libc, perhaps by adding DPADD+= ${LIBC}; LDADD+= -lc to the Makefile (I never tried to do that but it was supposed to work at some point, according to the code; it is not usual practice, so expect problems down the road.)

Compiler warnings for execvpe function

I have a program written in c which uses the execvpe(3) function, and I've got a line set to include the requisite header file:
#include <unistd.h>
I compile this file with the following command...
gcc foo.c -o foo
...only to get the following warning:
warning: implicit declaration of function ‘execvpe’ [-Wimplicit-function-declaration]
I've encountered similar behavior with files that reference the pthread_create(3) function. The difference is obviously that whereas the pthread_create(3) man page clearly states that one should "Compile and link with -pthread", the man page for the exec(3) family of functions does not have any such instructions. Furthermore, I cannot find any reference in the manual or online to an analogous compiler flag for the exec(3) family.
I'd appreciate any information you have on this matter. If there is some flag I should be using at compile time, or if I am looking in entirely the wrong place for a solution, please let me know.
The man page here states that it is necessary to define the _GNU_SOURCE feature test macro to enable the function declaration:
#define _GNU_SOURCE
#include <unistd.h>
Interestingly however the link to unistd.h on the same man page takes you to an implementation that does not declare execvpe at all. You could check your system's unistd.h file to check that it is declared and is dependent on _GNU_SOURCE - that is to solve this an similar problems in the future - check the header content to see if it is even there and what macros it may depend on.
If it is not in the header file, then it is most probably also not in the library, but you could check as follows:
#include <unistd.h>
extern int execvpe(const char *file, char *const argv[], char *const envp[]);
which will satisfy the compiler, but if you then get a linker error, then the function is simply not included in the library in any case.

vfork() implicit declaration

I working in C with vfork(). My program working fine, but I have warning about implicit declaration.
My code:
if(vfork()==0){
...
}
My warning is:
implicit declaration of function 'vfork' [-Wimplicit-function-declaration] if(vfork()==0){^
I include those:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
If I use fork() and not vfork() warning gone. Soo problem is only vfork() in my program.
I don't know what this mean or how I fix that.
You need to include these 2 headers:
#include <sys/types.h>
#include <unistd.h>
Also, add this line in the beginning of the program:
#define _BSD_SOURCE
If you already have the required include files, then, depending on your system version, you may need to define some feature test macros. Please see documentation for your system (man vfork on unix-like systems)
Adding onto Igor's answer, make sure you aren't compiling for C99. clang gives me the error "implicit declaration of function 'vfork' is invalid in C99", and removing -std=c99 from the arguments fixed the issue.

clone() function implicit declaration

I am using the function clone() to create threads. The problem is that I am having this error during compilation:
implicit declaration of function ‘clone’ [-Wimplicit-function-declaration]
I included <linux/sched.h>. What can be the problem?
Add the following lines at the top of you source file
#define _GNU_SOURCE
#include <linux/sched.h> /* or #include <sched.h> */
_GNU_SOURCE is a Feature test macro.
Feature test macros allow the programmer to control the definitions that are exposed by system header files when a program is compiled. In order to be effective, a feature test macro must be defined before including any header files. This can be done either in the compilation command (cc -DMACRO=value) or by #define-ing the macro within the source code before #include-ing any headers.
Defining _GNU_SOURCE internally defines _USE_GNU.
sched.h included <bits/sched.h>
In <bits/sched.h>, the function clone() is declared ONLY if _USE_GNU is defined.

using randomize() without time.h in C

I was able to run code that uses the randomize function without including the time.h library. is it automatically included with some other libraries i might have already included in my code? Below is a list of the libraries I included:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
#include <io.h>
This is very very specific to the version and implementation of your library. The standard doesn't force any header to include time.h 1 so you cannot rely on that.
In your case, it could be that one of dos.h, io.h, conio.h for example has included time.h (or any other of the headers there for all it's worth).
1 At least not the ones there and not likely in your seemingly ancient library. C11 says threads.h should include time.h
What does <compiler with high warning level> yourcode.c say? My guess would be:
either one of the non-standard DOS-specific headers (conio.h, dos.h, io.h, ...) includes it,
or there's no declaration at all, i. e. it's not included, in which case your compiler silently and implicitly assumes a function signature (specifically, it assumes a return value of int and whatever type of argument you call it with for the first time).
Note that the latter case is wrong, and you should pay attention not to do it (since it may lead your program invoking undefined behavior). Always compile with all warnings enabled so you can track down such an error.
When C compiler can't find a prototype to a function it assumes it is a function that returns int. It also prints a warning function if you didn't change the default settings.
So. In your case perhaps time.h was included, but be aware that it can cause a lot of problems if it wasn't.

Resources