English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 21 September 2021, 09:00   #1
Csoft
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 {
int a;
int b;
} myStruct;

void change();

________________________________________

struct.c


#include "struct.h"

void change() {
myStruct.a = 40;
}

________________________________________

main.c


#include <stdio.h>
#include "struct.h"

int main () {
myStruct.a = 10;
change();
printf ("%d\n",myStruct.a);
}

________________________________________

Makefile


gcc:
gcc struct.c main.c -o main.exe

vbcc:
vc +kick13 struct.c main.c -o amigamain.exe

________________________________________


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.
Csoft is offline  
Old 21 September 2021, 09:06   #2
Asman
68k
 
Asman's Avatar
 
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*/
Asman is offline  
Old 21 September 2021, 09:12   #3
Steady
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).
Steady is offline  
Old 21 September 2021, 09:54   #4
Csoft
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!
Csoft is offline  
Old 21 September 2021, 12:29   #5
phx
Natteravn
 
phx's Avatar
 
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.
phx is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 20:16.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.07113 seconds with 15 queries