Skip to content

Commit c99f35c

Browse files
committed
C++ & SWIG compilation working (tested on macOS)
1 parent 9f5fd68 commit c99f35c

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed

Makefile.in

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ INSTALL_LIBRARY = @INSTALL_LIBRARY@
9999
PACKAGE_NAME = @PACKAGE_NAME@
100100
PACKAGE_VERSION = @PACKAGE_VERSION@
101101
CC = @CC@
102+
CXX = @CXX@
103+
CCLD = @CCLD@
102104
CFLAGS_DEFAULT = @CFLAGS_DEFAULT@
103105
CFLAGS_WARNING = @CFLAGS_WARNING@
104106
EXEEXT = @EXEEXT@
@@ -179,12 +181,15 @@ LDFLAGS_DEFAULT = @LDFLAGS_DEFAULT@
179181
COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) \
180182
$(CFLAGS_DEFAULT) $(CFLAGS_WARNING) $(SHLIB_CFLAGS) $(CFLAGS)
181183

184+
CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) \
185+
$(CFLAGS_DEFAULT) $(CFLAGS_WARNING) $(SHLIB_CFLAGS) $(CXXFLAGS)
186+
182187
GDB = gdb
183188
VALGRIND = valgrind
184189
VALGRINDARGS = --tool=memcheck --num-callers=8 --leak-resolution=high \
185190
--leak-check=yes --show-reachable=yes -v
186191

187-
.SUFFIXES: .c .$(OBJEXT)
192+
.SUFFIXES: .c .$(OBJEXT) .cpp .cxx
188193

189194
#========================================================================
190195
# Start of user-definable TARGETS section
@@ -198,13 +203,13 @@ VALGRINDARGS = --tool=memcheck --num-callers=8 --leak-resolution=high \
198203
# for the BINARIES that you specified above have already been done.
199204
#========================================================================
200205

201-
all: @@SWIG_WRAP@@ binaries libraries
206+
all: @SWIG_WRAP@ binaries libraries
202207

203208

204209
wrap: $(SWIGOUTPUT)
205210

206211
$(SWIGOUTPUT): $(SWIGINTERFACE)
207-
$(SWIG) -tcl -c++ -namespace $(PACKAGE_NAME) -o $(SWIGOUTPUT) $(SWIGINTERFACE)
212+
$(SWIG) -tcl -c++ -namespace -pkgversion $(PACKAGE_VERSION) -module $(PACKAGE_NAME) -o $(SWIGOUTPUT) $(SWIGINTERFACE)
208213

209214
#========================================================================
210215
# The binaries target builds executable programs, Windows .dll's, unix
@@ -325,6 +330,10 @@ VPATH = $(srcdir):$(srcdir)/generic:$(srcdir)/unix:$(srcdir)/win:$(srcdir)/macos
325330

326331
.c.@OBJEXT@:
327332
$(COMPILE) -c `@CYGPATH@ $<` -o $@
333+
.cpp.@OBJEXT@:
334+
$(CXXCOMPILE) -c `@CYGPATH@ $<` -o $@
335+
.cxx.@OBJEXT@:
336+
$(CXXCOMPILE) -c `@CYGPATH@ $<` -o $@
328337

329338
#========================================================================
330339
# Distribution creation

configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ TEA_SETUP_COMPILER
7373
# and PKG_TCL_SOURCES.
7474
#-----------------------------------------------------------------------
7575

76-
TEA_ADD_SOURCES([cppsample.cpp])
77-
TEA_ADD_SWIGINTERFACE([cppsample.hpp])
76+
TEA_ADD_SOURCES([sample.cpp])
77+
TEA_ADD_SWIGINTERFACE([sample.hpp])
7878
TEA_ADD_HEADERS([])
7979
TEA_ADD_INCLUDES([])
8080
TEA_ADD_LIBS([])

generic/sample.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "sample.hpp"
2+
Animal::Animal(std::string type, int age) :
3+
type(type), age(age)
4+
{
5+
if (age < 0) {
6+
throw std::runtime_error("Age cannot be negative!");
7+
}
8+
}
9+
10+
int Animal::birthday() {
11+
return ++age;
12+
}
13+
14+
15+
std::string Animal::describe() {
16+
std::ostringstream description;
17+
description << "I am a "<<type<<" and I am "<<age<<" years old";
18+
return description.str();
19+
}
20+
21+

generic/sample.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef SAMPLE_HPP
2+
#define SAMPLE_HPP
3+
4+
#ifdef SWIG
5+
// SWIG directives
6+
// Include some standard SWIG library
7+
%include exception.i
8+
%include typemaps.i
9+
%include std_string.i
10+
%{
11+
// This goes into the wrap file. Include the declarations
12+
#include "sample.hpp"
13+
%}
14+
15+
// Convert C++ exceptions into Tcl excepions
16+
// (instead of crashing the program)
17+
%exception {
18+
try {
19+
$function
20+
} catch (const std::runtime_error &err) {
21+
SWIG_exception(SWIG_RuntimeError, err.what());
22+
} catch (...) {
23+
SWIG_exception(SWIG_RuntimeError, "Some C++-Error");
24+
}
25+
}
26+
#else
27+
28+
// C-preprocessor
29+
#include <string>
30+
#include <sstream>
31+
#include <stdexcept>
32+
33+
#endif //!SWIG
34+
35+
// Class definitions etc.
36+
37+
class Animal {
38+
public:
39+
std::string type;
40+
int age;
41+
42+
Animal(std::string type, int age);
43+
44+
int birthday();
45+
46+
std::string describe();
47+
48+
};
49+
#endif

tclconfig/tcl.m4

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ AC_DEFUN([TEA_CONFIG_CFLAGS], [
11661166
else
11671167
CCLD=$CC
11681168
fi
1169-
1169+
AC_SUBST(CCLD)
11701170
11711171
case $system in
11721172
# TEA specific:
@@ -3025,6 +3025,8 @@ AC_DEFUN([TEA_ADD_SWIGINTERFACE], [
30253025
# check for SWIG
30263026
AC_CHECK_PROG(SWIG_CHECK,swig,yes)
30273027
AS_IF([test x"$SWIG_CHECK" != x"yes"], [AC_MSG_ERROR([Swig is requred to compile this.])])
3028+
SWIG="swig"
3029+
30283030
# check for existence - allows for generic/win/unix VPATH
30293031
# To add more dirs here (like 'src'), you have to update VPATH
30303032
# in Makefile.in as well
@@ -3050,12 +3052,14 @@ AC_DEFUN([TEA_ADD_SWIGINTERFACE], [
30503052
fi
30513053
PKG_OBJECTS="$PKG_OBJECTS $SWIGOBJECT"
30523054
SWIG_WRAP="wrap"
3055+
TEA_ADD_CLEANFILES([$SWIGOUTPUT])
30533056
AC_SUBST(PKG_SOURCES)
30543057
AC_SUBST(PKG_OBJECTS)
30553058
AC_SUBST(SWIGOBJECT)
30563059
AC_SUBST(SWIGOUTPUT)
30573060
AC_SUBST(SWIGINTERFACE)
30583061
AC_SUBST(SWIG_WRAP)
3062+
AC_SUBST(SWIG)
30593063
])
30603064

30613065
#------------------------------------------------------------------------

0 commit comments

Comments
 (0)