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

Recommended Answers

All 3 Replies

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.

sorry. its not working

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.