Thread: Mingw resources - Example not working.

  1. #1
    60% Braindead
    Join Date
    Dec 2005

    Mingw resources - Example not working.

    I'm trying to link a resource file with mingw. I went to mingw's site, read this tutorial:

    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
    But, for making a .c / .cpp file into a .o, it doesnt work. And when I try linking my .o & .cpp, it deletes my cpp file! (Had to make a backup one -,-) I've tried this six times, it does the same thing each time
    $ g++ -o hello hello.o -mwindows
    g++.exe: hello.o: No such file or directory
    g++.exe: no input files
    Likewise when I specify a directory for hello.o. Is this bad documentation or am I just a retard? -,-.

  2. #2
    Join Date
    Jan 2002
    You need to compile source and resources to objects then link those objects together to create the executable. Using your example, you first invoke gcc(g++ for c++) with the '-c' switch which tells the compiler to emit an object file from the source:
    g++ -c hello.cpp
    should output hello.o. Next you compile the resource script with windres, the resource compiler:
    windres resfile.rc
    should output resfile.o. The final stage is linking the two objects together with some default libraries to create the final executable. The linker, ld, is invoked implicitly by the compiler:
    g++ -o hello.exe hello.o resfile.o -mwindows
    . Make sure you use g++ to compile c++ source code as gcc doesn't always work for c++ (gcc is the 'c' compiler).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  3. Resources, icon, resources....
    By bluehead in forum Windows Programming
    Replies: 11
    Last Post: 05-26-2002, 05:59 AM
  4. Compiling Resources In MinGW
    By Okiesmokie in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:16 PM
  5. MinGW Resource
    By Okiesmokie in forum Windows Programming
    Replies: 1
    Last Post: 03-05-2002, 08:32 PM