Mix C and assembly

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Mix C and assembly

3,653 Views
admin
Specialist II
My problem is how to define a variable that can be use in multi C files and assembly files at a application.
 
I define a variable in the first C file, for example:
/* file1.c*/
int i;
 
now I want this variable can be used in another C file and a asm file, for example:
/* main.c*/
#include "file1.c"
main()
{
  i=0;
}
 
/*main.asm*/
XREF i;
...
LDD #0
STD i
 
but it can not be link correctly withing CW for HCS12 v3.1.
 
Please help me!
 
Labels (1)
Tags (1)
0 Kudos
10 Replies

779 Views
Lundin
Senior Contributor IV
The linker error is likely there because you are including a c-file. Never do that, you should only include h-files. This is how it is done properly:

/* file.h */
extern int i;


/* file.c */
#include "file.h"
int i;


/* main.c */
#include "file.h"

int main()
{
i=0;
}


/* file.asm */

XREF i
0 Kudos

779 Views
CrasyCat
Specialist III

Hello

What is the message the linker generates?

CrasyCat

0 Kudos

779 Views
admin
Specialist II

Yeah, I placed the variable "i" in the .h file.

First, I do not use "extern", and the linker note "Link Error : L1818: Symbol - i duplicated in file1.c.o and main.c.o".

Then I added "extern", linker show "Link Error: L1822: Symbol i in file ...file.asm.o is undifined".

0 Kudos

779 Views
Lundin
Senior Contributor IV
You didn't do as shown in my example then. Did you add it to the .c file?


If you declare a variable as

int i;

as shown in my example it -will- be global and every other file in your project can access it. Unless you use the "static" keyword. This is very basic C knowledge.

And XREF works just fine - I'm also using CW3.1 for HCS12.
0 Kudos

779 Views
admin
Specialist II

I notice the different between your sample and mine:

Declare "i" in the h file as "extern", and be included in the two c file, alse be included in the asm file.

I notice that in the first c file you re-declare the variable "i", then everythink ok!

I just wondered whether the two "i" (file.h and file.c) share the same RAM space?

Could you explare it for me?

Thank you!

0 Kudos

779 Views
Lundin
Senior Contributor IV
When you type extern in front of the variable, you aren't declaring the actual variable. You just tell the compiler that we have a variable called 'i' but it is allocated elsewhere. By doing so, every c-file that includes the h-file gets the info that 'i' exists.

Note however that you should place the "extern" in the same memory segment as the variable:

/* file.h */

#pragma DATA_SEG MY_RAM
extern int i;


/* file.c */

#pragma DATA_SEG MY_RAM
int i;
0 Kudos

779 Views
admin
Specialist II

I got it!

Thank you again!

0 Kudos

779 Views
admin
Specialist II
Thanks a lot!
 
I am now declare i in a *.h file:
/* file.h */
int i;
 
and be included and used in file.c, main.c and file.asm:
/* file.c */
#include "file.h"
void file()
{
  i = 0;
}
 
/* main.c */
#include "file.h"
void main()
{
  i = 1;
}
 
/* file.asm */
  XREF i
  LDD #0
  STD i
 
but if I declare "i" as a global variable in the h file, linker show "i" be duplicated by the two c files;
if i declare as extern or static, linker show "i" undefined in asm file.
0 Kudos

779 Views
rocco
Senior Contributor II
Hi, Markie:

Since I don't use CodeWarrior, I can't test this, but it looks like a problem of scope.

In "file1.c", it looks like the variable "i" has file-scope. It does not exist outside of the file "file1.c".

Try declaring it as "global".

Message Edited by rocco on 2006-08-14 02:32 PM

0 Kudos

779 Views
admin
Specialist II
It's seemed that "global" not be supported by CW.
0 Kudos