Undeclared identifier with definition in header file - c

include "spinach.h" is included in the main.c file
main.c makes a function call to a struct defined in spinach.h header. The function call occurs in the main.c function, and is the first reference to this type Lexer. The main.c file runs without error absent this line.
Lexer *lex = malloc(sizeof(Lexer));
The definition is on the 0 indentation level (it is not defined in local scope); it is given below in triple quotes. In the header (spinach.h)
typedef struct {
char **lines;
char* current;
int pos;
int line;
int col;
int* indentlevel;
int indentcap;
int indentlen;
} Lexer;
What happpens?
 bash run.sh
main.c:115:3: error: use of undeclared identifier 'Lexer'
Lexer *lex;
^
main.c:115:10: error: use of undeclared identifier 'lex'
Lexer *lex;
^
2 errors generated.
Any variation that includes Lexer, including using pointers, references to malloc or the output of a custom initializer function will throw related errors.
Looked for multiple references (same global variable in 2 files in main.py references) to Lexer. All references on the internet are examples wherein the header file is not included, the variable or function is defined after the reference or call, or is absent entirely, else the error will show where there shows a reference in a global scope to a local definition. I do not see this to be the case. All references to custom struct lever in the main.c file, main function will throw this error. If the definitions are placed in the main.c file itself, instead of the header spinach.h referenced, it runs without error.

Related

Multiple definitions of 'some variable' which is declared in a header file

I have a header file command.h which contains all my variables and function declarations
//command.h
int someVar1;
int someVar2;
void modifying_loop (int a, int b);
int someVar3;
.
.
.
In another file my_algorithm.c I define the previously declared function modifying_loop and use some of the variables declared in the header in
//my_algorithm.c
#include "command.h"
void modifying_loop (int x, int y)
{
someVar1 = x+2;
someVar2 = y+2;
}
And I have my main file command.c I called the modifying_loop function like this :
#include "command.h"
int main ()
{
modifying_loop(5,6);
return 0;
}
I compile the it using gcc -o command command.c -lm -lpigpio -L/usr/lib/ which returns me
undefined reference to modifying_loop'
Then to tackle that I link the my_algorithm.c file by using
gcc -o command command.c my_algorithm.c -lm -lpigpio -L/usr/lib/ which gives me the following :
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here
and same errors for the rest of the variables declared in the header file. Does anyone have any idea what is causing the errors.
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here
you have to make sure, the same code won't be included multiple times.
You can do this by embedding the header file code in #ifndef #define #endif macros. These are called include guards
The rest of your code seems to be working fine.

C - Function has internal linkage but is not defined

I have this folder structure:
I wrote a simple function to test the folder structure, for seen if all header file but when I compile with the make command, I have this error:
warning: function 'stampa' has internal linkage but is not defined
I the lagrange.h file I have this:
#ifndef LAGRANGE_H
static void stampa(int i);
#endif /* LAGRANGE_H */
and in the lagrange.c file I have this:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdio.h>
void stampa(int i){
printf("%d", i);
}
in the end, int main.c I simply call the function stampa passing them a number.
Explanation of the error:
The compilation error occurs because:
You have declared a function with static keyword (i.e. with internal linkage) inside lagrange.h
And you have included this file lagrange.h in main.c file different from lagrange.c, where the function is not defined.
So when the compiler compiles main.c file, it encounters the static declaration without any associated definition, and raises logically the error. The error message is explicit.
Solution:
In your case the solution is to remove the static keyword, because static means that the function can be called only in the .c file where it is defined, which is not your case.
Moreover, a good practice may be to declare any static function in the same .c file where it is defined, and not in .h file.

Implicit declaration of function in spite of including header file

I have a function myfunc defined in a source file myfunc.c and declared in a header file myfunc.h. Both these files are part of a library.
In another project's (projA) source file, I am including the header file as:
#include "myfunc.h"
and using the function correctly (number of parameters, order, etc).
I've edited the Makefile so it has the path to myfunc.h in it's list of includes (-I).
However, I am still getting a warning about implicit declaration. Since projA has warning = error set, it fails on compilation.
Note: this is not an eclipse issue as here, or a missing header as here, or an undeclared function.
Addendum
int myfunc(char * source, size_t source_len, char * dest, size_t dest_len)
{
// manipulation
strncpy(dest, source, dest_len);
// other stuff
}
Take a look at this https://gcc.gnu.org/onlinedocs/cpp/Ifdef.html
You may need to add this in to your Header files to avoid duplicate inclusion

Warnings while organising variables and functions in C over multiple header and source files

