Ko:Using Visual C 2005 Express Edition

From wiki.gp2x.org

Contents

시작하기

  1. Visual C++ 2005 Express Edtion을 다운로드합니다. 마이크로소프트사의 홈페이지에서 무료로 이용가능합니다..
  2. Visual C++ 2005 Express를 인스톨합니다. (mscoree.dll과 관련된 에러가 발생하면, .NET Framework 2.0을 다시 인스톨하거나 인스톨합니다.)
  3. SourceForge에서 devkitGP2X를 다운로드합니다. devkitPro 인스톨러는 현재 devkitGP2X를 지원하지 않습니다.
  4. devkitGP2X를 C:\devkitpro에 압축을 풉니다. 그리고 C:\devkitpro 디렉토리에 devkitGP2X 디렉토리가 생성되었는지 확인하세요. 이러한 방법은 devkitPro가 추천하는 방법입니다. 이렇게 함으로 다양한 devkitPro 배포판을 하나의 디렉토리에 둘 수 있습니다.
  5. 주의: \devkitGP2X 이외의 디렉토리명을 사용하기로 하였다면 적절하게 "bin\arm-linux-sdl-config"과 "demo\Makefile" 내의 경로를 수정해주어야 합니다.
  6. bin 디렉토리를 당신의 PATH에 추가해주어야 합니다. 시작->설정->제어판을 클릭하시고, 시스템->고급->환경 변수를 열어 PATH를 선택한 뒤 c:\devkitpro\devkitGP2X\bin를 경로에 추가해줍니다.
  7. Win32용(Windows용) sed를 [1]에서 다운로드합니다. Sed는 GCC가 컴파일시 발생시키는 경고와 에러들의 포맷을 변경하여 Visual C++이 이해할 수 있는 형태로 바꾸어 줍니다.
  8. Sed 실행파일만을 압축을 풀어서 이름을 sed.exe로 바꾸고 PATH경로 상에 두어서 어디서나 실행가능하도록 합니다. 저의 경우에는 그것을 C:\devkitpro\devkitGP2X\bin에 두었습니다.


Note: See 'Using Visual C++ 2005 Express Edition with the Microsoft Platform SDK' about setting up Express so you can create Win32 applications. For example, if you are using SDL to have the same program run on Windows and the GP2X, you will be able use the same code to build a Win32 application to test and debug your code before trying it on the GP2X where debugging is more difficult.

Creating The Custom Build Rule

Visual C++ 2005 has a new feature called Custom Build Rules. We will use this feature to tell Visual C++ 2005 to use our arm-linux-gcc compiler instead of its built-in C/C++ compiler. Copy the following XML file and paste it into a (new) file called arm-linux.rules in c:\devkitpro\devkitGP2X:

<?xml version="1.0" encoding="utf-8"?>
<VisualStudioToolFile
 Name="GCC Compiler"
 Version="8.00"
 >
 <Rules>
  <CustomBuildRule
   Name="arm-linux-gcc"
   DisplayName="GCC Compiler"
   CommandLine="arm-linux-gcc.exe -c [AllOptions] [AdditionalOptions] -o $(IntDir)\$(InputName).o [inputs] 2&gt;&amp;1 | sed -e s/\(.[a-zA-Z]\+\):\([0-9]\+\):/\1(\2):/"
   Outputs="$(IntDir)\$(InputName).o"
   FileExtensions="*.c;*.cpp"
   ExecutionDescription="$(InputFileName)"
   >
   <Properties>
    <StringProperty
     Name="IncludePaths"
     DisplayName="Additional Include Paths"
     Description="Specifies one or more directories to add to the include path; use semi-colon delimited list if more than one."
     Switch="-I&quot;[value]&quot;"
     Delimited="true"
     Inheritable="true"
    />
    <BooleanProperty
     Name="WarningsAsErrors"
     DisplayName="Warnings As Errors"
     Description="Treat warnings as errors"
     Switch="-Werror"
    />
    <EnumProperty
     Name="OptimizationLevel"
     DisplayName="Optimization Level"
     Description="Set the optimization level"
     >
     <Values>
      <EnumValue
       Value="0"
       DisplayName="Default"
      />
      <EnumValue
       Value="1"
       Switch="-O0"
       DisplayName="No Optimization"
      />
      <EnumValue
       Value="2"
       Switch="-O1"
       DisplayName="Simple Optimization"
      />
      <EnumValue
       Value="3"
       Switch="-O2"
       DisplayName="Optimize for Speed"
      />
      <EnumValue
       Value="4"
       Switch="-Os"
       DisplayName="Optimize for Size"
      />
      <EnumValue
       Value="5"
       Switch="-O3"
       DisplayName="Maximum Optimization"
      />
     </Values>
    </EnumProperty>
    <StringProperty
     Name="PreprocessorDefines"
     DisplayName="Preprocessor Defines"
     Description="Specifies one or more preprocessor defines.  Separate multiple defines with a semi-colon."
     Switch="-D[value]"
     Delimited="true"
     Inheritable="true"
    />
    <EnumProperty
     Name="WarningLevel"
     DisplayName="Warning Level"
     Description="Set the level of warnings that are displayed."
     >
     <Values>
      <EnumValue
       Value="0"
       DisplayName="Default"
      />
      <EnumValue
       Value="2"
       Switch="-Wall"
       DisplayName="All"
      />
     </Values>
    </EnumProperty>
   </Properties>
  </CustomBuildRule>
  <CustomBuildRule
   Name="arm-linux-link"
   DisplayName="GCC Linker"
   CommandLine="arm-linux-gcc.exe $(IntDir)\*.o -o &quot;$(TargetPath)&quot; [AllOptions] [AdditionalOptions] -static 2&gt;&amp;1 | sed -e s/\(.[a-zA-Z]\+\):\([0-9]\+\):/\1(\2):/"
   Outputs="never-exists"
   FileExtensions="*.link"
   ExecutionDescription="Linking $(TargetFileName)..."
   >
   <Properties>
    <StringProperty
     Name="LibPaths"
     DisplayName="Additional Library Paths"
     Description="Specifies one or more directories to add to the lib path; use semi-colon delimited list if more than one."
     Switch="-L&quot;[value]&quot;"
     Delimited="true"
     Inheritable="true"
    />
    <StringProperty
     Name="Libraries"
     DisplayName="Additional Dependencies"
     Description="Specifies one or more libraries to link to; use semi-colon delimited list if more than one."
     Switch="-l[value]"
     DefaultValue="pthread"
     Delimited="true"
     Delimiters=";, "
     Inheritable="true"
    />
   </Properties>
  </CustomBuildRule>
  <CustomBuildRule
   Name="arm-linux-ar"
   DisplayName="GCC Archiver"
   CommandLine="arm-linux-ar rcs [AdditionalOptions] &quot;$(TargetPath)&quot; $(IntDir)\*.o"
   Outputs="never-exists"
   FileExtensions="*.ar"
   ExecutionDescription="Creating library $(TargetFileName)..."
   >
   <Properties>
   </Properties>
  </CustomBuildRule>
 </Rules>
