Solved

How to Use .NET to compile C++ code

Posted on 2004-09-18
15
289 Views
Last Modified: 2010-04-24
Hi I'm new to .Net and C++
I am trying to use .Net to compile my C++ code.  Does anyone know how to do this?  I'm not sure where to begin.

Thanks
0
Comment
Question by:BBrian
  • 7
  • 5
  • 3
15 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12093496
If your C++ is standard, non managed C++. Then you can make it as usual. Create a new Project in File -> New -> Project -> Win32 console application (I guess your app is console based. If not, please advice)
0
 

Expert Comment

by:axsaxs
ID: 12094560

If you download the .NET Framework (for instance here: http://msdn.microsoft.com/netframework/downloads/framework1_1redist/), after installing you will have the Microsoft C/C++ compiler, which is called "cl" and can compile standard C++ source code.

You can find compiler options here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_compiler_reference.asp

Altough not 100% ANSI-ISO C++ compatible, this new version of the Microsoft C++ compiler is quite standard and offers, IMHO, some of the best optimizations in the "free" compilers market.

0
 

Author Comment

by:BBrian
ID: 12103646
Here's the probem....when I paste this code..

#include <iostream.h>
void main ( )
{
      cout <<"Hello World"<< endl;
}


I get errors.  It doesn't recognize the iostream.h file.  It says there is no such file directory exists.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12103698
type with:

#include <iostream>

don't use .h extension.
0
 

Author Comment

by:BBrian
ID: 12104704
ok..i omitted the .h extention..but i still get..

There is no build command line to execute.
Performing Makefile project actions
GenTest2 : error PRJ0003 : Error spawning ''.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12104731
How have you created your project? which options?
0
 

Author Comment

by:BBrian
ID: 12105053
I chose...

1.  Visual C++ projects
2.  Under The General Tab...I selected...
     MakeProjectFile
3.  The wizard comes up..then I click finish
4.  Add a new item to the project
5.  I select a .cpp file


Then I paste this code into the .cpp file

#include <iostream>
void main ( )
{
      cout <<"Hello World"<< endl;
}


and i get the error..
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12105168
Yes, but which kind of project have selected in the Wizard? there are many. Mus be console application.
0
 

Author Comment

by:BBrian
ID: 12106037
There are five tabs..

1.  .NET
2.  ALT
3.  MFC
4.  WIN32
5.  GENERAL

I most recently tried selecting general and then clicking on MakeProjectFile
but I also tried clicking on Win32 then clicking on Win32Console Project

I got the same error either way.

am I doing something wrong?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12106088
for a win32Console, there is an option that says 'Simple hello world application", choose it, compile and run it. If succefull, replace main function internals with your code.
0
 

Author Comment

by:BBrian
ID: 12106669
I don't see that option.
0
 

Expert Comment

by:axsaxs
ID: 12109979
Ok. First of all correct the code:

#include <iostream>
void main ( )
{
     std::cout <<"Hello World"<< std::endl;

    char c;
    std::cin >> c; // just press any key
}

In fact you are using the std namespace. IMHO, forget the "Simple hello world"... it's better to learn to code from scratch!

Check out the steps:

1. New C++ project
2. New Win32 console project
3. Finish
4. Paste the code
5. Wipe out "precompiled headers" (Project/Properties then C/C++/Precompiled Headers choose: Not using precompiled headers)
6. Run/debug program.
7. Press any key + enter to exit



0
 

Author Comment

by:BBrian
ID: 12114484
Hey Axsaxs it worked....

I had to remove this code though..


#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
      return 0;
}

and replace it with my hello world code...


if I leave both in i get this error

Linking...
Win32ConsoleProject.obj : error LNK2005: _main already defined in HelloWorld.obj
Debug/Win32ConsoleProject.exe : fatal error LNK1169: one or more multiply defined symbols found
0
 

Accepted Solution

by:
axsaxs earned 50 total points
ID: 12120571

That's because the "stdafx.h" is the include for the pre-compiled headers. It's not a bad thing to have pre-compiled headers (it can speed up things if your project grows large), but it is Microsoft-specific. So, I'd rather prefer not to have them. But, there are some techniques to have precompiled headers without the "stdafx.h", but that should be the answer to another question... ;)



0
 

Author Comment

by:BBrian
ID: 12124972
Thanks alot
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now