[gambit-list] Building loadable library

Marc Feeley feeley at iro.umontreal.ca
Thu May 12 08:56:07 EDT 2011


On 2011-05-12, at 1:39 AM, Kartik Saranathan wrote:

> I have the following setup
> 
> fileio.scm - contains c-lambda forms to bind C functions in fileio_util.cpp
> fileio_util.cpp - contains code that uses functions from gambit
> runtime library (data conversion functions like
> ___UTF_8STRING_to_SCMOBJ)
> 
> I'd like to make a loadable library from the above.  Here is what I tried
> 
>  bash-3.2$ uname -srmp
>  Darwin 10.7.0 i386 i386
>  gsc -link -flat -o search.o1.c fileio
>  g++    -no-cpp-precomp -Wno-unused -O1 -fno-math-errno
> -fschedule-insns2 -fno-trapping-math -fno-strict-aliasing -fwrapv
> -fomit-frame-pointer -fno-move-loop-invariants -fPIC -fno-common
> -mieee-fp   -D___SINGLE_HOST  -I"/usr/local/Gambit-C/include" -c -o
> "fileio_util.o" -D___DYNAMIC fileio_util.cpp
>  g++    -no-cpp-precomp -Wno-unused -O1 -fno-math-errno
> -fschedule-insns2 -fno-trapping-math -fno-strict-aliasing -fwrapv
> -fomit-frame-pointer -fno-move-loop-invariants -fPIC -fno-common
> -mieee-fp   -D___SINGLE_HOST  -I"/usr/local/Gambit-C/include" -c -o
> "search.o1.o" -D___DYNAMIC search.o1.c
>  g++    -no-cpp-precomp -Wno-unused -O1 -fno-math-errno
> -fschedule-insns2 -fno-trapping-math -fno-strict-aliasing -fwrapv
> -fomit-frame-pointer -fno-move-loop-invariants -fPIC -fno-common
> -mieee-fp   -D___SINGLE_HOST  -I"/usr/local/Gambit-C/include" -c -o
> "fileio.o" -D___DYNAMIC fileio.c
>  g++ -bundle fileio_util.o fileio.o sqlite3.o search.o1.o -o search.o1
> 
> Linker gives the following errors
> 
>  Undefined symbols:
>    "___S64_to_SCMOBJ(long, long*, int)", referenced from:
>        ConvertSqliteData(sqlite3_stmt*, int, int, long&)in fileio_util.o
>    "___UTF_8STRING_to_SCMOBJ(char*, long*, int)", referenced from:
>        ConvertSqliteData(sqlite3_stmt*, int, int, long&)in fileio_util.o
>        w_sqlite_prepare(sqlite3*, char*)in fileio_util.o
>    "___F64_to_SCMOBJ(double, long*, int)", referenced from:
>        ConvertSqliteData(sqlite3_stmt*, int, int, long&)in fileio_util.o
> 
> Since libgambc.a defines those symbols I tried liking with it.  Here
> is what I get.
> 
>  Undefined symbols:
>    "_environ", referenced from:
>        ___unsetenv_UCS_2(unsigned short*)in libgambc.a(os_shell.o)
>        ___cleanup_shell_module()     in libgambc.a(os_shell.o)
>        ___os_environ()     in libgambc.a(os_shell.o)
>        ___setenv_UCS_2(unsigned short*, unsigned short*)in
> libgambc.a(os_shell.o)
>        ___setenv_UCS_2(unsigned short*, unsigned short*)in
> libgambc.a(os_shell.o)
>        ___getenv_UCS_2(unsigned short*, unsigned short**)in
> libgambc.a(os_shell.o)
>        ___device_stream_setup_from_process(___device_stream_struct**,
> ___device_group_struct*, char**, char**, char*, int)in
> libgambc.a(os_io.o)
>       (maybe you meant:
> ____G__23__23_fail_2d_check_2d_unbound_2d_os_2d_environment_2d_variable_2d_exception,
> ____G__23__23_display_2d_dynamic_2d_environment_3f_ , ... )

My guess is that some C linker options are missing to generate a dynamically loadable library.  From the file bin/gambc-cc I see that, on a Mac, the build command for a dynamically loadable object file is:

${C_COMPILER} ${FLAGS_DYN} ${DEFS_DYN} -I"${GAMBCDIR_INCLUDE}" -o "${BUILD_DYN_OUTPUT_FILENAME}" ${BUILD_DYN_CC_OPTIONS} ${BUILD_DYN_LD_OPTIONS_PRELUDE} ${BUILD_DYN_INPUT_FILENAMES} ${BUILD_DYN_LD_OPTIONS}

and

FLAGS_DYN="-mmacosx-version-min=10.5 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -arch x86_64 -m64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch x86_64 -m64  -bundle  -no-cpp-precomp -Wno-unused -O1 -fno-math-errno -fschedule-insns2 -fno-trapping-math -fno-strict-aliasing -fwrapv -fomit-frame-pointer -fno-move-loop-invariants -fPIC -fno-common -mieee-fp   -flat_namespace -undefined suppress"

You can print that information with commands like:

% gsi -e '(current-directory "~~bin")(shell-command "./gambc-cc BUILD_DYN")'
% gsi -e '(current-directory "~~bin")(shell-command "./gambc-cc C_COMPILER")'
% gsi -e '(current-directory "~~bin")(shell-command "./gambc-cc FLAGS_DYN")'

so in your makefile you could have something like:

BUILD_DYN=`gsi -e '(current-directory "~~bin")(shell-command "./gambc-cc BUILD_DYN")'`
C_COMPILER=`gsi -e '(current-directory "~~bin")(shell-command "./gambc-cc C_COMPILER")'`
FLAGS_DYN=`gsi -e '(current-directory "~~bin")(shell-command "./gambc-cc FLAGS_DYN")'`
...
BUILD_DYN_INPUT_FILENAMES=fileio_util.o fileio.o sqlite3.o search.o1.o
BUILD_DYN_OUTPUT_FILENAME=search.o1

build_lib:
	${C_COMPILER} ${FLAGS_DYN} ${DEFS_DYN} -I"${GAMBCDIR_INCLUDE}" -o "${BUILD_DYN_OUTPUT_FILENAME}" ${BUILD_DYN_CC_OPTIONS} ${BUILD_DYN_LD_OPTIONS_PRELUDE} ${BUILD_DYN_INPUT_FILENAMES} ${BUILD_DYN_LD_OPTIONS}

I haven't tested this so YMMV.

> Also at first tried using gsc to run the compiler for me
> 
>  gsc -verbose -cc-options "-D___DYNAMIC" -obj fileio.c
> fileio_util.cpp search.o1.c
> 
> But I kept getting
> 
>  *** ERROR IN "fileio_util.cpp"@1.1 -- Invalid token
> 
> I realized it was treating the .cpp file as a scheme file and failing.
> Is this a bug or am I using it incorrectly?

The command-line processing of gsc treats files with a ".c" extension as C source files and other extensions as Scheme files.  This is probably too conservative.  I could add ".cpp" and ".cc" and ".m" as alternatives for C source files.  Any others I should add?

> How do I compile the
> above as a loadable library?
> 
> Thank you for Gambit.  The more I use it the more I like it.

No problem!  Thanks!

Marc




More information about the Gambit-list mailing list