Results 1 to 11 of 11

Thread: Extended main function

  1. #1
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Extended main function

    Everybody knows the classic main function with 2 arguments, but there is an extended one - with environment list pointer

    Qt Code:
    1. int main(int argc, char* argv[], char** envp) { ... }
    To copy to clipboard, switch view to plain text mode 

    Using this extended format, I got compiler error with something like could not find qMain() - typical error for cases when we forget to include main.cpp file in qt project. So, is there any solution or to use this extended form of main or some way to get a list of environment variables. I know how to get a specific variable with getenv(), but what should I do when I do not know their specific names?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extended main function

    Typically you are only interested in variables with specific names as otherwise they make little sense

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Extended main function

    #include <unistd.h> and use the environ[] array.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Extended main function

    Qt Code:
    1. // app.cpp
    2. #include <QCoreApplication>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[], char ** envp){
    6. QCoreApplication app(argc,argv);
    7. qDebug() << "list0: " << envp[0];
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // app.pro
    2. TEMPLATE = app
    3. CONFIG += console
    4. SOURCES += app.cpp
    To copy to clipboard, switch view to plain text mode 
    Works ok ( prints ALLUSERSPROFILE=C:\Documents and Settings\All Users ).
    Using WindowsXP, Qt 4.5.2, g++ 4.5.2 ( what a coincidence )
    What is your Qt ver., OS and compiler ?

  5. #5
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extended main function

    Quote Originally Posted by stampede View Post
    Works ok
    Yes, it works, but try this

    Qt Code:
    1. // the-simplest-gui-app.cpp
    2. #include <QtGui/QApplication>
    3. #include <QtGui/QMainWindow>
    4. int main(int argc, char *argv[], char** envp)
    5. {
    6. QApplication app(argc, argv);
    7. mw.setWindowTitle(QString(envp[0]));
    8. mw.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by stampede View Post
    What is your Qt ver., OS and compiler ?
    Windows XP / Qt 4.7.1 / g++ 4.4.0

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Extended main function

    Yes, it works, but try this
    Works fine as well ( see attachement ).
    What is the compiler error ?

    -----------
    Having CONFIG += console in .pro file saved me from this error:
    c:\Qt\lib/libqtmain.a(qtmain_win.o):qtmain_win.cpp.text+0x114): undefined reference to `qMain(int, char**)'
    Looks like using the console config prevents from linking to default qtmain lib, which expects exactly two arguments in main function ( I could "fix" this error when modified qtmain_win.cpp and added third parameter for main declarations ). So use CONFIG += console, or solution proposed by SixDegrees ( which will work for Unix as well, I'm not sure about mine )
    Attached Images Attached Images
    Last edited by stampede; 11th March 2011 at 09:29. Reason: updated contents

  7. #7
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extended main function

    Quote Originally Posted by stampede View Post
    console config prevents from
    I confirm that in that way it works,
    but this black screen is not a kind of feature, which let's say politely "improves our customer satisfaction"

    Quote Originally Posted by stampede View Post
    solution proposed by SixDegrees
    ok, but this one for me sounds better

    Quote Originally Posted by stampede View Post
    I could "fix" this error when modified qtmain_win.cpp and added third parameter for main declarations

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Extended main function

    but this black screen
    What black screen ? I was just running my app from console, this is not visible when I start it with "double-click" ( edit: sorry, it is, looks like i was running other version, but you still have the other solution )
    ok, but this one for me sounds better
    no, no, no, please notice the quotation marks around "fix", this was only for testing, do not modify qt code
    --------------------
    second thought: check QProcess::systemEnvironment(), maybe it suits your needs
    Last edited by stampede; 11th March 2011 at 10:16.

  9. #9
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extended main function

    Quote Originally Posted by stampede View Post
    QProcess::systemEnvironment(), maybe it suits your needs
    Huraa! Souluton found!

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QProcess>
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QStringList list = QProcess::systemEnvironment();
    8. mw.setWindowTitle(list.join("&"));
    9. mw.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    stampede, you're awsome! Thank you so much!

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extended main function

    I have never seen any practical application of a three argument main()... There are functions to query for values of particular environment variables and it should be enough for every need (maybe apart from listing ALL environment variables but since QProcess::systemEnvironment() works I assume there is a solution for that as well - like "environ" on Unix).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    4

    Default Re: Extended main function

    Hello, I have a similar problem, but how do I read the system environment of the parent process that invoked the Qt application?

    The reason I need to read the parent process's system environment is that the Qt Application requires root access and so sudo is used which makes the Qt application's environment a different copy. The parent process has the environment variable "GDMSESSION" which is what I want to read as I want to be able to detect the current session's desktop environment from within the program.

    What I have done so far is use getppid(), and read the ppid's associated environ file. Problem is, that when using using a QFile to open the environ file, it cannot be read and parsed. Interestingly, the QFile::size() file specifically mentions that the environ file cannot be read like regular files and requires a read() to populate them. But that call is returning a error value so I am not sure if I am going about this the wrong way.

    Qt Code:
    1. int ppid = getppid();
    2. QFile ppidenv(QString("/proc/%1/environ").arg(ppid));
    3. bool bOpen = ppidenv.open(QIODevice::ReadOnly);
    4. char* str=NULL;
    5. qint64 MAXSIZE= 1e18;
    6. int result_of_read = (int)ppidenv.read(str,MAXSIZE);
    7. QString ppidenvstr(str);
    8. printf("The permission for file are: %x\n",(int)ppidenv.permissions());
    9. printf("Is the file open?: %s\n",bOpen?"true":"false");
    10. printf("file size: %d\n",(int)ppidenv.size());
    11. printf("PARENT PID environ path IS: %s\n\n\n\n\n\n\n\n",ppidenv.fileName().toUtf8().data());
    12. printf("PARENT PID IS: %d\n\n\n\n\n\n\n\n",ppid);
    13. printf("result_of_read: %d\n",result_of_read);
    14. printf("ppidenvstr: %s\n",ppidenvstr.toUtf8().data());
    15. printf("Str: %s\n",str);
    To copy to clipboard, switch view to plain text mode 

    cout:
    The permission for file are: 4400
    Is the file open?: true
    file size: 0
    PARENT PID environ path IS: /proc/21890/environ







    PARENT PID IS: 21890







    result_of_read: -1
    ppidenvstr:
    Str: (null)

Similar Threads

  1. Replies: 2
    Last Post: 15th July 2010, 09:00
  2. Replies: 11
    Last Post: 11th August 2008, 09:14
  3. access main window from function
    By eric in forum Qt Programming
    Replies: 6
    Last Post: 19th January 2008, 21:29
  4. QDialog - Calling Main Window Function
    By Preeteesh in forum Newbie
    Replies: 4
    Last Post: 20th June 2007, 18:41
  5. Replies: 15
    Last Post: 23rd March 2007, 16:16

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
  •  
Qt is a trademark of The Qt Company.