error: dm_mmc_ops undeclared (first use in this function) - c

I'm constructing an u-boot bootloader for my embedded system (cyclone V) using Buildroot and I get the following error :
error: 'dm_mmc_ops' undeclared (first use in this function)
After several unsuccessful attempts to understand/solve the error, I've manage to isolate the problem which looks like the simple code hereafter and generates the same error :
File1.h
#ifndef FILE1
#define FILE1
struct dm_mmc_ops {
int (*send_cmd)(int data);
int (*set_ios)(char* dev);
};
struct dev {
struct dm_mmc_ops* ops;
} *dev;
#define mmc_get_ops(dev) ((dm_mmc_ops *)(dev)->ops)
#endif
File2.h
#ifndef FILE2
#define FILE2
#include "file1.h"
extern const struct dm_mmc_ops dm_dwmci_ops;
#endif
File2.c
#include <stdio.h>
#include "file1.h"
#include "file2.h"
int return_int (int data)
{
return data;
}
int return_ptr (char* data)
{
return (int) data;
}
const struct dm_mmc_ops dm_dwmci_ops = {
.send_cmd = return_int,
.set_ios = return_ptr
};
void main (void)
{
struct dev my_dev = {.ops = &dm_dwmci_ops};
dev = &my_dev;
char text[] = "abcd";
struct dm_mmc_ops *test_mmc = mmc_get_ops(dev); // Error is here !!!
printf("%d\n",test_mmc->send_cmd(50));
printf("%d\n",text);
printf("%d\n",test_mmc->set_ios(text));
return;
}
Then the error generated is :
error: 'dm_mmc_ops' undeclared (first use in this function)
What is wrong in my code and what should I do to get rid of this error ?

Your problem is here
#define mmc_get_ops(dev) ((dm_mmc_ops *)(dev)->ops)
^^^^^^^^^^
You probably want
#define mmc_get_ops(dev) ((struct dm_mmc_ops *)(dev)->ops)
Besides that you have a number of other problems. Set you compiler to a high warning level (e.g. gcc -Wall ...) and then fix all warnings.

Related

Define a function pointer to be32toh() function

I'm newly learned about function pointers here but I couldn't define a function pointer to be32toh() or other functions of endian.h header.
First of all, I can do what is told in the given thread:
#include <endian.h>
int addInt(int n, int m) {
return n+m;
}
int main(){
int (*functionPtr)(int,int);
functionPtr = addInt;
return 0;
}
But when I try to do the same for a function like be32toh(), I'll get a compilation error:
#include <stdint.h>
#include <endian.h>
int addInt(int n, int m) {
return n+m;
}
int main(){
int (*functionPtr)(int,int);
functionPtr = addInt;
uint32_t (*newptr)(uint32_t);
newptr = &be32toh;
return 0;
}
Compile as:
$ gcc test.c
Result is as below:
test.c: In function ‘main’:
test.c:15:15: error: ‘be32toh’ undeclared (first use in this function)
15 | newptr = &be32toh;
| ^~~~~~~
test.c:15:15: note: each undeclared identifier is reported only once for each function it appears in
What's the problem and how to fix it?
What's the problem
be32toh is a macro.
how to fix it?
Just write the function yourself.
uint32_t be32toh_func(uint32_t a) {
return be32toh(a);
}
....
newptr = &be32toh_func;

C - Pass struct by reference

