Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Linker error - multiple definition

[es] :: C/C++ programiranje :: C/C++ za početnike :: Linker error - multiple definition

[ Pregleda: 1286 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

kish
NS

Član broj: 91320
Poruke: 44
*.dynamic.sbb.rs.

ICQ: 204


Profil

icon Linker error - multiple definition20.02.2009. u 14:00 - pre 184 meseci
Dakle, projekat ima sledecu strukturu:
main.cpp:
Code:

#include <iostream>
#include "test1.h"
#include "test2.h"
using namespace std;

int main()
{
    set();
    cout << "testint = " << testint << endl;
    return 0;
}


test1.h:
Code:

#ifndef TEST1_H
#define TEST1_h

void set();

#endif


test1.cpp:
Code:

#include "test1.h"
#include "test2.h"

void set() {
    testint=1;
}


test2.h:
Code:

#ifndef TEST2_H
#define TEST2_H

int testint;

#endif


Pri kompajliranju dobijam sledecu gresku:
Code:

Compiling: main.cpp
Linking console executable: C:\Trash\coding\c++\htest\console.exe
.objs\main.o:main.cpp:(.bss+0x0): multiple definition of `testint'
.objs\test1.o:test1.cpp:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 2 seconds)


Izgleda da kompajler pokusava dva puta da definise promenjivu testint, medjutim TEST2_H bi bas to trebao da spreci. U cemu je tacno problem? Unapred hvala.
EDIT: Sad vidim da ima i poseban podforum za c++. Nek se tema prebaci na svoje mesto. Sorry.

[Ovu poruku je menjao kish dana 20.02.2009. u 15:17 GMT+1]
 
Odgovor na temu

Mihajlo Cvetanović
Beograd

Moderator
Član broj: 37636
Poruke: 1249



+96 Profil

icon Re: Linker error - multiple definition20.02.2009. u 23:05 - pre 184 meseci
Stavi "int testint;" u cpp, a "extern int testint;" u heder.
 
Odgovor na temu

kish
NS

Član broj: 91320
Poruke: 44
*.dynamic.sbb.rs.

ICQ: 204


Profil

icon Re: Linker error - multiple definition21.02.2009. u 15:10 - pre 184 meseci
Radi. Zahvaljujem.
 
Odgovor na temu

[es] :: C/C++ programiranje :: C/C++ za početnike :: Linker error - multiple definition

[ Pregleda: 1286 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.