MinGW - Compiling and Building with MinGW

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 ");
  
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 ");
}
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 
- - DBUILD_DLL dllfct.c
gcc 
- shared  - o tst.dll  - Wl, -- out - implib,libtstdll.a dllfct.o
gcc 
- o hello.exe hello.o  - L. /   - ltstdll
// gcc -o hello.exe hello.o -L. -ltstdll

 

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 " ;
         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.

GNU Development Tools Documentation

Back to index


Win32 API Documentation

MinGW uses the runtime libraries distributed with the OS, but the API documentation is not supplied with the OS and is not re-distributable. If you don't own a copy of Microsoft development tools or MSDN subscription, you can still access the API documentation from the following places:

Back to index


Tips, Howtos, Contributed Documentation, etc.

** NOTE **

Some of the information in this section is grossly out of date, and currently here for historical purposes only. In particular, more recent and relevant information about "-mno-cygwin" and using MinGW within a Cygwin development environment can be found on the FAQ page.

Introductions and Tutorials

  • Mike Linkovich's MinGW Startup Guide
    An excellant primer for getting up and running with MinGW (with an emphasis on graphics/game development). Covers some popular and useful external libraries and IDE's that work with MinGW.
  • Programming Win32 with GNU C and C++
    A tutorial by Coin Peters, the original author of MinGW.
  • How to make DLLs using GCC
    Examples by Mumit Khan.
    A set of helper programs and examples to make DLLs in C, C++ and F77 using gcc on Mingw and Cygwin. Released v0.2.5 on Mar 13, 1999. Also contains some pointers to DLL related information available from Microsoft's on-line archives.
  • How to add compiled HTML help to a MinGW application
    A tutorial by Thomas Messenger.
    It is very easy for applications that already have a menu. And it is not difficult for any WinMain window style program.

Developing Modules for Specific Software Packages


Cross-compilation and Using with Other Tools

Back to index

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值