I am trying to pass a structure by reference in C so that I can modify the values from within the function. This is the code I have so far, but it produces some warnings and one error.
main.c
#include <stdio.h>
#include "myfunctions.h"
#include "structures.h"
int main(int argc, char const *argv[] {
struct MyStruct data;
data.value = 6;
printf("Before change: %d\n", data.value);
changeData(data);
printf("After change: %d\n", data.value);
}
myfunctions.c
#include "structures.h"
void changeData(MyStruct data) {
data.value = 7;
}
myfunctions.h
#ifndef MyStruct
#define MyStruct
void changeData(MyStruct data);
#endif
structures.h
typedef struct {
int value;
} MyStruct;
Errors Produced
In file included from main.c:2:0:
myfunctions.h:4:1: warning: parameter names (without types) in function declaration
void changeData(MyStruct data);
^
In file included from main.c:3:0:
structures.h:5:1: warning: unnamed struct/union that defines no instances
} MyStruct;
^
main.c: In function ‘main’:
main.c:9:5: error: ‘data’ undeclared (first use in this function)
data.value = 6;
^
main.c:9:5: note: each undeclared identifier is reported only once for each function it appears in
That's all caused by
#define MyStruct
With this line, you've defined MyStruct as a macro that expands to nothing. I.e. you've effectively removed all occurrences of MyStruct in the following code, which is why the compiler is so confused about seeing things like
typedef struct {
int value;
} ;
or
void changeData( data);
To fix this, use
#ifndef MYFUNCTIONS_H_
#define MYFUNCTIONS_H_
instead. (This is the reason why we use ALL_UPPERCASE names for macros: To avoid accidental name clashes with normal identifiers.)
applying all my comments and elimination of the unnecessary 'typedef', and placing it all in one file ( Note: there is no problem with extracting the various files), results in the following code:
#ifndef STRUCTURES_H
#define STRUCTURES_H
struct MyStruct
{
int value;
};
#endif // STRUCTURES_H
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
void changeData( struct MyStruct *data);
#endif // MYFUNCTIONS_H
#include <stdio.h>
//#include "myfunctions.h"
//#include "structures.h"
int main( void )
{
struct MyStruct data;
data.value = 6;
printf("Before change: %d\n", data.value);
changeData(&data);
printf("After change: %d\n", data.value);
} // end function: main
//#include "structures.h"
void changeData( struct MyStruct *data)
{
data->value = 7;
} // end function: changeData
which cleanly compiles and does do the desired operation

Error *** has no member named ***

