Jump to content

Recommended Posts

Hi,

 

I want to declare variables in a headerfile for some reason:

 

//
// file test.h
//

#ifndef test_INCLUDED
#define test_INCLUDED

#include <system.h>

unsigned char var_a, var_b;

#endif

 

This headerfile is used in two source-files in a project.

 

It compiles fine with C2C but BoostC gives an error:

BoostLink Optimizing Linker Version 1.9 Beta

http://www.picant.com/c2c/c.html

Copyright© 2004-2005 Pavel Baranov

Copyright© 2004-2005 David Hobday

Warning: argument of 'delay_us' calls must have a value of 1 or more

Error: Duplicate global var:var_a

Error: Duplicate global var:var_b

 

What am I doing wrong? Target is a pic18f4620.

Pls. no solutions like "place your var's outside the headerfile"...

 

Tnx,

 

Teuniz

Link to post
Share on other sites

Well, actually Pavel you CAN do this in 'C', as long as you properly wrap it in:

#ifndef xxx

#define xxx

 

.. do stuff ..

int MyGlobal;

 

#endif

 

This should define MyGlobal as an int only ONCE, no? If anyone else tries to #include the above file, 'xxx' will have been defined, and so the re-definition should be skippped, no?

Link to post
Share on other sites
Well, actually Pavel you CAN do this in 'C', as long as you properly wrap it in:

#ifndef xxx

#define xxx

 

  .. do stuff ..

  int MyGlobal;

 

#endif

 

This should define MyGlobal as an int only ONCE, no?  If anyone else tries to #include the above file, 'xxx' will have been defined, and so the re-definition should be skippped, no?

 

No one still can't do it even with these defines around. The problem is that even with the defines the variable is still processed and added to the variables list of every source file that includes this header. Remember that compiler doesn't store information between compilation of different files so these defines won't help.

 

Regards,

Pavel

Link to post
Share on other sites
No one still can't do it even with these defines around. The problem is that even with the defines the variable is still processed and added to the variables list of every source file that includes this header. Remember that compiler doesn't store information between compilation of different files so these defines won't help.

???

 

The gcc-compiler can, I tried it on a Linuxbox, no problem at all.

Link to post
Share on other sites
Well, actually Pavel you CAN do this in 'C', as long as you properly wrap it in:

#ifndef xxx

#define xxx

 

  .. do stuff ..

  int MyGlobal;

 

#endif

 

This should define MyGlobal as an int only ONCE, no?  If anyone else tries to #include the above file, 'xxx' will have been defined, and so the re-definition should be skippped, no?

 

No one still can't do it even with these defines around. The problem is that even with the defines the variable is still processed and added to the variables list of every source file that includes this header. Remember that compiler doesn't store information between compilation of different files so these defines won't help.

 

Regards,

Pavel

 

The thing to do here is:

header.h =============

#ifdef DECLARE_MY_VARS
   int myglobal;
#else
   extern int myglobal;
#endif
....

main.c ==============

#define DECLARE_MY_VARS
#include "header.h"

void main()
{
   myglobal = 0;
   ....
}

other.c ==============

#include "header.h"
void foo()
{
   myglobal++;
}

 

 

Hope this helps.

 

Regards

Dave

Link to post
Share on other sites
  • 2 years later...
...

 

The thing to do here is:

header.h =============

#ifdef DECLARE_MY_VARS
   int myglobal;
#else
   extern int myglobal;
#endif
....

main.c ==============

#define DECLARE_MY_VARS
#include "header.h"

void main()
{
   myglobal = 0;
   ....
}

other.c ==============

#include "header.h"
void foo()
{
   myglobal++;
}

 

 

Hope this helps.

 

Regards

Dave

 

 

Dave,

 

I have some 30 global variables. Do I really have to write them twice inside the header file?

Link to post
Share on other sites
No one still can't do it even with these defines around. The problem is that even with the defines the variable is still processed and added to the variables list of every source file that includes this header. Remember that compiler doesn't store information between compilation of different files so these defines won't help.

