Results 1 to 6 of 6

Thread: Problems with scope and C header functions

  1. #1
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with scope and C header functions

    The problem is that my instruction in QT 4 was limited mainly to the GUI, which I understand great. But it lacks in practical application. In other words, I'm having trouble getting my GUI code to blend with the libyahoo2 functions. The specific problem I'm getting is during the compile stage and appears to be dealing with scope.

    See, I have a struct that should be global;

    Qt Code:
    1. struct yahoo_local_account {
    2. char yahoo_id[255];
    3. char password[255];
    4. int id;
    5. int fd;
    6. int status;
    7. char *msg;
    8. };
    9.  
    10. static yahoo_local_account * ylad = NULL;
    To copy to clipboard, switch view to plain text mode 
    And then I have a member function, which should have access to the global struct, right?;

    Qt Code:
    1. void YahooLogin::okClicked()
    2. {
    3.  
    4. // accept QString input
    5. QString username = lineEditUsername->text();
    6. QString password = lineEditPassword->text();
    7.  
    8. // convert QString to string
    9. std::string usrnm = username.toStdString();
    10. std::string psswrd = password.toStdString();
    11.  
    12. // convert string to const char and assign struct values
    13. std::strcpy(ylad->yahoo_id, usrnm.c_str());
    14. std::strcpy(ylad->password, usrnm.c_str());
    15.  
    16. //get the session id from yahoo_init, which is a libyahoo2 function from <libyahoo2/yahoo2.h>
    17. ylad->id = yahoo_init(ylad->yahoo_id, ylad->password);
    18. }
    To copy to clipboard, switch view to plain text mode 

    This provides me with 2 separate compiler problems, 1st is that ylad is not declared in this scope, and 2nd is the reference to yahoo_init is undefined.

    Now, obviously I am missing something. It was my understanding that if the struct is placed before the main() that it would be global, meaning that it would be accessible from every piece of code after it. But in order to remedy the 1st compile error, I have to move the struct (which I had located in its own file yahoo2.cpp, which was in-turn #included in my main.cpp) directly into the yahooLogin.cpp file above the member function, and ylad's declaration actually in the okClicked() member function.

    That still leaves 2 problems, 1. is that now the struct is definitely not global and therefore useless beyond the okClicked member function, and 2. is that I still get the compile error with the call to yahoo_init().

    Errors:

    1st:
    yahoologin.cpp: In member function ‘void YahooLogin::okClicked()’:
    yahoologin.cpp:27: error: ‘ylad’ was not declared in this scope
    yahoologin.cpp:30: error: ‘yahoo_init’ was not declared in this scope
    2nd:
    yahoologin.o: In function `YahooLogin::okClicked()':
    yahoologin.cpp:(.text+0x12b): undefined reference to `yahoo_init'
    collect2: ld returned 1 exit status
    Qt:

    $ qmake --version
    QMake version 2.01a
    Using Qt version 4.2.1 in /usr/lib
    Make:

    $ make --version
    GNU Make 3.81
    Compiler:

    $ g++ --version
    g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

  2. #2
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with scope and C header functions

    Regarding yahoo_init(): did you include the libyahoo2 header files?

    Regarding the global ylad: in the .cpp files where you want to use ylad, you need notify the compile of the existence of ylad:
    Qt Code:
    1. extern struct yahoo_local_account * ylad;
    To copy to clipboard, switch view to plain text mode 
    The compile now knows that ylad is defined elsewhere.

    (but I think you should try and avoid global variables...)

    PS: I think this should be in General Programming

  3. The following user says thank you to lvi for this useful post:

    waldowoc (4th August 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with scope and C header functions

    PS: I think this should be in General Programming
    Yeah, I am a newbie to QT and I was thinking that the problem(s) I am having was due to some different way QT handles header files. But, I have also never had a practical use for C++ either, though I have read every resource I could get my hands on.

    Does qt handle header files differently than C++ alone? I still should be able to access any function (or object in general) if it's header file is included at any point before the call, right?

    Like, yahoo_init(), which is setup in the libyahoo2 header files...

  5. #4
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with scope and C header functions

    Quote Originally Posted by waldowoc View Post
    Does qt handle header files differently than C++ alone? I still should be able to access any function (or object in general) if it's header file is included at any point before the call, right?

    Like, yahoo_init(), which is setup in the libyahoo2 header files...
    Yes, as long as you include the header files AND link the corresponding library files.
    Now that I look back at your post:
    Qt Code:
    1. yahoologin.cpp: In member function &#8216;void YahooLogin::okClicked()’:
    2. yahoologin.cpp:27: error: &#8216;ylad’ was not declared in this scope
    3. yahoologin.cpp:30: error: &#8216;yahoo_init’ was not declared in this scope
    To copy to clipboard, switch view to plain text mode 
    This error message tells that that probably you forgot to include the proper header files. The error in yahoologin.cpp:27 was probably fixed by declaring ylad as extern, as I mentioned before. The error in yahoologin.cpp:30 should be fixed by include the proper library header file.

    Qt Code:
    1. yahoologin.o: In function `YahooLogin::okClicked()':
    2. yahoologin.cpp:(.text+0x12b): undefined reference to `yahoo_init'
    3. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 
    This is a linker error. You should add the yahoo2 library to your .pro file to make sure it gets linked.
    Last edited by lvi; 4th August 2008 at 20:26. Reason: reformatted to look better

  6. The following user says thank you to lvi for this useful post:

    waldowoc (5th August 2008)

  7. #5
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with scope and C header functions

    Thank you very much for your assistance.

    Unfortunately, using extern only made things worse, lots of errors during compile time. :-(

    About the header files, you came to the same conclusion I did, but, the header files have always been included in the code. When I first started getting the problem, I moved the libyahoo2 header file includes around to different .cpp files. I want it in the .cpp file with all the structs which is included in the main.cpp (because that makes more sense to me since it needs to be accessed several times throughout the program not just in yahooLogin()). I also tried including them in the main .cpp file, the mainwindow.cpp file and in the yahoologin.cpp file with no changes to the error. I tried putting them in ALL the files at the same time too. I even tried to #include them inside the member function, but that didn't work either.

    Now, about this adding the library to the .pro file, is there a command line option for that? Or should I just edit it in an editor?

    That would definitely prove it's a QT thing, especially since I can compile other programs (non QT) with the yahoo library just by including the libyahoo2 header files. The library is installed properly in the path.
    Last edited by waldowoc; 5th August 2008 at 07:48. Reason: reformatted to look better

  8. #6
    Join Date
    Jul 2008
    Location
    Netherlands
    Posts
    33
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with scope and C header functions

    What sort of error do you get using extern?

    You should use it like this (simplified example):

    main.cpp:
    Qt Code:
    1. #include "somefile.h"
    2.  
    3. Yahoo_t* ylad;
    4.  
    5. int main(int argc, char* argv) {
    6. /* do things */
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    somefile.cpp
    Qt Code:
    1. extern Yahoo_t* ylad;
    2.  
    3. /* other code */
    To copy to clipboard, switch view to plain text mode 

    So use extern in all but one cases to declare the variable globally.

    ---------------
    I'm not sure what causes the problems with the header files. If you included the libyahoo2 headers everywhere, I can't see why it wouldn't be able to find yahoo_init().

    You can add the lib to your .pro file or just add it in your Makefile. I'm not sure if there's a commandline option you can pass to qmake, maybe you can do something with the -after flag? I think adding the lib to LIBS with an editor is going to be the easiest option...

Similar Threads

  1. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 10:15
  2. Just for fun game
    By vermarajeev in forum Qt-based Software
    Replies: 6
    Last Post: 13th December 2007, 21:52
  3. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  4. Problems
    By euthymos in forum Installation and Deployment
    Replies: 2
    Last Post: 13th June 2006, 19:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.