I don't know what i'm doing wrong ..
Do you have any ideas what i'm doing wrong? Structure was declared in header file sender.h - code below
After trying to compile this program I got this error:
Sender/Sender.c: In function 'SenderCreate':
Sender/Sender.c:50: error: 'Sender' has no member named 'sim_buf'
Sender/Sender.c:51: error: 'Sender' has no member named 'sim_buf_length'
Sender/Sender.c: In function 'SenderExecuteTask':
Sender/Sender.c:75: error: 'sim_buf_length' undeclared (first use in this function)
Sender/Sender.c:75: error: (Each undeclared identifier is reported only once
Sender/Sender.c:75: error: for each function it appears in.)
Sender/Sender.c:77: error: 'sim_buf' undeclared (first use in this function)
make: *** [Sender.o] Error 1
Code of program below:
#include <stdlib.h> // calloc, free
#include <stdio.h>
#include "Sender.h"
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "../../rdo_module/readout_dev.h"
struct Sender_s {
ET4DataSource *data_source;
unsigned int num_of_images;
unsigned int num_of_el;
ConditionWait *condition_wait;
int sock;
struct sockaddr *PC;
char *sim_buf;
char *sim_buf_length;
};
void SenderSetNumOfEl(Sender *self, unsigned int num_of_el) {
self->num_of_el = num_of_el;
}
Sender *
SenderCreate(ET4DataSource *data_source,
unsigned int num_of_images,
unsigned int num_of_el,
ConditionWait *condition_wait,
int sock,
struct sockaddr *PC,
char *sim_buf, // here I get some problems
int *sim_buf_length) // and here
{
#ifdef DEBUG
printf("Sender.c SenderCreate line 25: Sender *self = calloc(1, sizeof(Sender));\n");
#endif
Sender *self = calloc(1, sizeof(Sender));
#ifdef DEBUG
printf("Sender.c SenderCreate line 25: success\n");
#endif
self->data_source = data_source;
self->num_of_images = num_of_images;
self->num_of_el = num_of_el;
self->condition_wait = condition_wait;
self->sock = sock;
self->PC = PC;
self->sim_buf = sim_buf;
self->sim_buf_length = sim_buf_length;
return self;
}
void
SenderDestroy(Sender *self)
{
free(self);
}
void *
SenderExecuteTask(void *self_)
{
Sender *self = self_;
ET4Buffer *buf = NULL;
int n = 0;
int c_len = sizeof(*(self->PC));
while(1) {
if(*sim_buf_length) {
n=sendto(self->sock, sim_buf, *sim_buf_length, 0, self->PC, c_len);
if(n < 0) {
perror("error in sendto()");
return NULL;
}
}
return NULL;
}
Code of sender.h below:
#ifndef __SENDER_H__
#define __SENDER_H__
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "../Utils/ConditionWait.h"
#include "../DataSource/ET4DataSource.h"
typedef struct Sender_s Sender;
Sender *
SenderCreate(ET4DataSource *data_source,
unsigned int num_of_images,
unsigned int num_of_el,
ConditionWait *condition_wait,
int sock,
struct sockaddr *PC,
char *sim_buf,
int *sim_buf_length);
void
SenderDestroy(Sender*self);
void *
SenderExecuteTask(void *self_);
void SenderSetNumOfEl(Sender *self, unsigned int num_of_el);
#endif /*__SENDER_MAKER_H__*/
The error messages and the source in the question don't match!
When I take the source given, the compiler tells, what is wrong.
There is no member sim_buf_length, (note it does not complain about sim_buf_length_flag)
I don't get the error message "error: ‘Sender’ has no member named ‘sim_buf’", because the member is clearly present
Furthermore the types char* (Sender_s member) and int* (SenderCreate argument) don't match
The error messages for function SenderExecuteTask are clear, there are no variables declared sim_buf or sim_buf_length(_flag). Probably the function signature should have been
void *SenderExecuteTask(Sender *self);
and then in the definition self->sim_buf and self->sim_buf_length(_flag) used respectively.

Trying to compile a simple file in C

I am writing you because I wrote the following file sources in C and I am not able to compile it with gcc... I get the error unknow type name :(
I searched on the internet but in most of cases it appears when there is a circular dependency or something like that.
So I have a source file and a header file :
vector.h :
#ifndef VECTOR_H
#define VECTOR_H
#include <stdio.h>
typedef struct dynamic_array_struct
{
uint64_t *data;
size_t capacity;
size_t size;
} vector;
int
vector_init(vector *, size_t);
int
vector_reinit(vector *);
#endif /* VECTOR_H */
and vector.c
#include <stdlib.h>
#include <vector.h>
int
vector_init(vector *v, size_t init_capacity)
{
v->data = (uint64_t *)malloc(init_capacity*sizeof(uint64_t));
v->capacity = init_capacity;
v->size = 0;
return 1;
}
int
vector_reinit(vector *v)
{
free(v->data);
v->data = (uint64_t *)malloc(v->capacity*sizeof(uint64_t));
v->size = 0;
return 1;
}
But when I try to compile it with gcc -c vector.c -I . I get the following error :
In file included from vector.c:2:0:
./vector.h:2:13: error: unknown type name ‘vector’
vector_init(vector *, size_t);
^
./vector.h:5:15: error: unknown type name ‘vector’
vector_reinit(vector *);
^
vector.c:6:13: error: unknown type name ‘vector’
vector_init(vector *v, size_t init_capacity)
^
vector.c:14:15: error: unknown type name ‘vector’
vector_reinit(vector *v)
Could you please tell me what I am doing wrong ?
Thanks for helping me :)
You should include vector.h like this:
#include "vector.h"
not like this:
#include <vector.h>

rpc remote proceduer compile error

I'm working on a rpc sample program on linux. When I try to compile my remote procedure I get this error :
msg_proc.c:10:7: error: conflicting types for ‘printmessage_1’
In file included from msg_proc.c:8:0:
msg.h:22:15: note: previous declaration of ‘printmessage_1’ was here
This is the command I used to complie :
cc msg_proc.c msg_svc.c -o msg_server -lnsl
And these are my header and procedure files :
/*msg.h
*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#ifndef _MSG_H_RPCGEN
#define _MSG_H_RPCGEN
#include <rpc/rpc.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MESSAGEPROG 0x20000001
#define PRINTMESSAGEVERS 1
#if defined(__STDC__) || defined(__cplusplus)
#define PRINTMESSAGE 1
extern int * printmessage_1(char **, CLIENT *);
extern int * printmessage_1_svc(char **, struct svc_req *);
extern int messageprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);
#else /* K&R C */
#define PRINTMESSAGE 1
extern int * printmessage_1();
extern int * printmessage_1_svc();
extern int messageprog_1_freeresult ();
#endif /* K&R C */
#ifdef __cplusplus
}
#endif
#endif /* !_MSG_H_RPCGEN */
/*
* msg_proc.c: implementation of the
* remote procedure "printmessage"
*/
#include <stdio.h>
#include <rpc/rpc.h>
#include "msg.h"
int * printmessage_1(char **msg, struct svc_req *req) {
static int result; /* must be static! */
FILE *f;
f = fopen("/dev/console", "w");
if (f == (FILE *) NULL) {
result = 0;
return (&result);
}
fprintf(f, "%s\n", *msg);
fclose(f);
result = 1;
return (&result);
}
What's wrong with my code ?
The argument types in your printmessage_1 function match the declaration of printmessage_1_svc, not printmessage_1. – Barmar

Resources