This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C++ global object multiple defined

Hello:
I'm coding in C++ for ARM for the first time, using Keil. I'm working in a project that already exists but I need to make some changes. I cannot change the libraries or the file's structure. The test_1 and test_2 functions use a global object and I cannot change it. Problem is, the code was created to run each test in an independent compilation, but now I need to create a menu for all the tests in a single project.

I'm having problems with the use of a global object. I want to instantiate one object in a .cpp file and use it in another .cpp file (using extern), but linker keep sayng that the object was multiple defined.

Any idea how to solve it?

Thanks.

---My project:

Library File:

// myclass.h:

class myclass{
....

Header for tests 1

// test_1.h
#ifndef TST1_H
#define TST1_H

void test_1(void);

extern myclass mc(0);  // extern declaration

#endif

Tests file 1

//test_1.cpp
#include "my_lib.h"
#include "test_1.h"

myclass mc(0);

void test_1(void){

mc.func1();
mc.func2();
...
}

Header for tests 2

// test_2.h
#ifndef TST2_H
#define TST2_H

void test_2(void);

extern myclass mc(0);

#endif

Tests file 2

//test_2.cpp
#include "my_lib.h"
#include "test_2.h"

void test_2(void){

mc.func1();
mc.func2();
...
}

Main cpp file

// main.cpp
#include "my_lib.h"
#include "test_1.h"
#include "test_2.h"

void main(void){

// menu for tests...
...
case 1: test_1(); break;
case 2: test_2(); break;

}

Parents Reply Children