OT: Portable UNIX makefile for C++

P

Peter Smithson

Hi,

I did a search for this and other than GNU specific replies, I couldn't
find the answer. I know it's not really a C++ question but I don't
know of a group for makefiles for C++ and people on this list must have
come across the problem.

It's really simple - how do I write a makefile which will work on
Solaris, HPUX and Linux using the "make" that comes with the OS. It'd
be nice if it worked on AIX, SCO OS, Unixware etc. too.

One nice looking solution I found was this -

%.o: %.cpp
g++ -I$(INCLUDES) -c $<

But I think that requires the GNU make as it didn't work on Solaris.

I've currently got this. It works on all platforms but it's a pain -

clist.o: clist.cpp
$(CXX) $(CFLAGS) -c -o $@ clist.cpp

$(LIB)(clist.o):clist.o
$(AR) -rv $(LIB) clist.o

$(LIB): \
$(LIB)(clist.o)\
etc.

I know one answer is to always use gmake but it's just one more step
when you do a port (get gmake built) so I'd rather avoid it.

Thanks.

Peter
 
R

REH

Peter Smithson said:
Hi,

I did a search for this and other than GNU specific replies, I couldn't
find the answer. I know it's not really a C++ question but I don't
know of a group for makefiles for C++ and people on this list must have
come across the problem.

It's really simple - how do I write a makefile which will work on
Solaris, HPUX and Linux using the "make" that comes with the OS. It'd
be nice if it worked on AIX, SCO OS, Unixware etc. too.

One nice looking solution I found was this -

%.o: %.cpp
g++ -I$(INCLUDES) -c $<

But I think that requires the GNU make as it didn't work on Solaris.

Yes, because that is the pattern rule available in GNU Make. If you really
want to use Solaris' Make instead of GNU (don't know why you would), use
suffix rules which most (all?) make tools should understand. For the above:

..cpp.o:
g++ -I$(INCLUDES) -o $@ -c $<


You should read: http://make.paulandlesley.org/rules.html

I have to have makefile that work on Linux, Solaris, Windows, and DOS. This
info helped a lot.

REH
 
B

Bushido Hacks

Well, while I do not have a solution for creating a make file, I did
make a couple batch files that I hope to use with MSYS
(http://www.mingw.org/).

These scripts should work with any version of GCC ported to Windows
(Dev-C++, GCC, or MINGW)

Let \ represent the top level directory. I use H:\ (yeah, you can
install GCC to a thumbdrive. Good thing for the on the go programmer
;-) ) Do not write H:\ in the script. Just use \ so that you can use
it on any drive letter.
Let \bin represent the path to the program c++.exe. Add the full path.

In \, create the file C++.BAT
in it write:
IF EXIST %1.o DEL %1.o
\bin\c++ -c %1.cpp %1.o

In \, create the file LINK.BAT
The full script I created for link is rather extensive, but I will show
what it looks like for linking 2 .o files.

In LINK.BAT write:

IF EXIST %2.exe DEL %2.exe
\bin\c++ -Wl--startgroups %1.o %2.o -Wl--endgroup -o %2.exe
IF NOT EXIST %2.exe GOTO err
\bin\strip %2.exe
:err

strip was added to implement strip.exe which will help unbloat the new
program in Windows.

To use c++.bat:
c++ file

NOT file.cpp!

To use link.bat:
link file1 file2

Again do not add the suffixes.

I believe it is possible to make a make program from these two scripts.

For now, I'd like to know what these scripts would look like as shell
scripts instead of batch files?
 
P

Peter Smithson

REH said:
Yes, because that is the pattern rule available in GNU Make. If you really
want to use Solaris' Make instead of GNU (don't know why you would), use
suffix rules which most (all?) make tools should understand. For the above:

.cpp.o:
g++ -I$(INCLUDES) -o $@ -c $<

Thanks but no luck -
make: Fatal error: Don't know how to make target `clist.o'

