Author Topic: Using Makefiles with CB article  (Read 5957 times)

Offline Gavrillo

  • Single posting newcomer
  • *
  • Posts: 7
Using Makefiles with CB article
« on: May 18, 2010, 04:59:42 pm »
Hi -  I'm not sure if I'm in the right place, but I've written an article for the wiki.  On the wiki it suggests that you have the article reviewed before submitting it so here it is.  I'm new to CB so if I am in the wrong place perhaps some one will be kind enough to direct me to the right one.  Otherwise remarks and corrections please.


Using makefiles with code::blocks

CB does not, by default,  use makefiles it has its own .cbp files which do the same thing automatically.  There are a few reasons that you might want to use a makefile.  One is if you are migrating  a project that has a makefile to CB (? - is this true - I've never done this)  the other is if you think you might want to take the project out of CB.

Another reason maybe that you need to use a pre-processor for instance one supplied by a database program.  This case is actually covered by CB  from the menu project/build options there appears a tab with pre/post build steps that can be used for this purpose.

This document deals with makefiles using mingw32-make 3.81, CB 8.02 and Wxwidgets 2.8 on Windows Vista, although I’m sure most of it will apply to other configurations.

If you decide that you want to use your own makefile, you only need enter the screen from project/build options and you will see a tick box for 'this is a custom makefile'.  Tick this box, make sure the name just above it is the one you want for your makefile.

You should also look at project/ build options.  There is a tab called 'Make commands' (you have to scroll the tabs horizontally to get to it).  In the field 'build project/target' you should see the line $make -f $makefile $target.   Assuming you're in debug mode $target will probably be called 'debug' which is probably not what you want.  You should change $target to your output file's name  (with the .exe extension and without the leading $).

One other useful addition is in  Project/Project Tree/Edit File types and categories.  If you add makefiles with the mask *.mak (CB seems to prefer .mak to .mk) you will be able to add your makefile with the extension .mak to the project and it will appear in the project management pane on the left.

Assuming you are going to edit the makefile within CB you should make sure that the editor uses tabs (as opposed to spaces).  This is a generic problem with make as it needs to start command lines with a tab character and many editors replace tabs with spaces.  To set this in CB, open the settings/editor window and check the tickbox  for use tab character.

The real problems start now however.  CB’s automatic makefile adds all the headers for Wxwidgets, but if you use a makefile, all this is turned off and you have to do this yourself.

Fortunately CB has another feature that can come to your rescue.  If you go to the menu ‘Settings’ ,  choose the option Compiler and Debugger,  scroll the tabs horizontally to the right end and you will find the tab ‘other settings’.  In there click on the tick box for ‘Save build to HTML …’. 

If you compile (without using a makefile – so if you’ve already reset everything –sorry) the default Wxwidgets  minimum program, you can see the compiler and linker commands that produce this file.

Assuming that you are going to use this as the basis for your project, you can use the content of the HTML file produced as the basis of your makefile.

You can’t just copy it from the HTML viewer in CB (there’s no such facility in CB) but you can load the file into a browser or editor and copy it from there.  It will be in your project directory with the_same_name_as_your_project_built_log.HTML.   Sadly it will require a little tweaking as shown below.

Here’s a copy of a build file for the basic Wxwidgets program as described above:
 

mingw32-make.exe -f test.mak test.exe
mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Wall -g -D__WXDEBUG__ -IC:\PF\wxWidgets2.8\include -IC:\PF\wxWidgets2.8\contrib\include -IC:\PF\wxWidgets2.8\lib\gcc_dll\mswud -c C:\Development\test\testApp.cpp -o obj\Debug\testApp.o
mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Wall -g -D__WXDEBUG__ -IC:\PF\wxWidgets2.8\include -IC:\PF\wxWidgets2.8\contrib\include -IC:\PF\wxWidgets2.8\lib\gcc_dll\mswud -c C:\Development\test\testMain.cpp -o obj\Debug\testMain.o
windres -IC:\PF\wxWidgets2.8\include -IC:\PF\wxWidgets2.8\contrib\include -IC:\PF\wxWidgets2.8\lib\gcc_dll\mswud -iC:\Development\test\resource.rc -o obj\Debug\resource.coff
mingw32-g++.exe -LC:\PF\wxWidgets2.8\lib\gcc_dll -o bin\Debug\test.exe obj\Debug\testApp.o obj\Debug\testMain.o obj\Debug\resource.coff -lwxmsw28ud -mwindows
Process terminated with status 0 (0 minutes, 12 seconds)
0 errors, 0 warnings

The above does not have any compilation for the .res file so you have to add this yourself, but with minimum tweaks, you can convert the build log into a makefile like the one below.  I have deliberately made it fairly close to the HTML file output.
 


# test program makefile

Incpath1 = C:\PF\wxWidgets2.8\include
Incpath2 = C:\PF\wxWidgets2.8\contrib\include
Incpath3 = C:\PF\wxWidgets2.8\lib\gcc_dll\mswud

Libpath = C:\PF\wxWidgets2.8\lib\gcc_dll

flags = -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Wall -g -D__WXDEBUG__

CXX = mingw32-g++.exe

test.exe : obj\Debug\testApp.o obj\Debug\testMain.o obj\Debug\resource.coff
               $(CXX) -L$(Libpath) -o bin\Debug\test.exe obj\Debug\testApp.o obj\Debug\testMain.o obj\Debug\resource.coff -lwxmsw28ud -mwindows

obj\Debug\testMain.o : C:\Development\test\testMain.cpp
                  $(CXX) $(flags) -I$(Incpath1) -I$(Incpath2) -I$(Incpath3) -c C:\Development\test\testMain.cpp -o obj\Debug\testMain.o

obj\Debug\testApp.o : C:\Development\test\testApp.cpp
                  $(CXX) $(flags) -I$(Incpath1) -I$(Incpath2) -I$(Incpath3) -c C:\Development\test\testApp.cpp -o obj\Debug\testApp.o

obj\Debug\resource.coff : C:\Development\test\resource.rc
                  windres -I$(Incpath1) -I$(Incpath2) -I$(Incpath3) -iC:\Development\test\resource.rc -oobj\Debug\resource.coff

# original output from codeblocks compilation
# note I've had to add compiling the .res file
#
# mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Wall -Wall -g -D__WXDEBUG__
# -Wall -g -IC:\PF\wxWidgets2.8\include -IC:\PF\wxWidgets2.8\contrib\include -IC:\PF\wxWidgets2.8\lib\gcc_dll\mswud
# -c C:\Development\test\testApp.cpp -o obj\Debug\testApp.o

# mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Wall -Wall -g -D__WXDEBUG__
# -Wall -g -IC:\PF\wxWidgets2.8\include -IC:\PF\wxWidgets2.8\contrib\include -IC:\PF\wxWidgets2.8\lib\gcc_dll\mswud
# -c C:\Development\test\testMain.cpp -o obj\Debug\testMain.o

# mingw32-g++.exe -LC:\PF\wxWidgets2.8\lib\gcc_dll -o bin\Debug\test.exe obj\Debug\testApp.o obj\Debug\testMain.o
# obj\Debug\resource.res -lwxmsw28ud -mwindows


I have written a generic makefile  which I have only tested on Windows Vista but that should work with any project started as described above.  You have to change the name of the project and set the paths as appropriate (you will probably only need to change Ppath and WXpath).


# Generic program makefile
# -- assumes that you name your directory with same name as the project file
# -- eg project test is in <development path>\test\

# Project name and version
Proj := test
Version := Debug

#paths for Project (Ppath) Object files (Opath) and binary path (Bpath)
Ppath := C:\Development\$(Proj)
Opath := obj\$(Version)
Bpath := bin\$(Version)

#Library & header paths
WXpath := C:\PF\wxWidgets2.8
IncWX := $(WXpath)\include
IncCON := $(WXpath)\contrib\include
IncMSW := $(WXpath)\lib\gcc_dll\mswud
Libpath := $(WXpath)\lib\gcc_dll

flags = -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Wall -g -D__WXDEBUG__

CXX = mingw32-g++.exe

Obj := $(Opath)\$(Proj)App.o $(Opath)\$(Proj)Main.o $(Opath)\resource.coff

$(Proj).exe : $(Obj)
               $(CXX) -L$(Libpath) -o $(Bpath)\$(Proj).exe $(Obj) -lwxmsw28ud -mwindows

$(Opath)\$(Proj)Main.o : $(Ppath)\$(Proj)Main.cpp
                  $(CXX) $(flags) -I$(IncWX) -I$(IncCON) -I$(IncMSW) -c $^ -o $@

$(Opath)\$(Proj)App.o : C:\Development\$(Proj)\$(Proj)App.cpp
                  $(CXX) $(flags) -I$(IncWX) -I$(IncCON) -I$(IncMSW) -c $^ -o $@

$(Opath)\resource.coff : C:\Development\$(Proj)\resource.rc
                  windres -I$(IncWX) -I$(IncCON) -I$(IncMSW) -i$^ -o $@

.PHONEY : clean

clean:
         del $(Bpath)\$(Proj).exe $(Obj) $(Opath)\resource.coff


 

One final remark.  Once you have used a makefile, attempts to compile anything without a makefile result in a number of warnings.  The only way I have found of solving this is to reinstall CB.  If you need both perhaps it is possible to install 2 versions of CB.

In general it is not recommended to use a makefile, but if there is some compelling reason to do so, then this is how.

« Last Edit: May 19, 2010, 10:50:02 pm by Gavrillo »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Using Makefiles with CB article
« Reply #1 on: May 18, 2010, 06:13:11 pm »
I did NOT see you state the version of Code::Blocks Used?
Since, you are using MakeFile you should also state the version of make used.

Note, Adding how to use CMake Generated makefile would be a good part two of the article.

Quote
CB does not use makefiles it has its own .cbp files which do the same thing automatically.
The above is not the real truth; the below is closer

Quote
CB, by default does not use makefiles, it has its own .cbp files which do the same thing automatically.
And, Adding the note somewhere that makefiles requires more manual work; because the project templates does not support makefiles.

I suggest reading the Code::Blocks user manual, I have never done so myself. It did not exist when I started.
http://www.codeblocks.org/user-manual

Tim S.
 


C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Gavrillo

  • Single posting newcomer
  • *
  • Posts: 7
Re: Using Makefiles with CB article
« Reply #2 on: May 19, 2010, 09:31:16 pm »
Thanks Tim S.   I've made the suggested changes.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2750
Re: Using Makefiles with CB article
« Reply #3 on: May 20, 2010, 03:11:00 pm »
Thanks Gavrillo, nice article.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Using Makefiles with CB article
« Reply #4 on: May 20, 2010, 05:24:14 pm »
Thanks Gavrillo, nice article.
Dito.

Can you put this in the WiKi, too? I think that would be the best place.

If you have done so, can you post a link to the page here, please?
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Gavrillo

  • Single posting newcomer
  • *
  • Posts: 7
Re: Using Makefiles with CB article
« Reply #5 on: May 22, 2010, 12:47:33 am »
I've managed to put the article on the Wiki. 

Link is:
http://wiki.codeblocks.org/index.php?title=Code::Blocks_and_Makefiles

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Using Makefiles with CB article
« Reply #6 on: May 22, 2010, 07:26:03 am »
I've managed to put the article on the Wiki. 
Thanks. :-) It will be persistent there.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