thinkabout Posted June 7, 2013 at 12:44 PM Report #511512 Posted June 7, 2013 at 12:44 PM (edited) Tenho um trabalho com vários ,c, e queria que as variáveis globais fossem conhecidos em todos. Se só trabalhar com um main.c/help.c ele trabalha bem, se começar a por mais help2.c... ele devolve a seguinte mensagem. Error 2 error LNK2005: _func already defined in help.obj Error 3 error LNK1169: one or more multiply defined symbols found vars.h #ifndef EXTERN #define EXTERN extern #endif EXTERN double x; EXTERN int y; main.c #include <stdio.h> #define EXTERN #include "vars.h" int main( int argc, char *argv[] ) { x = 3.14; y = 5; func(); // Call a function in help.c func(); // Call a function in help.c } help.c #include <stdio.h> #include "vars.h" void func ( ) { printf("x = %lf\n", x ); printf("y = %d\n", y ); } help2.c #include <stdio.h> #include "vars.h" void func ( ) { printf("x = %lf\n", x ); printf("y = %d\n", y ); } Edited June 7, 2013 at 12:47 PM by thinkabout
HappyHippyHippo Posted June 7, 2013 at 12:49 PM Report #511514 Posted June 7, 2013 at 12:49 PM 1º - espero que tenhas aprendido a lição de não usar variáveis globais ... 2º - o problema é que tens várias definições da função func, o que não tem nada haver com as variáveis globais IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted June 7, 2013 at 04:31 PM Author Report #511549 Posted June 7, 2013 at 04:31 PM (edited) Humm, nesse exemplo já o meti a bombar, pois aprendem-se com os erros. Contudo se tentar assim ele já não gosta, no outro trabalho tenho as duas inicializadas a 0, mas tirando o =0 o trabalho parece correr sem problemas. #ifndef EXTERN #define EXTERN extern #endif EXTERN double x=5; EXTERN int y=3; Output Error 4 error LNK2005: _x already defined in help.obj C:\Global\global\global\fazcafe.obj global Error 5 error LNK2005: _y already defined in help.obj C:\Global\global\global\fazcafe.obj global Error 6 error LNK2005: _x already defined in help.obj C:\Global\global\global\main.obj global Error 7 error LNK2005: _y already defined in help.obj C:\Global\global\global\main.obj global Error 8 error LNK2005: _x already defined in help.obj C:\Global\global\global\pagaconta.obj global Error 9 error LNK2005: _y already defined in help.obj C:\Global\global\global\pagaconta.obj global Error 10 error LNK1169: one or more multiply defined symbols found C:\Global\global\Debug\global.exe global Assim já gosta. #ifndef EXTERN #define EXTERN extern #endif EXTERN double x; EXTERN int y; Edited June 7, 2013 at 04:57 PM by thinkabout
HappyHippyHippo Posted June 7, 2013 at 05:35 PM Report #511561 Posted June 7, 2013 at 05:35 PM sabes qual é a diferença entre declaração e alusão ? a diferença do teu código está ai. enquanto que no primeiro, estás a declarar o segundo estás a fazer uma alusão. enquanto que no primeiro estás a "criar" o objecto em memória (algo que dará erro porque de cada vez que estás a incluir o ficheiro estás a criar os objectos) no segundo não estás a fazer mais do que "dizer que existe" IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now