25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music  

Microsoft Visual C++ IDE: how to create a new solution and import existing code into it

This site's zip files for Visual C++ projects only contain the essential source code required to create the projects. All the files that the IDE automatically adds when a new "solution" is created are omitted. 

The following steps describe the procedure I've successfully used to create a new Visual C++ solution for a project and import existing source code files into it so it can compile and run.

It might be tempting to try the feature called "Create a new Project from existing code...", but I have not been able to make that work. Creating a new solution from scratch is reliable.

Create new solution and import code, step by step

As a concrete example, I'll use the VCBif project, which has a zip download file and creates a solution called "VCBif".

After downloading the zip file:

  1. Unzip the zip file (vcbif.zip).
     
  2. Open Form1.h in Notepad. Note the name of the project's namespace near the top of the file, and then close the file. The namespace declaration looks like this:

    namespace VCBif {
     
  3. Launch the Visual C++ IDE.
     
  4. Use the following steps to create a new project. The project's name must exactly match the namespace because the namespace is already defined and is used throughout the project:
     
    • Click: File > New > Project > CLR > Windows Forms Application.
      The VCBif example is a Windows Forms Application. It uses the Windows Graphical User Interface (GUI).

      For Console Application projects, whose display occurs within a Command Prompt ("MSDOS box"), choose CLR Console Application.
       
    • Name = the namespace (VCBif)
    • Location = usually your Projects folder, but you can put it anywhere
    • Solution Name = (the same; it's filled in for you: VCBif)
    • Create directory for solution = checked
       
  5. Click OK and wait for the setup to complete.
     
  6. Close the VC++ IDE.
     
  7. Copy all the unzipped project files into the folder that the IDE just created for the project. The source code folder is the 2nd one with this project's name. In this example, it is:

    ...Projects\VCBif\VCBif 

    For a Windows Forms Application, it is OK to overwrite the default Form1.h file with the imported one. You shouldn't get a prompt about any other files being overwritten.

    For a CLR Console Application, it is OK to overwrite the project's main .cpp file (normally called ProjectName.cpp) with the imported one. You shouldn't get a prompt about any other files being overwritten.
     
  8. Launch Visual C++ again and open the solution. For a Windows Forms Application, you should now see the design view of the main form.
     
  9. Click View > Code to view its code.
     
  10. Scroll down to the list of library file #includes. Their paths need to be adjusted, depending on where you put the library files when you copied them:
     
    • Put the files in a location of your choosing, either in a folder of their own (best) or in the same folder as the project (the drawback is that it might cause confusion in Step 11 below).
       
    • Adjust the paths as needed. If you put the library files in the same folder as the project, just strip off the path, so

      #include "..\..\mylib\mylib.cpp"

      would become:

      #include "mylib.cpp"
       
    • When all the paths are correct, the project should compile successfully (F7).

    An alternative method that avoids using includes: Instead of using include files at all, you can replace each #include directive with the actual text of the file that it includes. In the example above, you would replace
    #include "..\..\mylib\mylib.cpp"
    with the text that is in mylib.cpp. Also replace any nested includes. For example, mylib.cpp includes my.h, so you would need to make that replacement, too. The advantage of this method is that you don't have to deal with separate directories and the paths to them. The disadvantage is that if you build more than one of our projects, there will be duplicated code in your source files.
     

  11. The files you just imported to the project directory don't appear in the project's Solution Explorer yet because they aren't officially part of the project. If you want to add them so they show there:
     
    • Click Project > Show All Files. Or open Solution Explorer and then click the button at the top that has the tooltip "Show All Files".
       
    • In Solution Explorer, the new files are marked with minus signs in a red circle.
       
    • Right-Click each file that came from the zip file. Select "Include in project".
      These are usually dialog box files or other large class definition files.

      If you put the library files (my.h, mylib.cpp, doubrect.cpp, filearay.cpp, sdib.cpp) in the same folder, do not "include" them in the project. (Each file you "include in project" gets compiled into its own .obj file. However, library files are already compiled because they are #included into other source files. Getting compiled twice will cause duplicate definition link errors.)
       
  12. Try compiling again. If you get errors, it is probably because library files accidentally got included in the project.
     
    • Right-click each file causing an error and select "Exclude from project".

Questions are welcome in the forum.

 


 

Valid HTML 4.01 Transitional
Yahoo! Search
Search the web Search this site
Valid CSS