21 September 2021, 09:00 | #1 |
Registered User
Join Date: Apr 2021
Location: Italy
Posts: 21
|
Global struct problems with VBCC
Hello all,
I'm facing a weird problem with vbcc. I want to have a global struct on my project. I extracted the problem here. struct.h
________________________________________ struct.c
________________________________________ main.c
________________________________________ Makefile
________________________________________ Everything compiles and run with gcc on linux, but when I try to compile this with vbcc targeting amiga this error happens: Error 19: /var/tmp/tmp.1.o: Global symbol _myStruct from tmp.1.o is already defined in tmp.0.o. I cannot understand why! Have any idea? Thank you in advance. |
21 September 2021, 09:06 | #2 |
68k
Join Date: Sep 2005
Location: Somewhere
Posts: 828
|
@Csoft
Change struct.h to Code:
#ifndef PROJECT_STRUCT_H #define PROJECT_STRUCT_H struct { int a; int b; } myStruct; void change(); #endif /*PROJECT_STRUCT_H*/ |
21 September 2021, 09:12 | #3 |
Registered User
Join Date: May 2021
Location: Melbourne, Australia
Posts: 40
|
You are actually creating a myStruct variable in the header file which then is included in both c files. This causes your error.
Define in the .h file: struct myStruct { int a; int b; }; If you want it to be global, in ONE of your .c files add: struct myStruct myStruct; In the other .c file, add: extern struct myStruct myStruct; It would be better not to use a global across .c files if you can help it, and just pass a pointer to myStruct in to the called functions as needed. Also, it would be better to use a different name from the actual structure type. (i.e. don't call both the structure and variable myStruct). |
21 September 2021, 09:54 | #4 |
Registered User
Join Date: Apr 2021
Location: Italy
Posts: 21
|
Thank you guys!!! I solved!!!
Yes, I know that global variables are usually considered bad, but in my scenario there's a reason, I can always change my mind. Thanks again! |
21 September 2021, 12:29 | #5 |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
|
Steady is right. vbccm68k behaves like all classic Amiga compilers here, because Common symbols were never really supported in hunk-format object files (partly starting with V36?). So gcc is (as usual) special here, because it uses Common symbols for mystruct.
A Common symbol is allowed to be defined in multiple objects with the same name, and the linker will merge them into a common address space, using the size of the largest definition. You can get the same with vbcc when using the option -use-commons. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
InputEvent struct destroyed | meynaf | Coders. System | 4 | 14 December 2018 10:13 |
compile problems (vbcc) | meynaf | Coders. C/C++ | 20 | 22 May 2017 19:08 |
Cannot get WBStartup struct when starting from Workbench (with VBCC) | tygre | Coders. C/C++ | 10 | 08 December 2015 23:30 |
Compile problems with VBCC | Yesideez | Coders. C/C++ | 9 | 18 October 2014 23:38 |
struct Image | mritter0 | Coders. C/C++ | 5 | 05 June 2014 02:36 |
|
|