</VisualStudioToolFile>

arm-linux-gcc is the C compiler, but it can also compile C++ source code if the source files end with a .cpp file extension.

Creating A Project

  1. Create the base project
    1. Start Visual C++ 2005 Express.
    2. Create a new project (File->New->Project).
    3. Select the General category and select Empty Project.
    4. Enter a name for the project in the Name editbox.
    5. Untick the Create directory for solution checkbox.
    6. Click OK
  2. Customizing the Project
    1. Open the Custom Build Rules dialog from the menu (Project->Custom Build Rules)
    2. Select the Find Existing button and locate the arm-linux.rules file you saved earlier in c:\devkitpro\devkitGP2X.
    3. Select Yes to add the path to the Rule Files Search Path.
    4. Tick the GCC Compiler checkbox.
    5. Select Ok. Visual C++ 2005 Express may show two Rule File Conflict warnings. Click Yes for both of these to allow GCC to build your C and C++ files instead of the built-in compiler.
  3. Customizing to compile for your GP2X
    1. Add some source files to the project. (The GCC Compiler property page will not appear until there are some source files in the project.)
    2. Right-Click on the project name in the Solution Explorer
    3. Choose Properties from the bottom of the drop-down list.
    4. Select the General property page. In Extensions to Delete on Clean, add *.o to the end of the list.
    5. In the same menu, open the Linker category and select the General property page. Change the extension of the Output File from .exe to .gpe.
      1. You should see: $(OutDir)\$(ProjectName).exe
      2. Change to: $(OutDir)\$(ProjectName).gpe
    6. If you need to add extra include paths or want to change some of the GCC compiler options, edit these in the General property page in the GCC Compiler category.
      1. For SDL: Under "Additional Include Paths" put "C:\devkitpro\devkitGP2X\include\SDL\" if you installed to the default paths
    7. Open the GCC Linker category and select the General property page.
    8. Fill in any additional library paths and libraries needed to build your project.
    9. Select Ok on the project settings dialog.
  4. Change the Linker
    1. Make sure your project is selected in the Solution Explorer (click on the project name, then...)
    2. Open the Tool Build Order dialog from the menu (Project->Tool Build Order), which will only show up if your project is selected.
    3. Move the GCC Linker entry to just after the Linker Tool entry.
    4. Select Ok
  5. Make your First Build
    1. Create a new text file with the extension .link to the project. This is to trigger the arm-linux-link build rule.
    2. Build the solution (Build->Build Solution).

If you used the default Debug build configuration, the .gpe file will be in the Debug directory in your project directory.

Advanced Projects

Using the custom build rules on just one project is all well and good, but eventually you want to get into more complex applications that might be built from multiple projects. Using the arm-linux-ar custom build rule, you can create libs for use by other applications, or as part of a larger application.

The directory structure of your solution and projects becomes important when you start having multiple projects in the one solution. This is because the linker and ar rules use a wildcard to specify the input files.

