I'm trying to write a test program using windows.h. I have a simple main function with a function in it and doesn't work when I compile it:
#include <windows.h>
#include <stdio.h>
int main(){
COORD coords;
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, &coords);
return 0;
}
But when I compile it using "mingw32-gcc main.c" i get:
main.c: In function 'main':
main.c:107:5: warning: implicit declaration of function 'SetConsoleDisplayMode' [-Wimplicit-function-declaration]
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, &coords);
^~~~~~~~~~~~~~~~~~~~~
main.c:107:60: error: 'CONSOLE_FULLSCREEN_MODE' undeclared (first use in this function)
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, &coords);
^~~~~~~~~~~~~~~~~~~~~~~
main.c:107:60: note: each undeclared identifier is reported only once for each function
(It says it's in line 107 because I wrote additional functions that does not need windows.h and don't cause trouble as I don't use them, also they are commented)
Related
Couple days ago it was working fine, but trying to use again today, my code editor cannot find sincos anymore and GCC throws me a warning that it cannot find sincos when compiling.
Here's the code:
// file: main.c
#include <math.h>
int main() {
double sin, cos;
sincos(0.0, &sin, &cos);
return 0;
}
Using gcc:
$ gcc main.c -lm
x.c: In function ‘main’:
x.c:5:2: warning: implicit declaration of function ‘sincos’ [-Wimplicit-function-declaration]
5 | sincos(0.0, &sin, &cos);
| ^~~~~~
x.c:5:2: warning: incompatible implicit declaration of built-in function ‘sincos’
x.c:2:1: note: include ‘<math.h>’ or provide a declaration of ‘sincos’
1 | #include <math.h>
+++ |+#include <math.h>
2 |
It says I should include math.h yet I do.
It says it can't find sincos yet it compiles and runs fine. I'm just annoyed by those warnings. Anyone knows what's wrong?
Add the following to the top of the file to enable the gnu extension:
#define _GNU_SOURCE
#include <math.h>
This will prevent the warnings. Note that this is a glibc extension and not part of the C standard.
I want to define an enumeration type ONCE and have that type be shared across all the other files when I include the file, however I keep getting the following errors:
$ gcc -std=c99 main.c invoc.h invoc.c
main.c: In function ‘main’:
main.c:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pr_alg’
main.c:12: error: ‘pr_alg’ undeclared (first use in this function)
main.c:12: error: (Each undeclared identifier is reported only once
main.c:12: error: for each function it appears in.)
main.c:13: error: ‘FIFO’ undeclared (first use in this function)
invoc.c:7: error: expected ‘)’ before ‘myalg’
The code is as follows:
invoc.h:
#define INVOC_H
#ifndef INVOC_H
typedef enum {FIFO, SECOND_CHANCE, RANDOM, NRU, CLOCK, AGING} alg_t;
void func1(alg_t myalg);
#endif
invoc.c:
#include "invoc.h"
void func1(alg_t myalg) {
myalg = NRU;
}
main.c:
#include "invoc.h"
int main(int argc, char **argv) {
extern alg_t pr_alg;
pr_alg = FIFO;
printf("PR_ALG: %d\n", pr_alg);
return 0;
}
Is there any way that I can define an enumeration in a .h file, and include it in all other files so that I can both create different variables of that type and pass it to functions?
You have an error in your invoc.h file:
#define INVOC_H
#ifndef INVOC_H
...
#endif
You first define a macro INVOC_H, then check if it does not exists (it does), so the code inside is removed by the preprocessor and not parsed by the compiler.
It should be:
#ifndef INVOC_H
#define INVOC_H
...
#endif
After this change your code will work fine.
You don't compile .h files, only .c files. That's why we put all definitions in .c files, and only declarations in .h files. To compile, just do:
gcc -std=c99 mmu.c invoc.c
You declare pr_alg in main() as extern variable. If the line you provided is the whole compilation line, the compile will issue linker error as variable pr_alg is nowhere defined. Remove extern or define variable pr_alg with global storage duration in one of the .c files.
I'm very new to C programming so wondered if anyone could help with these warnings. (I am trying to compile someone else's code, I couldn't code these lol)
src/connection.c:79:9: warning: implicit declaration of function 'close' [-Wimplicit-function-declaration
close(conn->fd);
src/main.c: In function 'main':
src/main.c:37:16: warning: implicit declaration of function 'inet_addr' [-Wimplicit-function-declaration]
addrs[0] = inet_addr("192.168.252.20"); // Address to bind to
src/server.c: In function 'server_queue_telnet':
src/server.c:93:9: warning: implicit declaration of function 'sleep' [-Wimplicit-function-declaration]
sleep(1);
src/telnet_info.c: In function 'telnet_info_parse':
src/telnet_info.c:59:12: warning: implicit declaration of function 'inet_addr' [-Wimplicit-function-decla
addr = inet_addr(addr_str);
src/telnet_info.c:60:12: warning: implicit declaration of function 'htons' [-Wimplicit-function-declarati
port = htons(atoi(port_str));
src/server.c: In function 'server_queue_telnet':
src/server.c:93:9: warning: implicit declaration of function 'sleep' [-Wimplicit-function-declaration]
sleep(1);
src/util.c:94:9: warning: implicit declaration of function 'close' [-Wimplicit-function-declaration]
close(fd);
src/util.c: In function 'util_trim':
src/util.c:161:11: warning: implicit declaration of function 'isspace' [-Wimplicit-function-declaration]
while(isspace(*str))
I managed to fix the above warnings by including the below missing include statements in the appropriate files.
include <unistd.h>
include <arpa/inet.h>
include <ctype.h>
However the below warnings still persist and I cannot work out which include statements are missing. When I do man ntohs , man htons and man rand I cannot see any specified libraries that should be included in connection.c and util.c files
src/connection.c: In function 'connection_close':
src/connection.c:62:17: warning: implicit declaration of function 'ntohs' [-Wimplicit-function-declaratio
ntohs(conn->info.port),
src/connection.c: In function 'connection_consume_arch':
src/connection.c:485:35: warning: implicit declaration of function 'htons' [-Wimplicit-function-declarati
ehdr->e_machine = htons(ehdr->e_machine);
src/util.c: In function 'util_socket_and_bind':
src/util.c:77:18: warning: implicit declaration of function 'rand' [-Wimplicit-function-declaration]
start_addr = rand() % srv->bind_addrs_len;
I have a script that compiles fine on Linux (Ubuntu 11.04), but not on OS X (Lion).
gcc -pthread -o hw1 hw1.c
hw1.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘barr’
hw1.c: In function ‘__syncthreads’:
hw1.c:53: error: ‘barr’ undeclared (first use in this function)
hw1.c:53: error: (Each undeclared identifier is reported only once
hw1.c:53: error: for each function it appears in.)
hw1.c:54: error: ‘PTHREAD_BARRIER_SERIAL_THREAD’ undeclared (first use in this function)
hw1.c: In function ‘parallel_psum’:
hw1.c:94: error: ‘barr’ undeclared (first use in this function)
hw1.c:107: warning: assignment from incompatible pointer type
Here's the first 22 lines of the code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#include <pthread.h>
#include <assert.h>
/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
int tid;
int* ints;
int* sums;
int num_ints;
int* temp;
} thread_data_t;
const int MIN_RAND_INT = 1;
const int MAX_RAND_INT = 65000;
// pthreads barrier variable
pthread_barrier_t barr;
Any ideas why this is happening?
According to info about pthread_barriers on opengroup.org, barriers are defined in the optional part of POSIX standard; the name of option is "(ADVANCED REALTIME THREADS)", sometimes more exact referred as "BAR, barriers (real-time)".
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap02.html
The system may support one or more options (see Options) denoted by the following symbolic constants:
_POSIX_BARRIERS
So, only if the _POSIX_BARRIERS macro is defined as positive number, you can use pthread_barrier_t or pthread_barrier_wait.
Mac OS X is POSIX Compliant, but full list of implemented options is not available online. There is a letter in apple mainling list from 2006, which says there is no barriers in Mac OS X.
I know that Solaris had some problems with pthread_barrier too.
Just like osgx mentioned, barriers are not implemented on OS X, but you can always implement it or just use this implementation. Quick note on the previous implementation, you can use the macro that osgx mentioned, _POSIX_BARRIERS, instead of the ones on the blog, like this #if !defined _POSIX_BARRIERS || _POSIX_BARRIERS < 0
I have a few source code files, such hashtable.c and such. The main issue is that when I write my main.c as such:
#include "tokens.h"
#include <stdio.h>
void yyerror(char *errorMsg)
{
fprintf(stderr, "%s\n", errorMsg);
}
main()
{
yyparse();
hsh = createHashtable();
}
And at the top of my yacc file (parser.y), I want to declear a hash table as such:
%{
#include <stdio.h>
#include "tokens.h"
#include "ast.c"
struct hashtable *hsh;
.............................
..............................
However I am getting this error.
main.c: In function ‘main’:
main.c:24: error: ‘hsh’ undeclared (first use in this function)
main.c:24: error: (Each undeclared identifier is reported only once
main.c:24: error: for each function it appears in.)
make: *** [main.o] Error 1
I am rather naive while it comes to C programming, any assistance will be greatful
You need an extern struct hashtable* hsh; in your main.c