bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Compiling TCL

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Tools: Bluesim
View previous topic :: View next topic  
Author Message
jeffc83



Joined: 05 Mar 2012
Posts: 5

PostPosted: Tue Mar 18, 2014 7:09 pm    Post subject: Compiling TCL Reply with quote

Hi, I'm using SWIG to wrap some C++ functions for use while debugging my design in the bluetcl interpreter. I'm having trouble getting it to link and run correctly. Correct linking requires me to add tclstub8.5 and tcl to the linker command. Bluetcl then dies when I try to load the library.


Build commands

# generate the C++ wrapper in intersection_wrap.cxx
swig -tcl -c++ intersection.i

# build the .so file
g++ -fPIC -I/usr/local/Bluespec/lib/tcllib/include -c intersection.cpp intersection_wrap.cxx
g++ intersection.o intersection_wrap.o -o intersection.so -L/usr/local/Bluespec/lib/tcllib/lib/linux64 -ltclstub8.5 -ltcl


# now run bluetcl
bluetcl
%load intersection.so intersection

*** glibc detected *** /usr/local/Bluespec/lib/bin/linux64/bluetcl: free(): invalid next size (fast): 0x0000000002545810 ***


I think the problem is related to including the right versions of the TCL libraries (I have 8.4 and 8.5 on my system and Bluespec seems to have a few libraries of its own, though no copy of libtcl that I could find). Any pointers?

Thanks

Jeff
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 496

PostPosted: Wed Mar 19, 2014 1:02 pm    Post subject: Re: Compiling TCL Reply with quote

It looks like you've found the directory ${BLUESPECDIR}/tcllib/lib/linux64/. This directory not only contains the library libtclstub8.5.a, but also libtcl8.5.a. So you might want to link against the name "tcl8.5" and not "tcl".

Also, I think the way you're linking only works for .so files, so it's probably finding those libraries elsewhere on your system. For static libraries like these, you can just provide the full path on the command line. I imagine you'd want to try this:
Code:
g++ intersection.o intersection_wrap.o -o intersection.so \
   /usr/local/Bluespec/lib/tcllib/lib/linux64/libtclstub8.5.a \
   /usr/local/Bluespec/lib/tcllib/lib/linux64/libtcl8.5.a

Does that work?
Back to top
View user's profile Send private message
jeffc83



Joined: 05 Mar 2012
Posts: 5

PostPosted: Wed Mar 19, 2014 2:40 pm    Post subject: Reply with quote

Hi, quark. The linker switches you have make sense, but my install (Bluespec 2012.01.A build 26572) does not appear to have the file libtcl8.5.a.

I searched in:

..... /tcllib/lib.linux64
.../tcllib/tcl8.5

My system does have its own version in /usr/lib (both versions 8.4 and 8.5) - have tried linking against both of those but still get the segfault. I assume there's a subtle difference between those and the version packaged with Bluespec.

Can you send or point me to the missing .a file?

Thanks,

Jeff
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 496

PostPosted: Wed Mar 19, 2014 3:43 pm    Post subject: Reply with quote

It occurs to me that you shouldn't need both libtcl and libtclstubs. In this case, you should only need the stubs, since you plan to load the object file into bluetcl (which is already linked with the real thing). So if you provide the stubs on the command, like this, does it work?
Code:
g++ intersection.o intersection_wrap.o -o intersection.so \
   /usr/local/Bluespec/lib/tcllib/lib/linux64/libtclstub8.5.a
Back to top
View user's profile Send private message
jeffc83



Joined: 05 Mar 2012
Posts: 5

PostPosted: Wed Mar 19, 2014 11:29 pm    Post subject: Reply with quote

Unfortunately not. Commands:

# From the Makefile
%.so: %.i %.cpp
swig -tcl -c++ $*.i
g++ -fPIC -I/usr/local/Bluespec/lib/tcllib/include -c $*.cpp $*_wrap.cxx
g++ -shared $*.o $*_wrap.o /usr/local/Bluespec/lib/tcllib/lib.linux64/libtclstub8.5.a -o [email protected]

# then run from shell
[email protected]:~/src/FullMonte/modules/Intersection$ bluetcl
% load intersection.so intersection
couldn't load file "intersection.so": ./intersection.so: undefined symbol: Tcl_GetStringFromObj
%


Tcl_GetStringFromObj is not present in the stubs file (in fact, stubs has very little in it). That function is defined in /usr/lib/libtcl.a but that doesn't seem to help since when I link against that one I get the error on double-free, probably due to conflicting symbols.
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 496

PostPosted: Thu Mar 20, 2014 11:22 am    Post subject: Reply with quote

A couple of suggestions:

One, are you able to compile and load your object into the plain tclsh on your system?

Two, when linking with tclstub, you need to also set -DUSE_TCL_STUBS during the compilation.

Three, however, I suspect that you don't need to link against either tcl or tclstub. What happens if you just compile without any tcl library and then try to load it? (Looking at the Swig documentation, they only suggest the tcl flags when creating a static tcl executable, not when creating a loadable module.)
Back to top
View user's profile Send private message
jeffc83



Joined: 05 Mar 2012
Posts: 5

PostPosted: Fri Mar 21, 2014 10:55 am    Post subject: Reply with quote

Resolved! Linking the stubs and using -DUSE_TCL_STUBS were the keys.
Thank you, quark.

For those who don't want to read the whole exchange:

Situation: Have C++ code which I want to wrap in Tcl (in this case using SWIG) for use in Bluesim

Problems: (1) Bluetcl fails to load because missing Tcl_GetStringFromObject or (2) Bluetcl attemps to load but segfaults or bombs due to a double-free

Causes: (1) shared library is missing necessary hooks for Tcl interpreter to load it or (2) hooks are there but it's the wrong library version and/or two library versions with symbol clashes

Solution: Link with Bluespec-provided tclstub8.5.a and #define USE_TCL_STUBS when compiling your code

Code:

#use SWIG to create a Tcl wrapper for my C++ code
swig -tcl -c++ mycode.i

# compile the code & wrapper to .o file
g++ -fPIC -DUSE_TCL_STUBS -I/usr/local/Bluespec/lib/tcllib/include -c mycode.cpp mycode_wrap.cxx

# link .o files with Bluespec tcl stubs into shared library
g++ -shared mycode.o mycode_wrap.o /usr/local/Bluespec/lib/tcllib/lib.linux64/libtclstub8.5.a -o mycode.so
bluetcl

# run it
% load ./mycode.so mycode
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Tools: Bluesim All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP