Skip to content

Compiling R libraries on MAC OS X

3 messages · Jan de Leeuw, Stephen Eglen

#
Hi,
can anyone help me with the following problem?

[Using R-1.6.2 patched on Mac OS X 10.2.4, Developer tools from ~August?]

I have made four personal R libaries on Linux, and I'm trying to get
them to work on Mac OS X.  All of them have C code that is called from
R using .C().  Three out of the four work fine, but when I try to
compile one of them (a Voronoi library, based on Steve Fortune's code)
ld complains.  

% R CMD check sjevor

* checking for working latex ... OK
* using log directory '/Users/stephen/langs/R/sjevor.Rcheck'
* checking for file 'sjevor/DESCRIPTION' ... OK
* checking if this is a source package ... OK
* Installing *source* package 'sjevor' ...
** libs
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c edgelist.c -o edgelist.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c geometry.c -o geometry.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c heap.c -o heap.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c main.c -o main.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c memory.c -o memory.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c output.c -o output.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c sjevor.c -o sjevor.o
gcc -no-cpp-precomp -I/usr/local/lib/R/include  -I/sw/include -I/usr/local/include  -g -fno-common  -g -O2 -c voronoi.c -o voronoi.o
gcc -bundle -flat_namespace -undefined suppress -L/sw/lib -L/usr/local/lib -o sjevor.so edgelist.o geometry.o heap.o main.o memory.o output.o sjevor.o voronoi.o   
ld: multiple definitions of symbol _ELhash
edgelist.o definition of _ELhash in section (__DATA,__common)
geometry.o definition of _ELhash in section (__DATA,__common)
ld: multiple definitions of symbol _ELhashsize
edgelist.o definition of _ELhashsize in section (__DATA,__common)
geometry.o definition of _ELhashsize in section (__DATA,__common)
ld: multiple definitions of symbol _ELleftend
...

ld produces many similar complaints, before the build fails.

Has anyone else come across this problem? The package builds fine on
my linux box.

Thanks for any pointers,
Stephen
#
If you use flat namespaces you cannot have multiple definitions of  
symbols,
because the linked application has no way of knowing which definition
you meant. There are a number of ways to deal with this. You can use
two-level namespaces (which give warnings for multiple definitions)
You can change the C code such that all definitions but one use extern  
(this is best). And you can force the link by adding the
-m flag, which also transforms errors to warnings, i.e.

        
On Wednesday, Feb 26, 2003, at 15:31 US/Pacific, Stephen Eglen wrote:

            
===
Jan de Leeuw; Professor and Chair, UCLA Department of Statistics;
Editor: Journal of Multivariate Analysis, Journal of Statistical  
Software
US mail: 9432 Boelter Hall, Box 951554, Los Angeles, CA 90095-1554
phone (310)-825-9550;  fax (310)-206-5658;  email: deleeuw@stat.ucla.edu
homepage: http://gifi.stat.ucla.edu
   
------------------------------------------------------------------------ 
-------------------------
           No matter where you go, there you are. --- Buckaroo Banzai
                    http://gifi.stat.ucla.edu/sounds/nomatter.au
   
------------------------------------------------------------------------ 
-------------------------
#
Jan de Leeuw writes:
 > If you use flat namespaces you cannot have multiple definitions of  
 > symbols,
 > because the linked application has no way of knowing which definition
 > you meant. There are a number of ways to deal with this. You can use
 > two-level namespaces (which give warnings for multiple definitions)
 > 
 > > gcc -bundle -bundle-loader /usr/local/lib/R/bin/R.bin -undefined error  
 > > -L/sw/lib -L/usr/local/lib -o sjevor.so edgelist.o geometry.o heap.o  
 > > main.o memory.o output.o sjevor.o voronoi.o
 > 
 > You can change the C code such that all definitions but one use extern  
 > (this is best). And you can force the link by adding the
 > -m flag, which also transforms errors to warnings, i.e.
 > 
 > > gcc -bundle -flat_namespace -undefined suppress -Xlinker -m -L/sw/lib  
 > > -L/usr/local/lib -o sjevor.so edgelist.o geometry.o heap.o main.o  
 > > memory.o output.o sjevor.o voronoi.o

Thanks very much Jan for your help.  I have updated the C code so that
the global variables are defined once, and then "extern"ed in other
files that need them.  This works very well.

Stephen