Projekat

Općenito

Profil

Akcije

Podrška #14599

Zatvoren

mingw compiler

Dodano od Ernad Husremović prije skoro 17 godina. Izmjenjeno prije više od 16 godina.

Status:
Zatvoreno
Prioritet:
Normalan
Odgovorna osoba:
Kategorija:
-
Početak:
21.06.2008
Završetak:
% završeno:

100%

Procjena vremena:

Opis

win32 native gcc


Fajlovi

mingw-utils-0.3.tar.gz (1,51 MB) mingw-utils-0.3.tar.gz Ernad Husremović, 21.06.2008 11:32
Akcije #1

Izmjenjeno od Ernad Husremović prije skoro 17 godina

Compiling and Building with MinGW

How to create a console application

Here's an example. The following is a code sample for a simple C program. Cut and paste it into a file named hello.c to try it out.

#include <stdio.h>

int main(int argc, char **argv)
{
  printf ("Hello\n");
  return (0);
}

If you want to create a console mode executable hello.exe from a c file called hello.c, try the following:

gcc -c hello.c

This compiles hello.c into an object file, hello.o

gcc -o hello hello.o

This creates an executable hello.exe from hello.o. Alternatively, you can compile and link in one step using:

gcc -o hello hello.c        

The following is a code sample for a simple C++ program. Cut and paste it into a file named hello.cpp to try it out.

#include <iostream>
int main(int argc, char **argv)
{
 std::cout << "Hello" << std::endl;
 return (0);
}

For the C++ program, use the following to compile and link:

g++ -c hello.cpp
g++ -o hello hello.o     

How to create a windows application?

Here's an example. The following is a code sample for a simple Windows program. Cut and paste it into a file named hello.c to try it out.

   #include <windows.h>

   int WINAPI WinMain (HINSTANCE hInstance, 
                        HINSTANCE hPrevInstance, 
                        PSTR szCmdLine, 
                        int iCmdShow) 
   {
      MessageBox (NULL, "Hello", "Hello Demo", MB_OK);
      return (0);
   }

If you want to create a Windows executable hello.exe, from a c file called hello.c, try the following:

   gcc -c hello.c

This compiles hello.c into an object file, hello.o

   gcc -o hello hello.o -mwindows

This creates an executable hello.exe from hello.o The -mwindows switch is needed to create Windows executables instead of console applications. It assures the appropriate Windows libraries are linked in for you. To get a console screen along with a standard windows application, add the -mconsole flag as well as -mwindows.

If you have resources from a resource file (.rc) that also need to be added to your executable, you'll need to compile the resource file as well as your other source files and include the compiled resources when linking to create the executable. Here's an example that shows how to compile and link in a resource file named resfile.rc.

   windres -o resfile.o resfile.rc
   gcc -o hello hello.o resfile.o -mwindows             

How to create a dll

Here's an example. Cut and paste the following into a file named dllfct.h:

   #ifdef BUILD_DLL
   // the dll exports
   #define EXPORT __declspec(dllexport)
   #else
   // the exe imports
   #define EXPORT __declspec(dllimport)
   #endif

   // function to be imported/exported
   EXPORT void tstfunc (void);

Cut and paste the following into a file named dllfct.c:

   #include <stdio.h>
   #include "dllfct.h" 

   EXPORT void tstfunc (void)
   {
      printf ("Hello\n");
   }

Cut and paste the following into a file named hello.c:

   #include "dllfct.h" 

   int main ()
   {
      tstfunc ();
      return (0);
   }

To create the dll and an executable that uses it, try the following:

   gcc -c hello.c
   gcc -c -DBUILD_DLL dllfct.c
   gcc -shared -o tst.dll -Wl,--out-implib,libtstdll.a dllfct.o
   gcc -o hello.exe hello.o -L./ -ltstdll         

For more information on dlls, see http://www.nanotech.wisc.edu/~khan/software/gnu-win32/dllhelpers.html
How to create a def file for a dll

