473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Writing portable code in Visual Studio C++

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.)

Jul 31 '06 #1
5 7598
On 30 Jul 2006 21:54:41 -0700, "Richard Giuly" <rg****@yahoo.com>
wrote in comp.lang.c++:
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
news:microsoft.publie.* family. But be sure to mention exactly which
version of Visual Studio you are using.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 31 '06 #2
Richard Giuly wrote:
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.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 31 '06 #3
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.
Richard Giuly wrote:
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.)
Jul 31 '06 #4

"Richard Giuly" <rg****@yahoo.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
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
Jul 31 '06 #5

Richard Giuly wrote:
//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.

Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rolf Hemmerling | last post by:
Hello ! Beginner's question: Howto access .RC/.RES Ressource files with portable C++ code ( BCC,MSVC,GNU-C++, OpenWatcom) ? I just wanna access "local language strings", so that I may...
0
by: JDeats | last post by:
I'm trying to create a Visual Studio.NET Add-in. I'm using the wizard provided by VS.NET. I have Visual Studio.NET Enterprise Architect edition and I've selected New->Projects->Other...
7
by: Desmond Cassidy | last post by:
Hi, I have being trying to get a grip of HTML data manipulation and am using the mshtml class and System.Net to retriver HTML pages. Now as far as I can see, one can read HTML in different ways....
14
by: mohammad.nabil.h | last post by:
hello, it's me again. i was implementing a DBMS, as usual, where i needed to call fwrite using 8-byte integer as the length of the data to be written ( just in case there is data bigger than 2 GB,...
5
by: Bob | last post by:
Hi, I have some very simple code that I have been using for quite a while that uses a streamwriter to write to a network file. I have never had any problems with it. I recently updated Visual...
7
by: jacob navia | last post by:
There are some people here (let's call them "regulars" for short) that are always giving lessons to people about how easy is to write portable code, etc. They always point fingers at you telling...
13
by: Ramon F Herrera | last post by:
My goal is to write a studio-type application which will initially run on Windows, but I would like it to run on other platforms with minimum efforts. I have made the decision to go with C++. ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.