Writing portable code in Visual Studio C++

R

Richard Giuly

Hello,

I would like to write "portable" C++ code that could theoretically run
on linux, windows, and other platforms, and I'd like to use VS as the
editor/compiler/linker.

The simplest thing that seems to complie in VS is this:

//VS example
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

This is a console project. But this is not really portable, because
it's using _tmain instead of main and it's using stdafx.h.

Is there some way to make a simple/portable thing like the code below
compile in Visual Studio? As far as I can tell, console type projects
are the closest to what I want but not really it.

//portable example that does not complie in VS
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
}



Any help will be appreciated.

-Richard Giuly, UCSD
(Please post replies to the group because I do not read the above yahoo
email account.)
 
J

Jack Klein

Hello,

I would like to write "portable" C++ code that could theoretically run
on linux, windows, and other platforms, and I'd like to use VS as the
editor/compiler/linker.

The simplest thing that seems to complie in VS is this:

//VS example
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

This is a console project. But this is not really portable, because
it's using _tmain instead of main and it's using stdafx.h.

First, compiler specific issues not defined by the language are
off-topic here. Second, there is no such thing as "Visual Studio",
there have been a lot of versions over the years. With different
versions of the IDE and different versions of the compiler every time.
Is there some way to make a simple/portable thing like the code below
compile in Visual Studio? As far as I can tell, console type projects
are the closest to what I want but not really it.

//portable example that does not complie in VS
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
}

I am pretty sure that there is nothing you can do to get this to
compile under Visual C++ 4 or earlier, for example. You can probably
get it to compile with VC++ 6 or later. VC++ 6, part of Visual Studio
98, which predates the ISO C++ standard, complains about the missing
return statement, but builds an executable that runs. Adding a return
0 statement eliminates the warning.

You need to read your IDE's help files on turning off precompiled
headers, among other things.
Any help will be appreciated.

If you want more specific help with Microsoft's IDE and extensions,
including how to disable those extensions, you can search on
http://msdn.microsoft.com, or look for IDE newsgroups on Microsoft's
support group server msnews.microsoft.com, in the
family. But be sure to mention exactly which
version of Visual Studio you are using.
 
P

Phlip

Richard said:
I would like to write "portable" C++ code that could theoretically run
on linux, windows, and other platforms, and I'd like to use VS as the
editor/compiler/linker.

The last time I had such a portability issue, I had a 3rd-party library that
was flakey on Linux, /and/ was complicated enough to require Intellisense.
You can get that on Linux, but I also didn't want to spend time researching
how to do that!

I used Samba to share a source code drive on Linux. Then, from Windows, I
shared the drive, switched to the source code folder, and opened the project
in Visual Studio.

For the Linux side, I arranged a Makefile with a test target. (You _are_
writing unit tests, aren't you?) Then I wrote a little script called
'trigger'. It waits until source code changes, then calls a command. So I
launch that, in a SSH shell, like this:

$ trigger 'make test' *.cpp *.h

Now each time I change code and test it in VS, the trigger script also
compiles tests the code in Linux. This saved me untold headaches when the
Linux side would crash. The Windows side worked fine. I could back out my
most recent change, instead of debugging it, or throwing away a lot of work
and reverting to the last integration checkpoint that Linux liked.

Now on to your actual question...
Is there some way to make a simple/portable thing like the code below
compile in Visual Studio? As far as I can tell, console type projects
are the closest to what I want but not really it.

//portable example that does not complie in VS
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
}

I don't understand what you are asking for. VS can compile the above fine,
to within a warning about the missing 'return 0'. Just add it.

If VS complains about the missing "stdafx.h", you can just add such a file.
It is not required to contain non-portable stuff. And I suspect you could
configure the GNU g++ to _also_ use it for precompiled headers.
 
F

flagos

Richard:

Perhaps it would be good idea to jump directly to a multi-plataform
framework. You will have all there to run on Windows, Linux, etc. I
would sugget you wxWidgets in VStudio 2005, but there are many to try.

Hope this help.
 
M

Mogens Heller Jensen

Richard Giuly said:
Hello,

I would like to write "portable" C++ code that could theoretically run
on linux, windows, and other platforms, and I'd like to use VS as the
editor/compiler/linker.

The simplest thing that seems to complie in VS is this:

//VS example
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

This is a console project. But this is not really portable, because
it's using _tmain instead of main and it's using stdafx.h.

Is there some way to make a simple/portable thing like the code below
compile in Visual Studio? As far as I can tell, console type projects
are the closest to what I want but not really it.

//portable example that does not complie in VS
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
}



Any help will be appreciated.

-Richard Giuly, UCSD
(Please post replies to the group because I do not read the above yahoo
email account.)

I think what you need is to create a new project like this (VS 2003):
File -> New -> Project -> Visual C++ Projects -> Win32 -> Win32 Console
Project

- and then, when the wizard thingamajig appears, do not forget to click
Application Settings and check the Empty Project checkbox... this should
result in an empty project, requiring a main() function to run.

This is probably also the way to go if you choose to use libs like e.g.
wxWidgets for portable UI code.

-Mogens
 
N

Noah Roberts

Richard said:
//VS example
#include "stdafx.h"

Turn off precompiled headers to get rid of the above required header.
Or, learn to use them and fill that header with includes you use all
the time.

int _tmain(int argc, _TCHAR* argv[])

Just change the sig to int main(int argc, char * argv[]) or any other
standard signature.
{
return 0;

You don't HAVE to return but I always do anyway since this is an int
function and any int function should have a return...the fact that std
c++ lets you not in this one case isn't that great imho.
}

This is a console project. But this is not really portable, because
it's using _tmain instead of main and it's using stdafx.h.

I don't know what _tmain is but I regularly replace it with a real
main. stdafx.h is just a header that your project is created with
because precomp headers are turned on by default; I often change its
name because stdafx is meaningless to me but you don't have to. Turn
them off or use them...no biggie...this doesn't make your program
non-standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top