For the first time i was trying to write an actual professional C code for a simple program.
1)I made a header file name Essential_data.h and declared all my functions and global variables in that. I had declared all my variables as extern.. And all function declaration were made normally
eg:
void test ();
extern int x;
2)Then i made another header file named main_data.h and defined all my global variables there.
for eg:int x;
3)I then made corresponding source files containing the definition of the respective functions and included main_data.h in sourcefiles that needed that global variable.
thats all. After when i compiled the project i got many warnings for all functions as
Implicit declaration of function test [-Wimplicit-function-declaration]
so after that i did the following
1) I put an extern in front of the functions declarations in Essential_data.h.
for eg:
extern void test();
2) in the main_data.h i declared the functions normally and wrote
void test ();
both the times my Essential_data.h was in the main function only and no where else.
And then recompiled and later all the warnings disappeared.
So was that the right method or is there any other way of organising them more efficiently?
You should have:
One or more .h files containing extern declarations (functions and objects). These files needs to be included by the .c files that need those symbols. You shouldn't have multiple declarations (each identifier should be declared exactly once)
One or more .c files in which you define the functions and objects
In your example:
/* essential_data.h */
void test ();
extern int x;
/* something.c */
#include "essential_data.h"
int x;
void test()
{
/* ... */
}
/* main.c */
#include "essential_data.h"
/* ... */

warning in extern declaration

I declared a variable i in temp2.h
extern i; which contains just one above line
and made another file
temp3.c
#include<stdio.h>
#include<temp2.h>
int main ()
{
extern i;
i=6;
printf("The i is %d",i);
}
When I compiled above as
cc -I ./ temp3.c I got following errors
/tmp/ccJcwZyy.o: In function `main':
temp3.c:(.text+0x6): undefined reference to `i'
temp3.c:(.text+0x10): undefined reference to `i'
collect2: ld returned 1 exit status
I had declared extern in temp3.c above as K R page 33 says as I mentioned in above post.
I tried another way for temp3.c with same header file temp2.h
#include<stdio.h>
#include<temp2.h>
int main ()
{
i=6;
printf("The i is %d",i);
}
and compiled it cc -I ./ temp3.c and got following error
/tmp/ccZZyGsL.o: In function `main':
temp3.c:(.text+0x6): undefined reference to `i'
temp3.c:(.text+0x10): undefined reference to `i'
collect2: ld returned 1 exit status
I also tried
#include<stdio.h>
#include<temp2.h>
int main ()
{
extern i=6;
printf("The i is %d",i);
}
compiled this one
cc -I ./ temp3.c
got same error as in post 1
temp3.c: In function ‘main’:
temp3.c:5: error: ‘i’ has both ‘extern’ and initializer
So I have tried at least 3 different ways to use extern but non of them worked.
When you declare a variable using extern , you are telling the compiler that the variable was defined elsewhere and the definition will be provided at the time of linking. Inclusion is a different thing altogether.
extern
An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it; this states the type of the variable. The declaration may be an explicit extern statement or may be implicit from context.
-The C Programming Language
A variable must be defined once in one of the modules(in one of the Translation Units) of the program. If there is no definition or more than one, an error is produced, possibly in the linking stage (as in example 1 and 2).
Try something like the following
a.c
int i =10; //definition
b.c
extern int i; //declaration
int main()
{
printf("%d",i);
}
Compile, link and create an executable using
gcc a.c b.c -o executable_name
or
gcc -c a.c // creates a.o
gcc -c b.c // creates b.o
Now link the object files and create an executable
gcc a.o b.o -o executable_name
extern is declaration mechanism used to tell the compiler that the variable is defined in another file.
My Suggestion is that you define a variable in a ".c" file and then add the extern declaration in the corresponding ".h" file. In this way, the variable declaration will be available to all the source files which includes this header file and also it will be easier for one to identify in which ".c" it is actually defined.
The first program said:
The variable i (implicitly of type int) is defined somewhere else - but you didn't define it anywhere.
The second program tried to use a variable for which there was no declaration at all.
The third program tried to declare a variable without an explicit type (used to be OK; not allowed in C99), and said:
The variable i is defined somewhere else, but I want to initialize it here.
You are not allowed to do that.
So, the compiler is correct in all cases.
The declaration in the header 'temp2.h' should be fixed to extern int i; first. The implicit int is long obsolete.
You could fix the first example in several ways:
#include <stdio.h>
#include <temp2.h>
int main()
{
extern int i;
i=6;
printf("The i is %d",i);
return 0;
}
int i;
This defines the variable after the function - aconventional but legitimate. It could alternatively be in a separate source file that is separately compiled and then linked with the main program.
The second example could be fixed with:
#include <stdio.h>
#include <temp2.h>
int main()
{
int i=6;
printf("The i is %d",i);
return 0;
}
It is important to note that this example now has two variables called i; the one declared in temp2.h (but not actually referenced anywhere), and the one defined in main(). Another way of fixing it is the same as in the first possible fix:
#include <stdio.h>
#include <temp2.h>
int main()
{
i=6;
printf("The i is %d",i);
return 0;
}
int i;
Again, aconventional placement, but legitimate.
The third one can be fixed by similar methods to the first two, or this variant:
#include <stdio.h>
#include <temp2.h>
int i;
int main()
{
extern int i;
i=6;
printf("The i is %d",i);
return 0;
}
This still declares i in <temp2.h> but defines it in the source file containing main() (conventionally placed - at the top of the file). The extern int i; insides main() is doubly redundant - the definition in the source file and the declaration in the header mean that it does not need to be redeclared inside main(). Note that in general, the declaration in the header and the definition in the source file are not redundant; the declaration in the header provides a consistency check with the definition, and the other files that also use the variable and the header are then assured that the definition in the file containing main() is equivalent to the definition that the other file is using too.

Resources