Skip to content

Commit

Permalink
Runtime (#15)
Browse files Browse the repository at this point in the history
* Add cpptcl_runtime

It can be difficult to get the headers, tcl.h and cpptcl.h
in correct order and settings when creating the first interpreter.
The new global function
   Tcl_CreateInterpWithStubs(const char *version, int exact)
make this easier as the library, cpptcl_runtime, is compiled without
stubs.

cpptcl end users can now get tcl.h from cpptcl.h.  They can also
create interpreters by changing

Tcl_CreateInterp() -> Tcl_CreateInterpWithStubs()

and they can remote the initialization calls.

* Add Tcl_CreateInterpWithStubs

Add a helper function for TCL interpreter creation.

* Change to static library.

cpptcl_runtime is now static library.
Add missing PIC flags to link.  No effect for static, but forgetting that
is a hassle.
  • Loading branch information
snoe925 authored Dec 12, 2018
1 parent f31eb4b commit 996f40e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ install(TARGETS cpptcl_static ARCHIVE DESTINATION lib)
install(FILES ${HDRS} DESTINATION include/cpptcl)
install(FILES ${HDRS_DETAILS} DESTINATION include/cpptcl/details)

add_library(cpptcl_runtime STATIC ${cpptcl_SOURCE_DIR}/cpptcl_runtime.c)
set_property(TARGET cpptcl_runtime PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(cpptcl_runtime ${TCL_LIBRARY})

add_subdirectory(test)
add_subdirectory(examples)
5 changes: 5 additions & 0 deletions cpptcl/cpptcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

#include "tcl.h"

// This function is not part of TCL, but is a useful helper.
extern "C" {
Tcl_Interp * Tcl_CreateInterpWithStubs(const char *version, int exact);
};

namespace Tcl {

// exception class used for reporting all Tcl errors
Expand Down
14 changes: 14 additions & 0 deletions cpptcl_runtime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <tcl.h>

Tcl_Interp * Tcl_CreateInterpWithStubs(const char *version, int exact)
{
Tcl_Interp *interp = Tcl_CreateInterp();

if (Tcl_Init(interp) == TCL_ERROR
|| Tcl_InitStubs(interp, version, exact) == NULL
|| Tcl_PkgRequire(interp, "Tcl", version, exact) == NULL) {
return NULL;
}

return interp;
}
4 changes: 2 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.0)

add_executable(example2 example2.cc)
target_link_libraries(example2 PUBLIC cpptcl_static ${TCL_STUB_LIBRARY} ${TCL_LIBRARY})
target_link_libraries(example2 PUBLIC cpptcl_static cpptcl_runtime ${TCL_STUB_LIBRARY} ${TCL_LIBRARY})

add_executable(example6 example6.cc)
target_link_libraries(example6 PUBLIC cpptcl_static ${TCL_STUB_LIBRARY} ${TCL_LIBRARY})
target_link_libraries(example6 PUBLIC cpptcl_static cpptcl_runtime ${TCL_STUB_LIBRARY} ${TCL_LIBRARY})

add_library(libcpptcl_module_two SHARED cpptcl_module_two.cc)
target_link_libraries(libcpptcl_module_two PUBLIC cpptcl_static ${TCL_STUB_LIBRARY})
Expand Down
5 changes: 1 addition & 4 deletions examples/example2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <string>


#include "tcl.h"
#define CPPTCL_NO_TCL_STUBS
#include "cpptcl/cpptcl.h"

using namespace std;
Expand All @@ -14,8 +12,7 @@ using namespace Tcl;
void hello() { cout << "Hello C++/Tcl!" << endl; }

int main() {
Tcl_Interp * interp = Tcl_CreateInterp();
Tcl_InitStubs(interp, "8.0", 0);
Tcl_Interp * interp = Tcl_CreateInterpWithStubs("8.6", 0);
interpreter i(interp, true);
i.def("hello", hello);

Expand Down
9 changes: 3 additions & 6 deletions examples/example6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ using namespace std;
using namespace Tcl;

int main() {
Tcl_Interp * interp = Tcl_CreateInterp();
// Setup the stubs table pointer. TCL can load any extension with a shared library
// and that library will expect stubs support.
Tcl_InitStubs(interp, "8.0", 0);
interpreter i(interp, true);
Tcl_Interp * interp = Tcl_CreateInterpWithStubs("8.6", 0);
interpreter i(interp, true);

int numbers[] = {5, 7, 1, 6, 3, 9, 7};
int numbers[] = {5, 7, 1, 6, 3, 9, 7};
size_t elems = sizeof(numbers) / sizeof(int);

object tab;
Expand Down

0 comments on commit 996f40e

Please sign in to comment.