???

 

The gcc-compiler can, I tried it on a Linuxbox, no problem at all.

 

I don't believe gcc can :rolleyes: Can you post what you tried?

 

Regards,

Pavel

Link to post
Share on other sites
...

 

The thing to do here is:

header.h =============

#ifdef DECLARE_MY_VARS
   int myglobal;
#else
   extern int myglobal;
#endif
....

main.c ==============

#define DECLARE_MY_VARS
#include "header.h"

void main()
{
   myglobal = 0;
   ....
}

other.c ==============

#include "header.h"
void foo()
{
   myglobal++;
}

 

 

Hope this helps.

 

Regards

Dave

 

 

Dave,

 

I have some 30 global variables. Do I really have to write them twice inside the header file?

 

No you don't. Something like this will work:

 

#ifndef DECLARE_MY_VARS
 extern
#endif
  int myglobal1, myglobal2, myglobal3;

 

Regards,

Pavel

Link to post
Share on other sites
I don't believe gcc can :) Can you post what you tried?

 

 

I would not expect it to work.

 

However I decided to see what another C compiler would do, I dont have gcc so I tried it with Borland CBuilder, it did work however the linker issued a warning regarding multiple declarations.

Link to post
Share on other sites

Strange there is so much about this very common task ...

Dave's method does work but it invites cyclic includes which ends up

chasing multiple defines, the simple solution is:

 

*** export.h ***
extern int expose;

*** export.c ****
int expose = 123;


**** main.c
include expose.h

void main(void)
{
  expose =42;
  while(1);
}

 

This avoides the cyclic include issue and exposes variables that you choose to expose as global.

As the variable is only defined in the C file and exposed in the H file there are no multiple defines.

 

Hope that helps.

Link to post
Share on other sites
Strange there is so much about this very common task ...

Dave's method does work but it invites cyclic includes which ends up

chasing multiple defines, the simple solution is:

 

*** export.h ***
extern int expose;

*** export.c ****
int expose = 123;


**** main.c
include expose.h

void main(void)
{
  expose =42;
  while(1);
}

 

This avoids the cyclic include issue and exposes variables that you choose to expose as global.

As the variable is only defined in the C file and exposed in the H file there are no multiple defines.

 

Hope that helps.

 

Emte,

 

If I understand correctly, you propose to add a .c file where all globals will be initiated.

Is it better than what Pavel proposed?

Link to post
Share on other sites

OrmatEli,

If I understand correctly, you propose to add a .c file where all globals will be initiated.

Is it better than what Pavel proposed?

Better is a matter of opinion. Depends on the exact application.

Declaring the globals in a .c file (rather than a .h) is standard practice.

 

Regards

Dave

Link to post
Share on other sites
OrmatEli,
If I understand correctly, you propose to add a .c file where all globals will be initiated.

Is it better than what Pavel proposed?

Better is a matter of opinion. Depends on the exact application.

Declaring the globals in a .c file (rather than a .h) is standard practice.

 

Regards

Dave

 

I've just tried what Pavel proposed, it didn't compile. Showed error on "intern" line after the #infndef

Link to post
Share on other sites
OrmatEli,
If I understand correctly, you propose to add a .c file where all globals will be initiated.

Is it better than what Pavel proposed?

Better is a matter of opinion. Depends on the exact application.

Declaring the globals in a .c file (rather than a .h) is standard practice.

 

Regards

Dave

 

I've just tried what Pavel proposed, it didn't compile. Showed error on "intern" line after the #infndef

 

Not sure what you tried but I never suggested to use neither "intern" nor "#infndef" :(

 

Regards,

Pavel

Link to post
Share on other sites

The funny thing is that if you open a new project in BoostC, using an automatic header file creation you get:

#ifndef _RUNTRIAL_H_
#define _RUNTRIAL_H_


#endif //_RUNTRIAL_H_

 

source fle is:

#include <system.h>
#include "RunTrial.h"

void main()
{

}

 

According to this thread this template will not work :( !

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...