Following this question, I'm trying to use sysconf to get the number of processors on a Linux machine:
#include <unistd.h>
int main()
{
...
int CPUs = sysconf(_SC_NPROCESSORS_ONLN);
...
}
However, the compiler gives me this error:
error: implicit declaration of function 'sysconf'
Am I doing something wrong? I tried to also add #include <sys/sysinfo.h> but nothing changed.
Related
My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
You are using a function for which the compiler has not seen a declaration ("prototype") yet.
For example:
int main()
{
fun(2, "21"); /* The compiler has not seen the declaration. */
return 0;
}
int fun(int x, char *p)
{
/* ... */
}
You need to declare your function before main, like this, either directly or in a header:
int fun(int x, char *p);
The right way is to declare function prototype in header.
Example
main.h
#ifndef MAIN_H
#define MAIN_H
int some_main(const char *name);
#endif
main.c
#include "main.h"
int main()
{
some_main("Hello, World\n");
}
int some_main(const char *name)
{
printf("%s", name);
}
Alternative with one file (main.c)
static int some_main(const char *name);
int some_main(const char *name)
{
// do something
}
When you do your #includes in main.c, put the #include reference to the file that contains the referenced function at the top of the include list.
e.g. Say this is main.c and your referenced function is in "SSD1306_LCD.h"
#include "SSD1306_LCD.h"
#include "system.h" #include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
The above will not generate the "implicit declaration of function" error, but below will-
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
#include "SSD1306_LCD.h"
Exactly the same #include list, just different order.
Well, it did for me.
You need to declare the desired function before your main function:
#include <stdio.h>
int yourfunc(void);
int main(void) {
yourfunc();
}
When you get the error: implicit declaration of function it should also list the offending function. Often this error happens because of a forgotten or missing header file, so at the shell prompt you can type man 2 functionname and look at the SYNOPSIS section at the top, as this section will list any header files that need to be included. Or try http://linux.die.net/man/ This is the online man pages they are hyperlinked and easy to search.
Functions are often defined in the header files, including any required header files is often the answer. Like cnicutar said,
You are using a function for which the compiler has not seen a
declaration ("prototype") yet.
If you have the correct headers defined & are using a non GlibC library (such as Musl C) gcc will also throw error: implicit declaration of function when GNU extensions such as malloc_trim are encountered.
The solution is to wrap the extension & the header:
#if defined (__GLIBC__)
malloc_trim(0);
#endif
This error occurs because you are trying to use a function that the compiler does not understand. If the function you are trying to use is predefined in C language, just include a header file associated with the implicit function.
If it's not a predefined function then it's always a good practice to declare the function before the main function.
Don't forget, if any functions are called in your function, their prototypes must be situated above your function in the code. Otherwise, the compiler might not find them before it attempts to compile your function. This will generate the error in question.
The GNU C compiler is telling you that it can find that particular function name in the program scope. Try defining it as a private prototype function in your header file, and then import it into your main file.
I think the question is not 100% answered. I was searching for issue with missing typeof(), which is compile time directive.
Following links will shine light on the situation:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
as of conculsion try to use __typeof__() instead. Also gcc ... -Dtypeof=__typeof__ ... can help.
I am getting an error in my compiler:
Warning: implicit declaration of function 'system'
I added:
system("cls");
To be able to clear the screen, and now I get the error. I am using this code to test:
#include <stdio.h>
int nothing; //random name
int main()
{
printf("this is a msg");
scanf("%d",¬hing);
system("cls");
printf("hello");
getchar();
return 0;
}
This is just a test code, so it's very sloppy. I am new to coding so any help would be appreciated.
For C++: #include <cstdlib>, for C: #include <stdlib.h>.
Or, you can do as follows:
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
if (system("CLS")) system("clear");
You can also see a full article w.r.t Clear the screen.
This warning is reported when a function is called before its declaration. In your case, you haven't included the library stdlib.h at the start of your code. So the compiler sees the call to function before its prototype.
My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
You are using a function for which the compiler has not seen a declaration ("prototype") yet.
For example:
int main()
{
fun(2, "21"); /* The compiler has not seen the declaration. */
return 0;
}
int fun(int x, char *p)
{
/* ... */
}
You need to declare your function before main, like this, either directly or in a header:
int fun(int x, char *p);
The right way is to declare function prototype in header.
Example
main.h
#ifndef MAIN_H
#define MAIN_H
int some_main(const char *name);
#endif
main.c
#include "main.h"
int main()
{
some_main("Hello, World\n");
}
int some_main(const char *name)
{
printf("%s", name);
}
Alternative with one file (main.c)
static int some_main(const char *name);
int some_main(const char *name)
{
// do something
}
When you do your #includes in main.c, put the #include reference to the file that contains the referenced function at the top of the include list.
e.g. Say this is main.c and your referenced function is in "SSD1306_LCD.h"
#include "SSD1306_LCD.h"
#include "system.h" #include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
The above will not generate the "implicit declaration of function" error, but below will-
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
#include "SSD1306_LCD.h"
Exactly the same #include list, just different order.
Well, it did for me.
You need to declare the desired function before your main function:
#include <stdio.h>
int yourfunc(void);
int main(void) {
yourfunc();
}
When you get the error: implicit declaration of function it should also list the offending function. Often this error happens because of a forgotten or missing header file, so at the shell prompt you can type man 2 functionname and look at the SYNOPSIS section at the top, as this section will list any header files that need to be included. Or try http://linux.die.net/man/ This is the online man pages they are hyperlinked and easy to search.
Functions are often defined in the header files, including any required header files is often the answer. Like cnicutar said,
You are using a function for which the compiler has not seen a
declaration ("prototype") yet.
If you have the correct headers defined & are using a non GlibC library (such as Musl C) gcc will also throw error: implicit declaration of function when GNU extensions such as malloc_trim are encountered.
The solution is to wrap the extension & the header:
#if defined (__GLIBC__)
malloc_trim(0);
#endif
This error occurs because you are trying to use a function that the compiler does not understand. If the function you are trying to use is predefined in C language, just include a header file associated with the implicit function.
If it's not a predefined function then it's always a good practice to declare the function before the main function.
Don't forget, if any functions are called in your function, their prototypes must be situated above your function in the code. Otherwise, the compiler might not find them before it attempts to compile your function. This will generate the error in question.
The GNU C compiler is telling you that it can find that particular function name in the program scope. Try defining it as a private prototype function in your header file, and then import it into your main file.
I think the question is not 100% answered. I was searching for issue with missing typeof(), which is compile time directive.
Following links will shine light on the situation:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
as of conculsion try to use __typeof__() instead. Also gcc ... -Dtypeof=__typeof__ ... can help.
I've been trying to make a keylogger on Ubuntu 16.04LTS for a while now, and this is what I have so far:
#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdbool.h>
int main()
{
char devname[] = "/dev/input/event0";
int device = open(devname, O_RDONLY);
struct input_event ev;
bool logging = true;
while(logging)
{
if (read(device,&ev, sizeof(ev)) >= 0){
printf("Key: %i State: %i Type: %i\n",ev.code,ev.value,ev.type);
}
}
}
However when I compile and run it (gcc), it does not output anything!
I've tried every device listed in /dev/input/by-id andthensome,but nothing seems to work.
When I compile the code using GCC, I get the warning:
keylogger.c: In function ‘main’:
keylogger.c:15:7: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
if (read(device,&ev, sizeof(ev)) >= 0){
^
Which I have no idea if this has to do with the functionality of the program.
Any help is appreciated! Thanks!
I figured it out, it was a simple matter of not having superuser permissions. I excecuted the file using sudo and now everything is fine.
I have done some search about problems when using time.h to obtain a random seed initialization. Particularly in my case, I want to place the time outside the main function.
Based on the comments I made some changes. After including twice in the include as and , these errors and warning went off:
too few arguments to function ‘random’
implicit declaration of function ‘srandom’ [-Wimplicit-function-declaration]
too few arguments to function ‘random’
Once I declared the code as:
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include <math.h>
int main(void)
{
struct timeval time;
...
}
It works, however it has to move outside main. But if I change to:
#include <sys/time.h>
struct timeval time;
int main(void)
{
...
}
It gives me the error:
‘time’ redeclared as different kind of symbol
In my case the implementation I am working on is in C not in C++. When I try to move the code to a function in order to receive a different random number every time my function is called, some other errors happen:
double myRandom(double dAverage,double dStddev);
double myRandom(double dAverage,double dStddev){
int iRandom1, iRandom2;
double dRandom1, dRandom2,result;
long int liSampleSize;
iRandom1=(random());
dRandom1=(double)iRandom1 /2147483647;
iRandom2=(random());
dRandom2=(double)iRandom2 /2147483647;
result= dAverage + dStddev * sqrt(-2.0 * log(dRandom1))*cos(6.28318531 * dRandom2);
return(result);
}
int main(void){
...
struct timeval time;
gettimeofday(&time, NULL);
srandom((unsigned int) time.tv_usec);
for (i=0; i<liSampleSize;i++)
{ result=myRandom(dAverage,dStddev);
}
}
Apparently it should be working. Has somebody any idea what is wrong here. All comments are highly appreciated.
Update: Then, taking srandom out of myRandom makes the generated values as expected. So, it worked!
The first error is related to time.h. Including it introduces some instance called time to your global namespace.
The second is most likely because you haven't included stdlib.h.