Headery - multiple definitions
Ostatnio zmodyfikowano 2015-07-04 14:13
Grzyboo Temat założony przez niniejszego użytkownika |
Headery - multiple definitions » 2015-07-02 00:08:25 Witam, mam problemy przy kompilacji projektu z plikiem nagłówkowym. Co tutaj jest nie tak? main.cpp #include "myfunc.h"
int main() { setMessage(); std::cout << message << std::endl; return 0; }
myfunc.h #ifndef MYFUNC_H_INCLUDED #define MYFUNC_H_INCLUDED
#include <iostream> #include <string>
int a; int b;
std::string message;
void setMessage();
#endif
myfunc.cpp #include "myfunc.h"
void setMessage() { message = "Hello world!"; }
Logi: -------------- Build: Debug in HEADERY (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -o bin\Debug\HEADERY.exe obj\Debug\main.o obj\Debug\myfunc.o obj\Debug\myfunc.o: In function `Z10setMessagev': F:/Program Files/C++ projects/HEADERY/myfunc.cpp:4: multiple definition of `a' obj\Debug\main.o:F:/Program Files/C++ projects/HEADERY/main.cpp:4: first defined here obj\Debug\myfunc.o: In function `Z10setMessagev': F:/Program Files/C++ projects/HEADERY/myfunc.cpp:4: multiple definition of `b' obj\Debug\main.o:F:/Program Files/C++ projects/HEADERY/main.cpp:4: first defined here obj\Debug\myfunc.o: In function `Z10setMessagev': F:/Program Files/C++ projects/HEADERY/myfunc.cpp:5: multiple definition of `message' obj\Debug\main.o:F:/Program Files/C++ projects/HEADERY/main.cpp:4: first defined here collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 0 second(s)) 6 error(s), 0 warning(s) (0 minute(s), 0 second(s))
|
|
notabigthreat |
» 2015-07-02 00:23:55 W nagłówku: extern int a; extern int b; extern std::string message;
a w pliku źródłowym: int a; int b; string message;
dają taki rezultat, jakiego pewnie chciałeś (inaczej na każde dołączenie pliku *.h tworzone byłyby te zmienne, #include działa jak Ctrl+C, Ctrl+V). |
|
Grzyboo Temat założony przez niniejszego użytkownika |
» 2015-07-02 00:35:50 Dziękuję za odpowiedź, jakoś w poradnikach na temat nagłówków nic nie jest wytłumaczone. |
|
Roofy5 |
» 2015-07-03 23:53:19 A tak przy okazji tego ctrl+c i v - jak to właściwie działa - który element jest gdzie kopiowany? Skoro mamy includa w cpp z funkcja to tam jest wklejany nagłówek h Aale potem ten nagłówek h wklejamy w cpp main - i skąd on ma wiedzieć gdzie są definicje jego funkcji(tego naglowka)? |
|
pekfos |
» 2015-07-04 14:13:08 i skąd on ma wiedzieć gdzie są definicje jego funkcji(tego naglowka)? |
Po co ktokolwiek miałby wiedzieć, gdzie jest definicja jakiejś funkcji? |
|
« 1 » |