PDA

View Full Version : Extended main function



daorus
11th March 2011, 08:27
Everybody knows the classic main function with 2 arguments, but there is an extended one - with environment list pointer


int main(int argc, char* argv[], char** envp) { ... }

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?

squidge
11th March 2011, 08:51
Typically you are only interested in variables with specific names as otherwise they make little sense

SixDegrees
11th March 2011, 08:53
#include <unistd.h> and use the environ[] array.

stampede
11th March 2011, 08:58
// app.cpp
#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[], char ** envp){
QCoreApplication app(argc,argv);
qDebug() << "list0: " << envp[0];
return 0;
}


// app.pro
TEMPLATE = app
CONFIG += console
SOURCES += app.cpp

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 ?

daorus
11th March 2011, 09:29
Works ok
Yes, it works, but try this



// the-simplest-gui-app.cpp
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
int main(int argc, char *argv[], char** envp)
{
QApplication app(argc, argv);
QMainWindow mw;
mw.setWindowTitle(QString(envp[0]));
mw.show();
return app.exec();
}




What is your Qt ver., OS and compiler ?

Windows XP / Qt 4.7.1 / g++ 4.4.0

stampede
11th March 2011, 09:45
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+0x 114): 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 )

daorus
11th March 2011, 10:54
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" :)


solution proposed by SixDegrees

ok, but this one for me sounds better


I could "fix" this error when modified qtmain_win.cpp and added third parameter for main declarations

stampede
11th March 2011, 11:02
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() (http://doc.qt.nokia.com/latest/qprocess.html#systemEnvironment), maybe it suits your needs

daorus
11th March 2011, 12:44
QProcess::systemEnvironment(), maybe it suits your needs

Huraa! Souluton found!


#include <QApplication>
#include <QMainWindow>
#include <QProcess>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow mw;
QStringList list = QProcess::systemEnvironment();
mw.setWindowTitle(list.join("&"));
mw.show();
return app.exec();
}


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

wysota
11th March 2011, 13:03
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).

positive_4_life
26th October 2011, 20:00
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() (http://doc.qt.nokia.com/latest/qfile.html#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.



int ppid = getppid();
QFile ppidenv(QString("/proc/%1/environ").arg(ppid));
bool bOpen = ppidenv.open(QIODevice::ReadOnly);
char* str=NULL;
qint64 MAXSIZE= 1e18;
int result_of_read = (int)ppidenv.read(str,MAXSIZE);
QString ppidenvstr(str);
printf("The permission for file are: %x\n",(int)ppidenv.permissions());
printf("Is the file open?: %s\n",bOpen?"true":"false");
printf("file size: %d\n",(int)ppidenv.size());
printf("PARENT PID environ path IS: %s\n\n\n\n\n\n\n\n",ppidenv.fileName().toUtf8().data());
printf("PARENT PID IS: %d\n\n\n\n\n\n\n\n",ppid);
printf("result_of_read: %d\n",result_of_read);
printf("ppidenvstr: %s\n",ppidenvstr.toUtf8().data());
printf("Str: %s\n",str);


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)