Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Linking with resource files in MinGW

Credits go to 23yrold3yrold for helping out!
This thread is locked; no one can reply to it. rss feed Print
Linking with resource files in MinGW
aybabtu
Member #2,891
November 2002

I apparently need to do this to use dialogs in my programs...but I can't figure out how to link with them. Thanks!

23yrold3yrold
Member #1,134
March 2001
avatar

  windres rsrc.rc -o rsrc.o

Then link with rsrc.o like all other object files.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

aybabtu
Member #2,891
November 2002

Okay...I'm not sure how to do that...usually I do this:

g++ -s -mwindows -O2 -O3 winwolf.cpp -o winwolf.exe -lalleg

So, I'm guessing I'd stick rsrc.o in between the -o and winwolf.exe? (I'm not going to use resources for winwolf, this is just hypothetical.)

23yrold3yrold
Member #1,134
March 2001
avatar

I dunno; I've never been real sharp on the layout of that compile line :) I could post my makefile full of variables, but the layout of that line is a lot different from yours ... I think you have to use a line for compiling and a line for linking though (since .cpp and .rc files don't use the same compiler) ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Monolith Tyriss
Member #2,523
July 2002

Yup. You put the rsrc.o file in after the -o option but before the .exe option. If I remember right anyway, -o is putting everything together, so adding rsrc.o in after -o is telling the compiler to put that object with the result of winwolf.cpp together into winwolf.exe. I'm not sure that's how it works, given that I haven't used a gcc command line in 2 or 3 years, but I think that's what you need. :}

X-G
Member #856
December 2000
avatar

Monolith: NOOO :o

-o xxxx sets gcc's output file to be xxxx. 23 got it right, but what he REALLY should do is compile all the .cpp files and then link the .o files together. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

23 got it right, but what he REALLY should do is compile all the .cpp files and then link the .o files together.

That's what I said (twice) ;) Here's the relevant bit of my makefile, if it does Oolong any help ...

# main rule; compiles and links everything
$(BINARY) : $(OBJS) rsrc.o 
  $(CXX) -o $(BINARY) $(OBJS) rsrc.o $(FLAGS)

# the rule that handles the resource file/object
rsrc.o : rsrc.rc
  windres rsrc.rc -o rsrc.o

# compiles all .cpp files
%.o : %.cpp *.h
  $(CXX) -o $@ -c $< -gstabs

:)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

X-G
Member #856
December 2000
avatar

You didn't actually SAY that, though. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

aybabtu
Member #2,891
November 2002

Okay. This is what I'm gonna do:

//Type my file.cpp!
//Type my filerc.rc!
//Or in the other order...
>windres filerc.rc -o filerc.o

>g++ -s -mwindows -O2 -O3 file.cpp -o file.exe

Now, this is the code I want to use. Can I link with resources in that second commandline? Or do I have to separate the compiler/linker commands?

EDIT: Whoa...you beat me...TWICE!
23: I have <i>NO</i> idea what that stuff means...;D:o

Carrus85
Member #2,633
August 2002
avatar

One suggestion-- don't use windows dialogs. It ruins the cross compatability between windows and linux. What exactly are you using the dialogs for anyway?

X-G
Member #856
December 2000
avatar

23, show him your generic makefile that automatically generates object targets from every .cpp file in the directory. :P

Oh, and supplying both -O2 and -O3 on the same commandline seems ... redundant at least.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

aybabtu: that doesn't link in the resource file ::)

Compile the rsrc.rc file, then the source code, then link them all. Three lines, like I posted above. Here's the whole makefile at X-G's request:

1# Generic Makefile.
2TEMP = $(wildcard *.cpp)
3FILES = $(if $(TEMP), $(TEMP), $(error No source code found!))
4OBJS = $(addsuffix .o, $(basename $(FILES)))
5 
6# binary name
7BINARY = Something.exe
8 
9# Flags used only by the C compiler
10CCFLAGS = -D__GTHREAD_HIDE_WIN32API -O2 -ffast-math -fomit-frame-pointer -funroll-loops
11# Flags used only by the linker
12LDFLAGS = -Wl,--subsystem,windows -lalleg -llua
13# compiler flags
14FLAGS = -Wall -s -O2 -ffast-math -fomit-frame-pointer -funroll-loops
15 
16# main rule; compiles and links everything
17$(BINARY) : $(OBJS) rsrc.o
18 $(CXX) -o $(BINARY) $(OBJS) rsrc.o $(FLAGS) $(LDFLAGS)
19 
20# the rule that handles the resource file/object
21rsrc.o : rsrc.rc
22 windres rsrc.rc -o rsrc.o
23 
24# compiles all .cpp files
25%.o : %.cpp *.h
26 $(CXX) -o $@ -c $< $(FLAGS) $(CCFLAGS)

I'm constructing this out of one makefile that uses proper flags and another that compiles a resource file, so let's hope this works ... and why would you use the command line over a makefile? Do you like typing out three lines (error free, I hope) every time you compile?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

aybabtu
Member #2,891
November 2002

Carrus: I'm learning the Win API...I thought I would since I now have a windows compiler!

GEEZ! Slow down people!
23: I wanted to give you guys the commandline I'll be using, so you could know how to tell me where to stick filerc.o!

X-G
Member #856
December 2000
avatar

You might want to fix up that linking line though - I doubt you'll want Lua all the time. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

aybabtu said:

tell me where to stick filerc.o!

;D

X-G: I know, I was still working on it ... how's it now?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

aybabtu
Member #2,891
November 2002

The question is now:
How do I link the resource.o file on the command line?

Carrus85
Member #2,633
August 2002
avatar

Learning the windows API is ok, just don't corrupt your winwolf project with foreign propretary windows "devils", so no one can test in on linux. :P

aybabtu
Member #2,891
November 2002

Yay! I got it! I did this:
1) Made an icon.
2) Made "resrc.rc", to use that icon
3) Made "resrc.h", with the #define
4) Compiled resrc.rc into resrc.o
5) Did this commandline:

g++ -s -mwindows res.cpp resrc.o -o res.exe

Yay! My program has an icon! Kewl...now I've just gotta continue on with my tutorial!

Go to: