[m-rev.] diff: more improvements to the Mercury calls C++ sample

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Oct 9 13:09:43 AEST 2003


Estimated hours taken: 0.25
Branches: main

Fix a bug where the Mercury calls C++ sample was not linking with the
C++ library.  This worked, because the sample didn't actually make use
of the C++ library, but failed as soon as users tried to extend the
sample to use with their real C++ programs.

samples/c_interface/mercury_calls_cplusplus/cpp_main.cc:
	Use the C++ standard library's stream I/O, rather than printf().

samples/c_interface/mercury_calls_cplusplus/Mmakefile:
	Make sure that we link with the C++ standard library.
	Also, comment out the old hack that worked around a g++ 2.7 bug
	which is fixed in g++ 2.95.

Workspace: /home/ceres/fjh/mercury
Index: samples/c_interface/mercury_calls_cplusplus/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/mercury/samples/c_interface/mercury_calls_cplusplus/Mmakefile,v
retrieving revision 1.5
diff -u -d -r1.5 Mmakefile
--- samples/c_interface/mercury_calls_cplusplus/Mmakefile	16 Dec 2001 05:22:20 -0000	1.5
+++ samples/c_interface/mercury_calls_cplusplus/Mmakefile	9 Oct 2003 03:04:51 -0000
@@ -14,17 +14,31 @@
 	$(CXX) $(HACK) $(CXXFLAGS) -c $<
 
 CXX=$(MGNUC) $(ALL_GRADEFLAGS) $(ALL_MGNUCFLAGS)
+CXXFLAGS=
+
+# We need to link in the C++ standard library.
+MLLIBS = -lstdc++
+
+# Another alternative, rather than explicitly linking in -lstdc++ like we
+# do above, would be to use g++ rather than gcc to do the linking, like
+# this:
+#ML =		MERCURY_C_COMPILER=g++ \
+#		MERCURY_LINK_SHARED_OBJ="g++ -shared" \
+#		ml
 
 # Due to a bug in g++ 2.7, global register variables don't work in C++.
 # Hence we need to ensure that we don't include the global register
 # variable declarations.  The following hack seems to do the trick.
-
-HACK=		-DMR_MACHDEPS_ALPHA_REGS_H \
-		-DMR_MACHDEPS_MIPS_REGS_H \
-		-DMR_MACHDEPS_SPARC_REGS_H \
-		-DMR_MACHDEPS_I386_REGS_H \
-		-DMR_MACHDEPS_PA_REGS_H \
-		-DMR_MACHDEPS_RS6000_REGS_H
+# This is commented out now because it should not be needed with
+# g++ 2.95 or later.
+#
+HACK=
+#HACK=		-DMR_MACHDEPS_ALPHA_REGS_H \
+#		-DMR_MACHDEPS_MIPS_REGS_H \
+#		-DMR_MACHDEPS_SPARC_REGS_H \
+#		-DMR_MACHDEPS_I386_REGS_H \
+#		-DMR_MACHDEPS_PA_REGS_H \
+#		-DMR_MACHDEPS_RS6000_REGS_H
 
 #-----------------------------------------------------------------------------#
 
Index: samples/c_interface/mercury_calls_cplusplus/cpp_main.cc
===================================================================
RCS file: /home/mercury1/repository/mercury/samples/c_interface/mercury_calls_cplusplus/cpp_main.cc,v
retrieving revision 1.1
diff -u -d -r1.1 cpp_main.cc
--- samples/c_interface/mercury_calls_cplusplus/cpp_main.cc	8 Oct 2003 12:15:44 -0000	1.1
+++ samples/c_interface/mercury_calls_cplusplus/cpp_main.cc	9 Oct 2003 02:59:44 -0000
@@ -1,6 +1,6 @@
 // This source file is hereby placed in the public domain.  -fjh (the author).
 
-#include <stdio.h>
+#include <iostream>
 
 #include "cpp_main.h"
 
@@ -8,5 +8,7 @@
 class Foo { };
 
 void cpp_main(void) {
-	printf("In cpp_main().\n");
+	Foo *p = new Foo;
+	std::cout << "In cpp_main()." << std::endl;
+	delete p;
 }

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list