I had to add the $(LIB)(clist.o):clist.o rule when I ported to Solaris
to make it realise how to build the module - I'm not sure why.
You should read: http://make.paulandlesley.org/rules.html

I have to have makefile that work on Linux, Solaris, Windows, and DOS. This
info helped a lot.

Hmmm - well it's pretty easy if you use GNU make (step 1 in the URL you
gave me) but I want to avoid the step of getting gnu make every time I
port to a machine. I'm often limited for time or using a customers
machine etc.

I didn't think it would be too hard - someone must use make on Solaris,
AIX etc. for C++?

Cheers

Peter
 
P

Peter Smithson

For now, I'd like to know what these scripts would look like as shell
scripts instead of batch files?

Well - pretty similar but you'd normally use make on UNIX.

IF EXIST %1.o DEL %1.o
\bin\c++ -c %1.cpp %1.o

would be

if [ -f $1.o ]
then
rm $1.o
fi
g++ -c $1.cpp

similar of the other one. There's no real need for the first 3 lines
though.

Cheers

Petre
 
R

REH

Peter Smithson said:
Thanks but no luck -
make: Fatal error: Don't know how to make target `clist.o'
Two things:
1) .cpp and .o MUST be defined as dependencies to the pseudo target
..SUFFIXES
2) there must be a clist.cpp file

I suggest you get a good book on make, like O'Reilly's "Managing Projects
with Make." The latest edition only covers GNU Make, but the 2nd edition
covers make versions and their differences.
I had to add the $(LIB)(clist.o):clist.o rule when I ported to Solaris
to make it realise how to build the module - I'm not sure why.

If you had to do this then you have defined clist.o in archive $(LIB) in
your makefile. I find this method of creating archives more thouble than
it's worth, besides being less efficient (files are added to the archive
individually).
Hmmm - well it's pretty easy if you use GNU make (step 1 in the URL you
gave me) but I want to avoid the step of getting gnu make every time I
port to a machine. I'm often limited for time or using a customers
machine etc.

I didn't think it would be too hard - someone must use make on Solaris,
AIX etc. for C++?

I use Solaris for C++ everyday. If you stick to suffix rules for your
implicit targets, and "standard" implicit variables, you should be OK. I
would also avoid archive member targets (above) as these are support
different or not at all in various incarnations of make.
 
P

Peter Smithson

..SUFFIXES - it's all coming back. I used to know this stuff about 10
years ago. You're right - I should really RTFM. I'm still having no
luck on Solaris even with .SUFFIXES in there but then I think this
particular Solaris box isn't very well anyway -

$ echo abc|tr '[A-Z]' '[a-z]'
bcd
$ clear
ld.so.1: clear: fatal: libncurses.so.5: open failed: No such file or
directory
Killed


But this is getting even more off topic!

Cheers

Peter
 
P

Peter Smithson

Right - this is it -

..SUFFIXES:.cpp

..cpp.o:

$(CXX) -I$(INCLUDES) -o $@ -c $<

..cpp.a:
$(CXX) $(CXXFLAGS) -c -o $% $<
$(AR) $(ARFLAGS) $@ $%
$(RM) $%

I still managed to avoid reading the manual (almost). I did "make -DD"
to show the built in rules and looked for the Solaris standard c++
extension .cc and found what was defined for that.

Just got to test it out on other platforms now.

Cheers

Peter
 
B

Bushido Hacks

Thanks Peter.

The reason why I delete the .o file everytime is so that I can have a
fresh start if there are any errors.
 
G

Greg Schmidt

Thanks Peter.

The reason why I delete the .o file everytime is so that I can have a
fresh start if there are any errors.

Forgive me if I'm being dense. It's later than I normally post. If
there are any errors, would you not edit the source file to fix them?
If so, any decent makefile will handle the dependency checking,
recognize that the .o is older than the .c, and rebuild it. That's kind
of one of the main points of having a makefile, after all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top