C compile error - c

What does the following error mean when compiling.
Tilemap.h:21: error: conflicting types for ‘ThreeDWorld’
Tilemap.h:21: error: previous declaration of ‘ThreeDWorld’ was here
Tilemap.h:29: error: conflicting types for ‘CGPoint’
Tilemap.h:29: error: previous declaration of ‘CGPoint’ was here
Tilemap.h:31: error: conflicting types for ‘tileForCoordinates’
Tilemap.h:31: error: previous declaration of ‘tileForCoordinates’ was here
Why is it giving an error for what was there?My source file has one instance of it as such
typedef struct
{
int xPosition;
int yPosition;
}
CGPoint;

Are you including the header file from more than one place? Use a guard in the header file, if so.
For example, in Tilemap.h:
#ifndef TILEMAP_H
#define TILEMAP_H
// header file contents
#endif /* TILEMAP_H */

Stick some inclusion guards on your headers.
Your type definition is appearing more than once in your compilation unit.

You included the header file twice.
In my own code, I wrapped all header files with
#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
#endif
to avoid such errors.

Related

Undeclared identifier with definition in header file

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.

Redefinition error while accessing a global variable defined in executable from library

I have static library file(lib_XXX.a) with global variable defined in it. I am trying to access the global variable in my executable(exe_XXX.o).
Linker error is coming. Any help would be thankful.
Languae : c
OS : Ubuntu gcc compiler
Sample as follows
exe_xxx.o module has 2 files resource.h and main.c
resource.h code as follows :
#ifndef RESOURCE_H
#define RESOURCE_H
#define APL
extern const StructTest g_AplObjDef;
const StructTest g_AplObjDef = {
abc, def, ghi,
....
};
#endif //APL
main.c code as follows:
#include "resource.h"
....
....
....
lib_xxx.a has another main.c in it. Its sample code as follows:
#include "resource.h"
int main()
{
#if defined(APL)
fun1(g_AplObjDef);
#endif
}
I suspect the reason is because resource.h included in both the main.c files.
I couldn't way to get rid of this. Can anyone help ?
Error details:
/lib_XXX.a(lib_XXX_a-main.o):(.data.rel.ro.local+0x40): `g_AplObjDef' が重複して定義されています
/exe_xxx-main.o:(.data.rel.ro.local+0x260): ここで最初に定義されています
Above error is in Japanese.. 1st line says "Duplicate is defined". 2nd line says "Here it is defined"
This part:
const StructTest g_AplObjDef = {
abc, def, ghi,
....
};
is a definition, and should not be in a header. Move it to
a .c file.
The reason for this is that header files are textually inserted, so if a header has a definition, and is included from multiple translation units, the symbol will be defined multiple times, which is an error.
Here you are defining a variable in the header
const StructTest g_AplObjDef =
You should only declare, which you did the line before.
This definition should go into a code file, accessing it will be possible by the knowledge provded by the declaration in the header. But the definition in the header will be done in each code file which includes it, which causes the redundant definition mentioned in the error message.
Moving the definition (as you now have it in the header, including the {...} into the libs code file should help.
Note that having two main() will probably get you into trouble, I only focus this answer on the double definition of the variable.

Resolving Nested macro linking error

I am getting a linking error because a function is declared in a .h file but its definition is commented.
Below is the linking error:
--- Linking Error ---
Error: L6218E: Undefined symbol clk_SetHMSSAPMFreqPlan (referred
from VVDRV_clk_glue.o).
Now, the file where the function is being called has multiple nested if blocks like below:
#if defined(COMPILING_APPS_PROC) || defined(COMPILING_SPM_PROC)
#ifndef AXF_BUILD
eClkResult clk_SetFreqPlan(eClockFrequencyType eFreqPlan, uint32 eSubsystemLists) {
----some code----
#if defined(COMPILING_APPS_PROC) && !defined(AXF_BUILD)
clk_SetHMSSAPMFreqPlan(0, eFreqPlan); /* This function is giving a linking error */
#endif
#endif /*AXF_BUILD*/
#endif /*COMPILING_APPS_PROC*/
To get over the linking error, on top of the file I am declaring the macro:
#ifndef AXF_BUILD
#define AXF_BUILD 1
#endif
Because the other macro COMPILING_APPS_PROC has to be kept defined, I cannot change it.
Since, AXF_BUILD is now defined, I am expecting the function clk_SetHMSSAPMFreqPlan(0, eFreqPlan) will not be compiled and compilation should be clean. But, I am still getting the same linking error. What might be the reason?

Redefinition of structure

Below is the code of my header file trie.h.
The compiler keep showing the following error:
In file included from speller.c:11:
./trie.h:3:8: error: redefinition of 'letter'
struct letter
^
./trie.h:3:8: note: previous definition is here
struct letter
^
1 error generated.
make: *** [speller.o] Error 1
The code:
struct letter
{
int is_word;
struct letter* arr[27];
};
// fuctions
struct letter* create_trie();
void free_trie(struct letter* trie);
Most likely your file gets included multiple times, hence the redefinition error.
To avoid this problem use include guards:
#ifndef HEADERNAME_DEFINED
#define HEADERNAME_DEFINED
// your code goes here.
#endif // HEADERNAME_DEFINED
or you can use non-standard preprocessor directive like #pragma once to do the job. It results in less code, and sometimes faster compilation speed.
Put that on top of your file:
#pragma once
// your code goes here
Note: The comment (// HEADERNAME_DEFINED part) after #endif isn't necessary. It is just a hint for programmer to know what belongs together.

error: expected specifier-qualifier-list before

I am getting this following error,
I have a "A.c" file in which I have included a "b.h" file, which has a "c.h" file.
Now this c.h has structures which are getting used and they are all int.
the structures are used in the following way:
In "c.h" file
struct abc{
int a;<---- error
};
In "b.h"
struct def{
struct abc;
};
and I have used struct def in file "A.c" file.
Please, help me know what wrong have I done.
You probably have some nesting error, a missing ; or something that confuses the compiler.
I would recommend trying to get hold of the preprocessor output, so you can see what the compiler sees, once the #includes have been executed.

Resources