Here is a suggested directory structure for a solution containing two libraries and an application.

GameSolution
  GameSolution.sln
  LibProject1
    LibProject1.vcproj
    debug
    release
    include
    source
  LibProject2
    LibProject2.vcproj
    debug
    release
    include
    source
  GameProject
    GameProject.vcproj
    debug
    release
    include
    source
  lib

Note: The debug and release folders are automatically created by Visual C++ 2005 Express.

Each project is in its own directory under the solution directory. This allows each project to have its own Debug and Release directories for the .o (object) files. So when the linker and ar use their wildcard to gather together all the .o files for a project, they will not accidentally gather the .o files for the other projects as well. It also means that the IDE can clean the object files for one project and leave the other projects intact.

Now that we have the directory structure laid out, we can start creating the projects. As opposed to the simple project instructions above, we are now going to create an empty project but with the difference being that we are going to leave the "Create directory for solution" checkbox enabled. This will start off the directory structure as shown above.

Now create two more empty projects and add them to the solution so they form the directory structure shown above.

  1. Check the Custom Build Rules dialog (right-click on the project name) and make sure that the GCC Compiler rule is enabled.
  2. Open the project properties for LibProject1.
  3. On the General page under Configuration Properties, set the Output Directory property to $(SolutionDir)lib. This will place the .a files in the lib directory of the solution.
  4. Add *.o to the Extensions to Delete on Clean property.
  5. Just below that in Project Defaults, change Configuration Type to Static Library (.lib). Now you will need to hit the Apply button because this changes some of the other property pages.
  6. There should now be a Librarian property page. Select this and change the Output File property to $(OutDir)\libxxxd.a where xxx is the name you wish to give your library. The d is a naming convention for debug versions of libraries (omit the d for release configurations), and naming conventions also state that the library name starts with lib and ends in .a.
  7. Skip down to the GCC Compiler property page and set any properties here that you may require, such as additional include paths.
  8. Click the Ok button and open the Tool Build Order dialog (right-click on the project name).
  9. Select each project in turn in the Projects combobox and tick the projects in the Depends on list that the project is dependent on.

In our example, GameProject is dependent on LibProject1 and LibProject2, but LibProject1 and LibProject2 are not dependent on any other projects.

Now we should be ready to build the solution. Build the solution (Build->Build Solution) and if everything is good, you should end up with two .a files in the lib folder and a .gpe in the GameProject\debug folder.

Compiling for the ARM940T CPU

If you have been following the demo coding tutorials by Dzz then you will know how to create a bin file for the 940T to run. Here is how to do create the bin file within Visual C 2005 Express just as if you were creating a gpe.

The process requires two application passes over the compiled files, so the rules file calls a DOS batch file as the custom build rules are unable to execute more than one command for each source file. This batch file goes in the bin directory of your gp2x sdk, eg. C:\devkitPro\devkitGP2X\bin. I've been using this for more than a month now and it works a treat.

The custom build rule expects the batch file to be called make_940_bin.bat.

ld.exe -o %1 -Ttext 0x0 %3 %4 %5 %6 %7 %8 %9 2>&1
arm-linux-objcopy.exe -O binary %1 %2

As with the other rules you have to add a trigger file to the project. This one uses a file called file.940. I've put my 'trigger' files in my sdk root folder, as they can be re-used without problems by more than one project.

Here is the addition to the rules file you need to add.

<CustomBuildRule
    Name="arm-940-link"
    DisplayName="GCC 940 Linker"
    CommandLine="make_940_bin.bat $(TargetPath) $(TargetDir)$(TargetName).bin [AllOptions] [AdditionalOptions] $(IntDir)\*.o"
    Outputs="never-exists"
    FileExtensions="*.940"
    ExecutionDescription="Linking 940 $(TargetFileName)..."
    >
<Properties>
    <StringProperty
        Name="LibPaths"
        DisplayName="Additional Library Paths"
        Description="Specifies one or more directories to add to the lib path; use semi-colon delimited list if more than one."
        Switch="-L"[value]""
        Delimited="true"
        Inheritable="true"
    />
    <StringProperty
        Name="Libraries"
        DisplayName="Additional Dependencies"
        Description="Specifies one or more libraries to link to; use semi-colon delimited list if more than one."
        Switch="-l[value]"
        DefaultValue="pthread"
        Delimited="true"
        Delimiters=";, "
        Inheritable="true"
    />
    <StringProperty
        Name="EntryPoint"
        DisplayName="Entry point for 940T CPU"
        Description="Specifies where the execution starts"
        Switch="-e"[value]""
        Delimited="true"
        Inheritable="true"
    />
</Properties>
</CustomBuildRule>

More Information

To avoid having to copy the .gpe and data files to the SD card in the GP2X every time you make a change, install the Samba client on the GP2X. This allows you to mount a shared directory on your PC as a directory on the GP2X. Compile your .gpe and data files into this shared directory, then use a telnet connection to the GP2X to run the application straight from the shared directory.

Personal tools