header bs.h
#ifndef BS_H
#define BS_H
extern int glob_1;
#endif
header og.h
#ifndef OG_H
#define OG_H
#include < bs.h >
extern void func_1( void );
#endif
func_1
#include < og.h >
extern void func_1( void )
{
int dummy;
dummy = 22;
glob_1 += dummy;
}
mainfile
#include < bs.h >
#include < og.h >
int glob_1;
int main()
{
glob_1 = 33;
func_1();
return 0;
}
I have made a small sample that is structured as i understand how to
declare, define init and share globals among different modules.
But this throws me a lnk2019 error - unresolved symbol glob 1
What is wrong here?
Related
Good morning.
I have a problem - I created definition of the myOwnType and I still have a problem with compile. Please help. C language (not C++).
main.c:
#include <stdio.h>
#include "additional.h"
#include "config.h"
int main(int argc, char *argv[])
{
return 0;
}
config.h:
#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_
#include "additional.h"
typedef struct MyType
{
int x;
} MyOwnType_t;
#endif /* SRC_CONFIG_H_ */
additional.c:
#include "config.h"
void foo(MyOwnType_t x)
{
}
additional.h:
#ifndef SRC_ADDITIONAL_H_
#define SRC_ADDITIONAL_H_
#include "config.h"
extern void foo(MyOwnType_t x);
#endif /* SRC_ADDITIONAL_H_ */
Compiler generate error:
..\Src\additional.h:13:10: error: unknown type name 'MyOwnType_t'
Help me please.
Expanding your includes in additional.c gets you:
#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_
#ifndef SRC_ADDITIONAL_H_
#define SRC_ADDITIONAL_H_
/* no effect #include "config.h" */
#ifndef SRC_CONFIG_H_
/* not seen because SRC_CONFIG_H_ is already defined here, from above line 2ish */
#endif /* SRC_CONFIG_H_ */
extern void foo(MyOwnType_t x);
#endif /* SRC_ADDITIONAL_H_ */
typedef struct MyType
{
int x;
} MyOwnType_t;
#endif /* SRC_CONFIG_H_ */
void foo(MyOwnType_t x)
{
}
"no effect" refers to the fact that the second include of config.h meets an already defined reinclusion guard, the content is not seen in this place. The content IS seen from the first include, but later, AFTER the content of additional.h which would need the content from config.h.
Cleaning that up gets you:
/* no effect #include "config.h" */
extern void foo(MyOwnType_t x);
typedef struct MyType
{
int x;
} MyOwnType_t;
void foo(MyOwnType_t x)
{
}
In the first line, extern void foo(MyOwnType_t x); you use a not yet known type, MyOwnType_t.
The compiler does not like that.
I am using a structure that I defined in main.h and now using it in stack.c. In my main.h I have defined struct details and struct library. When I access them in stack.c I get the following error. When I am running a single main.c file then it's having no issues, so, I assume the issue lies in the stack.c or stack.h file.
Undefined symbols for architecture x86_64:
"_details", referenced from:
_main in main.o
_library_details in main.o
_push in stack.o
(maybe you meant: _library_details)
"_library", referenced from:
_main in main.o
_library_details in main.o
_push in stack.o
(maybe you meant: _library_details)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation
Here are my code snippets:
main.c
// write code below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
void library_details(void);
FILE *fp;
int choice, indexer = 1;
//details_struct details[maxsize];
//library_struct library[maxsize];
int main(void)
{
// library = malloc(sizeof(library_struct) * 2);
fp = fopen("input.txt","r");
library_details();
for (int i = 1; i < indexer; i++)
{
if(library[i].type == is_book)
{
printf("Item %i is book: %s with %i pages\n", i, details[i].title, details[i].pages);
}
else if(library[i].type == is_article)
{
printf("Item %i is article: %s with %i pages\n", i, details[i].title, details[i].pages);
}
}
fclose(fp);
//free(library);
return 0;
}
main.h
#ifndef __MAIN_H_
#define __MAIN_H_
enum book_type {is_book, is_article};
typedef struct library_struct
{
enum book_type type;
void *item;
}library_struct;
typedef struct details_struct
{
char title[50];
int pages;
}details_struct;
// external variables
extern int choice, indexer;
extern details_struct details[100];
extern library_struct library[100];
// library details
void library_details(void);
#endif // __MAIN_H_
stack.c
// stack.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "main.h"
//functions declarations
void push(void);
stack books_stack[100];
int top = 0;
int stack_main(void)
{
return 0;
}
void push()
{
if(top >= 99)
{
printf("Stack Overflow\n");
exit(-1);
}
else
{
if(library[top].type == is_book)
{
strcpy(books_stack[top].s_title, details[top].title);
books_stack[top].s_pages = details[top].pages;
}
}
}
stack.h
#ifndef __STACK_H_
#define __STACK_H_
typedef struct stack
{
char s_title[50];
int s_pages;
}stack;
// functions declaration
void push();
#endif // __STACK_H_
Is there any issue with struct declarations?
Haven't used C in a while so I can't straight answer your question, but I made your code compile. However the compiled program has a C runtime abort. (Potentially some exception or something...) Examine this and consider the diff... in h you never instantiate, you declare, in c you instantiate your declaration. I don't know why you were doing extern + trying to instantiate... The other answers noted a most of the areas I messed with.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
FILE *fp;
int choice, indexer = 1;
int maxsize = 50;
details_struct details[100];
library_struct library[100];
void library_details(void)
{
return;
}
int main(void)
{
// library = malloc(sizeof(library_struct) * 2);
fp = fopen("input.txt","r");
library_details();
for (int i = 1; i < indexer; i++)
{
if(library[i].type == is_book)
{
printf("Item %i is book: %s with %i pages\n", i, details[i].title, details[i].pages);
}
else if(library[i].type == is_article)
{
printf("Item %i is article: %s with %i pages\n", i, details[i].title, details[i].pages);
}
}
fclose(fp);
//free(library);
return 0;
}
main.h
#ifndef __MAIN_H_
#define __MAIN_H_
enum book_type {is_book, is_article};
typedef struct library_struct
{
enum book_type type;
void *item;
} library_struct;
typedef struct details_struct
{
char title[50];
int pages;
} details_struct;
// external variables
extern int choice, indexer;
details_struct details[];
library_struct library[];
// library details
void library_details(void);
#endif // __MAIN_H_
stack.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stack.h"
//functions declarations
void push(void);
stack books_stack[100];
int top = 0;
int stack_main(void)
{
return 0;
}
void push() {
if (top >= 99) {
printf("Stack Overflow\n");
exit(-1);
} else {
if (library[top].type == is_book) {
strcpy(books_stack[top].s_title, details[top].title);
books_stack[top].s_pages = details[top].pages;
}
}
stack.h
#ifndef __STACK_H_
#define __STACK_H_
#include "main.h"
typedef struct stack
{
char s_title[50];
int s_pages;
} stack;
// functions declaration
void push();
#endif // __STACK_H_
}
library an library_details are not exist in your code
You declare the function in main.h, but is not in in main.c
And, with library[], you are using but is not declared
You seem to have commented out the variable declarations:
//details_struct details[maxsize];
//library_struct library[maxsize];
So, neither details nor library are defined, and this is what the compiler is complaining about.
test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
int func(void);
typedef int (*FPTR)(void);
#endif // TEST_H_INCLUDED
func.c
#include "test.h"
static int x = 22; // persistent with external linkage.
int func(void)
{
extern int x; // Referencing declaration
static int count = 0; // persistent within block
printf("%d : %d\n",++count,++x);
return 1;
}
FPTR funcptr = func; // persistent with external linkage. ??
main.c
#include "test.h"
#include <stdio.h>
extern funcptr; // referencing declaration ??
int main(void)
{
func();
funcptr(); // Compile Time Error Here
return 0;
}
This fails with error called object ‘funcptr’ is not a function or function pointer.
Am I breaking any cardinal rules here ?
Wrong syntax; should be
extern FPTR funcptr;
because an extern declaration still needs to mention a type.
And you'll better call it with (*funcptr)() which is at least more readable.
I have 4 files (2 headers, 2 source):
lcd.h,
lcd.c,
azt.h and
azt.c
I included lcd inside azt.
In lcd there's a function WriteMsg which is called inside azt,
it works fine.
But when I add a call to this function in lcd.c, I get the following error
Description Resource Path Location Type
unresolved symbol WriteMsg, first referenced in ./Includes/AutoZeroTracking.obj Joe1000 C/C++ Problem
I understand that I referenced it first at azt files, but this function is referenced in other places and I don't have this problem.
Here's the lcd.h:
#ifndef MSP
#define MSP
#include <msp430g2553.h>
#endif
#include "Utilities.h"
#include "Declarations.h"
#ifndef INCLUDES_LCD_H_
#define INCLUDES_LCD_H_
#define LCD_CALL 1
void InitLCD(unsigned char SDA,unsigned char SCL,unsigned char slaveAddress);
void StartLCD();
inline int ReadyToSend();
void SendToLcd(unsigned char* data, int size);
void WriteToLCD(int clearScreen);
void switchFrom8To4Bits();
void write2x4Bits(unsigned char bits, int is_data);
void writeCommand(unsigned char command);
inline void writeData(unsigned char data);
void ClearScreen();
void WriteWeight();
void WriteWeightMode();
inline void WriteMsg(const char* msg, int msgLen,char msgLocation,int maxDigits);
inline void WriteNum(long num,char location,int maxDigits);
void WriteTare(char num);
#endif /* INCLUDES_LCD_H_ */
here's the azt.h:
#ifndef INCLUDES_AUTOZEROTRACKING_H_
#define INCLUDES_AUTOZEROTRACKING_H_
#include "Declarations.h"
#include "LCD.h"
#include "Utilities.h"
void AZTSetup();
void AZTProcess();
void DisplayAZT();
void GetPreviousAZT();
void GetNextAZT();
void KeyPressedAZTMode(char keyPressed);
inline char HasAZTStopped();
#endif /* INCLUDES_AUTOZEROTRACKING_H_ */
the code in azt.c
void DisplayAZT()
{
WriteMsg(AZT[m_curr_azt],AZTLen[m_curr_azt],0x8A,5);
}
the call in lcd.c
void WriteWeight()
{
WriteNum(CountBy[cnt_by_idx],0x80,2);
WriteNum(NOD[n_o_d_idx],0x83,5);
WriteNum(max_weight,0x89,6);
WriteNum(idx_decimal_point,0x90,1);
WriteMsg(AZT[az_tracking_idx],AZTLen[az_tracking_idx],0x9A,3);
WriteNum(BaudRate[baud_rate_idx],0xC0,6);
WriteNum(PP2Z[percent_p2z_idx],0x9E,2);
WriteNum(wt_zero,0x94,10);
WriteNum(wt_slope,0xD4,10);
/*int i = 0;
for(i = 0; i < WEIGHT_ARR_LEN ; i++)
{
if(display_weight[i] != IGNORE_CHAR)
writeData(display_weight[i]);
}*/
}
Thanks in advance.
I removed the inline and it compiles.
I need several global pointers to be shared among a few files - the pointers are essentially arrays of double whose lengths are only determined at runtime.
I include here the pieces of the code that caused the issue. This is not the exact code, but it illustrates all the points precisely:
foo.h
#ifndef FOOH
#define FOOH
/* ------------------
COMMON VARIABLES
---------------------*/
// create_bundles.c
extern double *all_bundle;
/* ------------------
COMMON FUNCTIONS
---------------------*/
// create_bundles.c
void create_bundles(int num_firm);
// memory_allocation.c
void allocate_memory(int num_firm, int num_bundle);
void clean_memory(void);
#endif
create_bundles.c
#include "foo.h"
extern double *all_bundle;
void create_bundles(int num_firm) {
int i;
for (i = 0; i < num_firm; i++) {
all_bundle[i] = 1
}
memory_allocation.c
#include "foo.h"
// create_bundles.c
double *all_bundle = NULL;
void allocate_memory(int num_firm, int num_bundle) {
all_bundle = calloc(num_bundle * num_firm, sizeof(double));
}
void clean_memory(void) {
free(all_bundle);
}
main.c
#include "foo.h"
void main(int num_firm, int num_bundle) {
allocate_memory(num_firm, num_bundle);
create_bundles(num_firm);
clean_memory();
}
What happened is that if I print out all_bundle[i] it'll all be 0, and then it'll give me a segmentation error.
Why the error and how to fix it?
The problem is not in global pointer, but something else. Keep looking for the problem in your common code. I hope you are trying to print contents of all_bundle array before calling clean_memory. I have edited your code a little bit and it works great without any segmentation errors and prints 1.0000. Here it is, take a look:
foo.h:
#ifndef FOOH
#define FOOH
// create_bundles.c
extern double *all_bundle;
// create_bundles.c
void create_bundles(int num_firm);
// memory_allocation.c
void allocate_memory(int num_firm, int num_bundle);
void clean_memory(void);
#endif
memory_allocation.c:
#include <stdlib.h>
#include "foo.h"
double *all_bundle = 0;
void allocate_memory(int num_firm, int num_bundle) {
all_bundle = calloc(num_bundle * num_firm, sizeof(double));
}
void clean_memory(void) {
free(all_bundle);
}
create_bundles.c:
#include "foo.h"
void create_bundles(int num_firm) {
int i;
for (i = 0; i < num_firm; i++) {
all_bundle[i] = 1;
}
}
main.c:
#include <stdio.h>
#include "foo.h"
int main(int argc, char *argv[]) {
allocate_memory(100, 1);
create_bundles(100);
{
int i;
for(i = 0; i < 100; ++i)
printf("%f\n", all_bundle[i]);
}
clean_memory();
return 0;
}
Have a header file to access the memory (i.e. add stuff to it, remove stuff from it, readf bits of it, etc).
Have the corresponding .c (or .cpp if that fancies you) to do the magic. And then use static to define the memory.
This is a simple and easy solution to your problem and also enables you to change the implementation if it is required to do so.