0

i have got 2 cpp files & a header file, which i have included in all 2 cpp files. its like this

abc.h
extern uint32_t key;

a.cpp
#include "abc.h"
uint32_t key;
int main
{
.............
}

b.cpp
#include "abc.h"

int main
{
printf("Key: %.8x\n", key);
.............
}

now when i compile a.cpp, there is no error. but when i compile b.cpp it gives error
"undefined reference to `key'". please help me in finding the problem in this code. thnx

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by nbaztec
0

You have to declare key inside b.cpp

b.cpp
#include "a.cpp"
int main
{
extern uint32_t key;
printf("Key: %.8x\n", key);
........

P.S. Always use Code Tags.

Edited by nbaztec: n/a

0

sorry. its not working

It's because you didn't do it correctly:

your middleware abc.h is blatant. I asked you to include extern statement in b.cpp
so a.cpp is

//a.cpp
uint32_t key = 10;

//b.cpp
[B]#include <stdio.h>[/B] //for stdio.h
[B]#include "a.cpp"[/B]
int main
{
extern uint32_t key;
printf("Key: %.8x\n", key);
}

EDIT: Do make sure stdint.h is included other wise instead of uint32_t user the good 'ol unsigned long int

Edited by nbaztec: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.