There are several methods that can be tried in order to create a definition file (.def) when one is not supplied.

  • One option is the tool, pexports which is provided in the MinGW Utilities package, mingw-utils. See the Downloads page for the Current version. If your dll has functions that use the Pascal calling convention, you'll need to use the -o option.
  • Another option is the tool, impdef.
    • More instructions on how to create def files from dlls, a copy of impdef and more information on how to use it are available at Colin Peters' site. See the Tutorials section. Other compilers may also supply versions of the impdef program that can be used to create a .def file which will work with any compiler. If you have another version of impdef from another compiler, you may wish to try it. Some handle the Pascal calling convention better than others. Borland has a version of impdef and other compiler utilities available for download at their Borland Community web site. Their Borland C++ version 5.5 compiler includes several utilities to help convert between standard formats, their formats and Microsoft's formats.
  • Another option is to use nm which comes with the MinGW distribution. This option will not work for all dlls. Problems may occur if the dll is stripped or compiled as 16 bit. To use this technique, you'll need to filter the output from nm to create a def file. This can be done by hand in an editor or automated using tools like Perl (Practical Extraction and Report Language) or grep (global regular expression print) and sed (stream editor). Even with the automated methods, you may have to make some changes by hand if the Pascal calling convention is used by the dll. See Colin Peters' site for more details on this case. (Versions of sed and grep are available from various sites including archives that host gnuish MSDOS and archives such as Virtually Un*x that contain Win32 ports of common Unix tools and from the self-hosting MinGW port distribution. The ActiveState version of Perl works well on Win32 platforms.) Here are examples of possible filtering techniques.
    • This example uses grep and sed. If you have a dll named file.dll that you wish to create a def file for named file.def, try the following:
      
       echo EXPORTS > file.def
       nm file.dll | grep ' T _' | sed 's/.* T _//' >> file.def
      
    • To create a library file named file.a from the dll and def file, type:
          dlltool --def file.def --dllname file.dll --output-lib file.a
      
    • This example uses Perl. Copy the following Perl script to a file called dll.pl and use it:
      
                     open (OUTFILE,">dll.def");
                     print OUTFILE "EXPORTS\n";
                     open (INFILE,"dll.fil");
                     while(<INFILE>)
                     {
                        if ($_ =~ /T _/)
                        {
                           $line = $_;
                           $line =~ s/.* T _//;
                           print OUTFILE $line;
                        }
                     }
                     close (INFILE);
                     close (OUTFILE);
      
      
    • If you have a dll file named file.dll. At the command line, type:
             nm file.dll > dll.fil
             perl dll.pl
      
      • A def file named dll.def will be created. You can rename this as needed. You'll also probably want to delete dll.fil when you're finished with this process.
    • If you don't have any of these tools on your system, you can still use nm to create a def file and edit it by hand through an editor. For example:
               nm file.dll > dll.fil
               find " T _" dll.fil > dll.def
      
      • Replace the line at the top of dll.def that was created by the find program and shows a file name with a line that says EXPORTS. Set your editor to search for T _ and erase it and anything on the line before it, leaving only the routine names in the file.
      • If the previous options don't work, you can still try to create a def file using the output from the objdump program (from the MinGW distribution). Here's an example.
            
        objdump -p file.dll > dll.fil
        
    • Search for [Ordinal/Name Pointer] Table in dll.fil and use the list of functions following it to create your def file.
Akcije #3

Izmjenjeno od Ernad Husremović prije skoro 17 godina

C:\mingw-utils\bin

21.06.2008  11:46       <DIR>          .
21.06.2008  11:46       <DIR>          ..
20.02.2004  22:36                2.494 a2dll
20.02.2004  22:36              409.680 dos2unix.exe
20.02.2004  22:36            1.027.205 drmingw.exe
20.02.2004  22:36               27.313 dsw2mak
20.02.2004  22:36              416.256 exchndl.dll
20.02.2004  22:36              421.913 pexports.exe
20.02.2004  22:36              405.481 redir.exe
20.02.2004  22:36              409.474 reimp.exe
20.02.2004  22:36              409.719 res2coff.exe
20.02.2004  22:36              409.150 unix2dos.exe

Akcije #4

Izmjenjeno od Ernad Husremović prije više od 16 godina

  • Status promijenjeno iz Dodijeljeno u Zatvoreno
  • % završeno promijenjeno iz 0 u 100
Akcije

Također dostupno kao Atom PDF