Resolving Nested macro linking error - c

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?

Related

Compiler says parameter list is wrong, then in next sentence says function is an unresolved external symbol? How is this possible? How to fix?

I'm confused by this. The compiler is complaining about an unresolved external symbol... but if I add a parameter to it, it still knows to complain about the added parameter.
The function definition with the types and macros, in util.h (which is already included by the header I'm including and I've tried including myself):
typedef u32 ticks_t;
#define TICK_DIFF(a,b) ((signed int)(((a)<<1)-((b)<<1))>>1)
#define TICK_GT(a,b) (TICK_DIFF(a,b) > 0)
#define TICK_MAKE(a) ((a) & 0x7fffffff)
ticks_t current_ticks(void);
So current_ticks() does not work, but the macros and types from the same header do work:
ticks_t now = TICK_MAKE(current_ticks());//does not complain about ticks_t or TICK_MAKE,
//but does complain about current_ticks??
TICK_DIFF(now,p->lastdeath);//no complaining at all??
Any ideas what is happening here?

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.

Enumerations in C head files shared across multiple files

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.

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.

C compile error

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.

Resources