This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between _tmain() and main() in C++?
what is the difference between int _tmain(int argc, _TCHAR* argv[]) and int main(int argc, char** argv)?
I am not clear of the difference.
_tmain is the Microsoft-specific wrapper around "main()". You can use it with either 8-bit ASCII or 16-bit Unicode. Here's the MS documentation for it:
http://msdn.microsoft.com/en-us/library/6wd819wh%28v=vs.80%29.aspx
You can also use _tmain, which is defined in TCHAR.h. _tmain will
resolve to main unless _UNICODE is defined, in which case _tmain will
resolve to wmain.
_tmain is the unicode version of main. I think this is a MS only extension though.
Related
Is using the main function in the form:
int main(int argc, const char* argv[])
non-portable and non-standard? The C++ standard says that argv is "pointer to pointer to char". I prefer to use the word const to prevent modification and out-of-bounds in an unknown (to me) area of memory.
Is such a form wrong and non-portable?
Your version is indeed unportable.
The variants that the standard allows are
int main()
int main(int argc, char* argv[])
int main(int argc, char** argv)
The standard allows implementation-defined possibilities, so const might be permitted on some platforms. But if the implementation doesn't define such an alternative then the behaviour of your program is undefined.
I am trying to save argv into a vector as strings but I keep getting the error: see reference to function template instantiation 'std::vector<_Ty>::vector<_TCHAR*[]>(_Iter,_Iter)' being compiled
I have tried Save argv to vector or string and it does not work
I am using Microsoft Visual Studio 2010.
Here is my code:
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::string> allArgs(argv + 1, argv + argc);
return 0;
}
The problem is that std::string doesn't have a constructor for the _TCHAR* type, so you can't generate a vector of strings from an array of _TCHAR*.
Try using as said by #NathanOliver the "normal" version of the main: int main(int argc, char *argv[]).
Or switch to std::wstring.
Note: If not compiling with unicode enabled, _TCHAR* may then be equivalent to char * and the code would not provoke any compiler errors.
Use this:
typedef std::basic_string<TCHAR> tstring;
// Or:
// using tstring = std::basic_string<TCHAR>;
// If you have latest compiler
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<tstring> allArgs(argv + 1, argv + argc);
return 0;
}
And if you would always want to use wide-strings, use this
int wmain(int argc, whar_t* argv[])
{
std::vector<std::wstring> allArgs(argv + 1, argv + argc);
return 0;
}
Or, if you want to have ANSI only (I'd not recommend), just use old style char and main.
So I am starting with c++ (i am trying to broaden my mind with new languages) but I came across a little issue which confuses me more than what I guess it should...
Using Visual Studio Express 2012, I created a console win32 application in C++ and this is my main method decleration:
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
however, since I dont know anything about c++, I searched for some tuts online and all of them had there declerations setup in a different manner
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
}
and
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I tried typing in the "std::cout", but it wouldnt accept it,
could someone just clarify why and the significance of the difference ?
The main method can be define with or without parameters. It all depends upon what you are using your application for.
Take a look at this: https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fmainf.htm
Also for your program you need to have a return value
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
is (at least I think so) a Windows only library and compiler depending way of declaring the main function.
Definitly not wrong is to declare main like this:
int main(int argc, char const *argv[])
{
//do something
return 0;
}
or like this:
int main()
{
//do something
return 0;
}
This is definitly proper C++ and you can use this universially.
C++ programs may have one of two beginnings:
int main(int argc, char *argv[])
or
int wmain(int argc, wchar_t *argv[])
The first of these gets its arguments (argv) as ANSI charachers, while the second gets "wide" characters -- generally UTF-16 or UTF-32, depending on the platform.
Microsoft define a framework to allow you make code that can compile with either ANSI or wide characters.
int _tmain(int argc, TCHAR *argv[])
Behind the scenes, they have something like this:
#if defined UNICODE
#define _tmain wmain
#define TCHAR wchar_t
#else
#define _tmain main
#define TCHAR char
#endif
They also have helper functions like _tprintf() and _tcscpy().
NOTE: as pointed out by others, the argc and argv params are optional, so you can also have
int main()
and
int wmain()
and (for Microsoft and compatible compilers)
int _tmain()
Also note that while _tmain() is not strictly portable, you can easily create your own #define macros if you want to be portable to other platforms.
g++ -pthread threads.cpp I'm compiling my program with that command and I don't know how should I specify some arguments given from the keyboard(file names).
int main(int argc,const char argv[]) is the head of my main and f1.txt and f2.txt are the files I want to pass.
./a.out f1.txt f2.txt is what I've typed and the answer was a screen full of unreadable characters.How should I do this?
int main(int argc,const char argv[]) is not correct, this int main(int argc, char** argv) is correct.
Then argv[1] and argv[2] will be your file names.
You should have got a warning from the g++, regarding the second argument in main.
So getting into the new millenia I rewrote my c++ code with:
int main(int argc, wchar_t **argv)
If built with either Unicode or MBCS options then when the app is run with a commandline arg, either directly or by dbl-click the filenames passed to argv[] are unreadable = in some mixture of chinese fonts.
Thanks for the comments - I will try and summaris(z)e them here for the search engine.
wmain(int argc,char **argv) can only be used for a commandline (subsystem:console) app
int winMain(int argc, wchar_t **argv) works for a gui (subsystem:windows) but the gui replaces it with it's own entry point. In the case of Qt this doesn't work
qtmaind.lib(qtmain_win.obj) : error LNK2019: unresolved external symbol _main referenced in function _WinMain#16
The solution seems to be use main(int arc,char **argv)
or main(int argc,wchar_t**argv) but ignore the argv. Then call QApplication with argv or NULL - the argv is ignored as Qt internally calls GetCommandLine().
Then use app.arguments to return the parsed arguments.
These can then be converted back into wchar with Qt's string functions if needed.
QApplication app(argc, (char**)argv); or QApplication app(argc,NULL);
QStringList args = app.arguments();
Sorry I didn't originally flag this Qt because I didn't think that was relevant.
If somebody wants to edit this to also include how to do this in MFC - please do.
You need to name your entry point wmain: http://msdn.microsoft.com/en-us/library/fzc2cy7w(VS.80).aspx
Try this:
#include <tchar.h>
int _tmain( int argc, TCHAR **argv )
{
return 0;
}
_tmain is defined as wmain when compiled with the UNICODE option and as main when compiled with the MBCS option.
You can use GetCommandLine function for that purpose.