Sunday, June 23, 2013

Eclipse CDT and Microsoft Visual C++ compiler

Trying to work on this legacy project in Eclipse instead of Visual Studio 2003 I hit a few snags, so I started again by creating a new project to see if possible to use the VC++ compiler in the first place - so here it is sharing this with you. Although proficient with Eclipse for Java development I'm still a C++ newbie, so if any Visual Studio / C++ veterans reading this, feel free to correct me :-)

  • Eclipse 4.3 RC3 (CDT 8.2)
  • Visual Studio 2003 (VC7)

Let's create the project - File > New > C++ Project



The project will have a few compile errors, due to missing includes:


Now to fix this, you can obviously search the VC++ directories for iostream in this case, or if you want to go all the includes you can run vcvars32.bat and show the INCLUDE environment variable, e.g.

C:>"C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat"
Setting environment for using Microsoft Visual Studio .NET 2003 tools.
(If you have another version of Visual Studio or Visual C++ installed and wish
to use its tools from the command line, run vcvars32.bat for that version.)

C:>set INCLUDE
INCLUDE=C:\Program Files\Microsoft Visual Studio .NET 2003\VC7\ATLMFC\INCLUDE;...
I'll just add the minimum necessary to fix the project - go to Project (right-click) > Properties > C/C++ Build > Settings > Tool Settings > C++ Compiler > Preprocessor > Add...


Coming back to the Project Explorer you may notice the Includes section - if the errors still persist, right click the project and choose Index > Rebuild or choose Project (top menu) > C/C++ Index > Rebuild and that should clear them.


This concludes the CDT compiler part, now what we want to do is build the project using VC++ compiler and linker. If you chose to Build the project (the "hammer" or right-click build), you will notice the errors in the console where it can't find CL.exe.
01:15:55 **** Incremental Build of configuration Debug for project HelloWorld ****
Info: Internal Builder is used for build
cl /c /EHs /MD /Zi "/IC:\\Program Files\\Microsoft Visual Studio .NET 2003\\Vc7\\include" /nologo "/Fosrc\\HelloWorld.obj" "..\\src\\HelloWorld.cpp" 
Cannot run program "cl": Launching failed

Error: Program "cl" not found in PATH
PATH=[...]

01:15:55 Build Finished (took 46ms)

We're going to fix this by adding VCx\bin location to the PATH environment variable: Project > Properties > C/C++ Build > Environment > PATH... We're going to add it to all configurations, not just debug.


If you're going to build the project again, you will notice that CL.exe now runs correctly, but it ends abruptly and it's not followed by the linking.


Running a ProcMon trace it shows that CL.exe could not find mspdb71.dll that it needs and it just ends there. To resolve this we'll need to add the correct location (..\Common7\IDE) to the PATH environment variable we defined at previous step.


Rebuilding the project looks better, but now linking will fail to find msvcprt.lib.


To fix this, we're going to define the LIB environment variable (VC7\lib), same as we did for PATH earlier - it also also fail for kernel32.lib so we're going to add Vc7\PlatformSDK\Lib as well:


All is working well now and the .exe is generated:


Now that we got an exe we get to run it - either manually create it in Run Configurations or right-click the project > Run As > Local C/C++ Application:


Now if you change the source and click to "run" again, it will kick the build automatically and you'll see your changes in action - you may actually notice the CDT build output for a while, or you can configure to see both console views as the same time.


4 comments :

  1. Thank you very much

    ReplyDelete
  2. It's perfect, I have understood all.
    THANKs!!!

    ReplyDelete
  3. Thank you very much! All of my PATH and LIB information randomly vanished so this was very helpful to figure out how to set everything up manually.

    ReplyDelete
  4. I wrote the post trying to learn how to use Eclipse with a legacy C++ project but I haven't used much with it after, so I'm glad at least it helped you guys! :-)

    ReplyDelete