I have a C header file which defines the following function:
void my_func(pthread_t tid);
This is defined by another function:
void my_func(pthread_t tid) {
...
When I compile, it says:
****.h:2: error: expected specifier-qualifier-list before ‘pthread_t’
Any ideas what I'm doing wrong?
You need to #include <pthread.h> in the header file so that pthread_t is in scope for the my_func() prototype.
Without the #include the compiler does not recognize pthread_t as a type, but it needs a type before the argument.
The error expected specifier-qualifier-list before ‘pthread_t’ is saying just that. You need a type (specifier-qualifier-list) before a parameter (‘pthread_t’).
Related
I'm writing this simple cuda code and I'm unable to compile it. The code contains part of code written in C. This is the structure of the program:
read_data.c file contains a function called read_data
add.cu file contains a function called add (this is the part that should run in the GPGPU)
optimize.h file contains the necessary headers.
master.c file contains the main function.
The optimize.h file looks like follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
__global__ void add(int, float*, float*);
void read_data(char*, float**, float**, int**);
master.c file looks like follows:
#include "optimize.h"
int main(){
char* path = "./data/0";
float* base_load;
float* comfortable_temperatures;
int* comfort_index;
read_data(path, &base_load, &comfortable_temperatures, &comfort_index);
int N = 1<<20;
float *x, *y;
int i;
cudaMallocManaged(&x, N*sizeof(float));
cudaMallocManaged(&y, N*sizeof(float));
for (i = 0; i < N; i++) {
x[i] = 1.0f;
y[i] = 2.0f;
}
add<<<1, 256>>>(N, x, y);
cudaDeviceSynchronize();
// still need to read the result back.
cudaFree(x);
cudaFree(y);
}
I compiled this using the following line:
nvcc -o master master.c read_data.c add.cu
and I'm getting this error:
In file included from master.c:1:0:
optimize.h:9:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
__global__ void add(int, float*, float*);
^
master.c: In function ‘main’:
master.c:51:26: error: ‘add’ undeclared (first use in this function)
add<<<1, 256>>>(N, x, y);
^
master.c:51:26: note: each undeclared identifier is reported only once for each function it appears in
master.c:51:31: error: expected expression before ‘<’ token
add<<<1, 256>>>(N, x, y);
I think the whatever the error is, it should be a very small one. But I cannot find it.
nvcc by default treats filenames ending in .c or .cpp as having no CUDA-specific syntax, and sends those to the host compiler. The host compiler cannot handle CUDA-specific syntax, which is why you are getting the errors.
The usual recommendations are to place your CUDA code in files ending with .cu. You can alternatively pass -x cu as a compile switch to do this.
Note that nvcc uses c++ style linkage, so you will need to arrange for correct c-style linkage if you are trying to link code in a .cu file with code in a .c file. If you have no C-specific usage, again, a simple solution may be to rename your .c file to .cpp or .cu.
There are many questions here on the cuda tag explaining how to do C++/C linkage, otherwise.
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.
When I try to compile my code, I'm getting the error:
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
I read that "Undefined symbol errors can occur when a function is declared (as is the case for the lock functions in lock.h), but it is not properly implemented." But I feel that I am implementing all the functions in lock.h properly since all the functions are of type voidand I don't return anything. So I don't know what I could be doing wrong to get this error.
Any help would greatly be appreciated!
Note: Please let me know if I need to provide more code. I don't think I need to write what I put in the function implementations of lock.h since I feel all you need to know is that the function implementations do not return anything, but please let me know if I need to include that.
Code
lock.c
#include "lock.h"
extern process_t * current_process;
extern process_t * process_queue;
void l_init(lock_t* l){
//Does stuff but never type return or return anything
}
void l_lock(lock_t* l){
//Does stuff but never type return or return anything
}
void l_unlock(lock_t* l){
//Does stuff but never type return or return anything
}
lock.h
#ifndef __LOCK_H_INCLUDED__
#define __LOCK_H_INCLUDED__
#include "3140_concur.h"
#include "shared_structs.h"
void l_init(lock_t* l);
void l_lock(lock_t* l);
void l_unlock(lock_t* l);
#endif /* __LOCK_H_INCLUDED */
3140_concur.c
#include "3140_concur.h"
#include <stdlib.h>
3140_concur.h
#ifndef __3140_CONCUR_H__
#define __3140_CONCUR_H__
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "process.h"
void process_blocked (void);
void process_terminated (void);
unsigned int * process_stack_init (void (*f)(void), int n);
void process_stack_free (unsigned int *sp, int n);
void process_begin (void);
#endif
process.h
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "3140_concur.h"
struct process_state;
typedef struct process_state process_t;
unsigned int * process_select (unsigned int * cursp);
extern process_t * current_process;
extern process_t * process_queue;
void process_start (void);
int process_create (void (*f)(void), int n);
process.c
(I do process_t* process_queue = NULL; & process_t* current_process = NULL; b/c I want them to be NULL before any functions are called)
#include "3140_concur.h"
#include "shared_structs.h"
#include <stdlib.h>
#include <fsl_device_registers.h>
process_t* process_queue = NULL;
process_t* current_process = NULL;
lab4_t0.c
#include "process.h"
#include "utils.h"
#include "lock.h"
lock_t l;
\\uses functions in lock.h
Update
Build output
*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'Target 1'
compiling 3140_concur.c...
compiling lab4_t0.c...
lab4_t0.c(42): warning: #111-D: statement is unreachable
return 0;
lab4_t0.c: 1 warning, 0 errors
compiling process.c...
process.c(100): warning: #1-D: last line of file ends without a newline
}
process.c: 1 warning, 0 errors
linking...
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
Not enough information to list image symbols.
Finished: 1 information, 0 warning and 3 error messages.
".\Objects\Lab4.axf" - 3 Error(s), 2 Warning(s).
Target not created.
Build Time Elapsed: 00:00:39
Thanks to #AjayBrahmakshatriya, it turns out I didn't add lock.c to my project. That fixed everything. Whew.
I improved my signal handling function but now when I try to compile my program via gcc ./test2.c -Wall -Wextra, I receive the following;
./test2.c: In function 'end_app':
./test2.c:21: warning: implicit declaration of function 'flock'
./test2.c: In function 'main':
./test2.c:46: warning: implicit declaration of function 'usleep'
./test2.c:47: warning: implicit declaration of function 'close'
This is the source code to test2.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
static int end=0;
static int f;
static void end_app(int sig){
printf("Ending from sig#%d",sig);
struct sigaction si;
si.sa_handler=SIG_DFL;
si.sa_flags=0;
sigaction(SIGCHLD,&si,NULL);
sigaction(SIGTSTP,&si,NULL);
sigaction(SIGTTOU,&si,NULL);
sigaction(SIGTTIN,&si,NULL);
sigaction(SIGSEGV,&si,NULL);
sigaction(SIGTERM,&si,NULL);
sigaction(SIGHUP,&si,NULL);
flock(f,LOCK_UN); //compiler gives implicit declaration warning
printf("Ended\n");end=1;
}
void newsig(){
struct sigaction s,o;
o.sa_handler=SIG_DFL;
o.sa_flags=0;
s.sa_handler=&end_app;
s.sa_flags=SA_RESTART;
sigaction(SIGCHLD,&s,&o);
sigaction(SIGTSTP,&s,&o);
sigaction(SIGTTOU,&s,&o);
sigaction(SIGTTIN,&s,&o);
sigaction(SIGSEGV,&s,&o);
sigaction(SIGTERM,&s,&o);
sigaction(SIGHUP,&s,&o);
}
int main(){
f=open("xx.pid",O_WRONLY|O_CREAT|0x700);
flock(f,LOCK_EX); //function works here
printf("Started\n");
newsig();
while(1){
usleep(1000);
if (end==1){close(f);return 0;}
}
return 0;
}
My question is why would the compiler complain about the use of flock in my signal handler function but it doesn't complain about it in the main function? and what can I do to rectify this? I need flock to work so that I can successfully lock and unlock the file.
As the warning says, the flock function is implicitly declared the first time you use it. Since it was already (implicitly) declared earlier in the file, the compiler doesn't complain about the later usage.
If you comment out the first call, then it should complain about the second.
You should include the header file where the function is declared to get rid of the warning. According to the manpage link in Joachim Pileborg's comment above, the header is <sys/file.h>.
You're getting that warning because you haven't added the proper #includes (i.e. you used those functions without an explicit declaration). When a function isn't given an explicit prototype, the compiler assumes each parameter type is int. This can cause undesired behavior when the size of actual function parameter types don't equal the size of int.
GCC only issues this warning once per function so your build output won't be cluttered with pages of the same message.
For flock() you need #include <sys/file.h>.
usleep() and close() require #include <unistd.h>.
I've been given function called statPrint to handle printing of the system call stat(). The function is provided with another .o file. I'm getting errors when compiling my implementation with that function:
In function ‘main’:
statcall.c:9:19: error: expected expression before ‘,’ token
statPrint(argv[1]*,sb*);
^
statcall.c:9:19: error: incompatible type for argument 2 of ‘statPrint’
statcall.c:4:8: note: expected ‘struct stat *’ but argument is of type ‘struct stat’
extern statPrint(char*,struct stat*);
Here is my code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
extern statPrint(char∗,struct stat∗);
int main(int argc, char *argv[])
{
struct stat sb;
stat(argv[1],&sb); ///argv[1] contains input from the terminal/shell
statPrint(argv[1]*,sb*);
}
I compile it with(libstat contains the external function):
gcc -o statcall statcall.c libstat.o
How do I get rid of the errors?
This line makes no sense:
statPrint(argv[1]*,sb*);
There's no valid syntax that ends with *.
I think you want:
statPrint(argv[1], &sb);
Recommend you read up on addresses of variables and pointers.
Your function expects char * please provide it
statPrint(argv[1],sb);
I really didn't get what is argv[1]*