Compiling -fPIC from object files

R

Robbie Brown

I realise this is a C forum but background is.

I'm interfacing with existing C code from Java using the Java native
interface.

I have my C code for which I have written a driver to test the program
and it all works as expected (usual caveats on 'expected' of course).

I have written the Java interface, generated the header file and written
and compiled the C interface that complies with the header

my makefile is thus

all: jnicalc

jnicalc: calculator.c calcdriver.c
gcc -shared -o libcalc.so -fPIC calculator.c calcdriver.c

Where calculator.c is the original unaltered C code and calcdriver.c is
the jni interface code.

This all works fine, I can call my original C code from the Java
interface and get the hoped for results.

The next step is to interface with C object code.

I tried the following

all: jnicalc

jnicalc: calculator calcdriver
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

calcdriver: calcdriver.o
gcc -std=c99 -pedantic -c calcdriver.c

calculator: calculator.c
gcc -std=c99 -pedantic -c calculator.c

But this fails, the point is I may not always have the code for the C
interfaces, only the Object files. How can I build my Java interface in
C when I don't have access to the original code, only precompiled object
files and header files. Does trying to do this even make sense?

Thanks






--
Rob - not floundering, fishing
========================================================
For every SCoP we generate the polyhedral representation
and transform it back to gimple.
========================================================
Seems perfectly reasonable to me.
 
S

Stephen Sprunk

I tried the following

all: jnicalc

jnicalc: calculator calcdriver
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

calcdriver: calcdriver.o
gcc -std=c99 -pedantic -c calcdriver.c

calculator: calculator.c
gcc -std=c99 -pedantic -c calculator.c

But this fails, the point is I may not always have the code for the
C interfaces, only the Object files. How can I build my Java
interface in C when I don't have access to the original code, only
precompiled object files and header files. Does trying to do this
even make sense?

AFAIK, everything linked into a shared object must have been compiled
with -fPIC. If you have object files (from some other source) that were
compiled without that, you can't include them.

OTOH, -fPIC is unnecessary when linking, which is the step where you
have applied it above.

S
 
R

Robbie Brown

I realise this is a C forum but background is.

I'm interfacing with existing C code from Java using the Java native
interface.

I have my C code for which I have written a driver to test the program
and it all works as expected (usual caveats on 'expected' of course).

I have written the Java interface, generated the header file and written
and compiled the C interface that complies with the header

my makefile is thus

all: jnicalc

jnicalc: calculator.c calcdriver.c
gcc -shared -o libcalc.so -fPIC calculator.c calcdriver.c

Where calculator.c is the original unaltered C code and calcdriver.c is
the jni interface code.

This all works fine, I can call my original C code from the Java
interface and get the hoped for results.

The next step is to interface with C object code.

Apologies, the next bit got a bit confused, I'll try again

all: jnicalc

jnicalc: calcdriver
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

calcdriver: calcdriver.o
gcc -std=c99 -pedantic -c calcdriver.c

Here I compile the calcdriver as I have the code but I don't have the
code for calculator.c (well I do but I compiled it separately, imagine I
only have the .o file)

I've tried various things to get this to work

Any advice much appreciated.


--
Rob - not floundering, fishing
========================================================
For every SCoP we generate the polyhedral representation
and transform it back to gimple.
========================================================
Seems perfectly reasonable to me.
 
K

Kaz Kylheku

all: jnicalc

jnicalc: calculator calcdriver
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

calcdriver: calcdriver.o
gcc -std=c99 -pedantic -c calcdriver.c

calculator: calculator.c
gcc -std=c99 -pedantic -c calculator.c

Your makefile is flawed.

This rule says that a jnicalc is made from a calculator and calcdriver:
jnicalc: calculator calcdriver
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

But, the build recipe actually does no such thing; it builds from
a calculator.o and calcdriver.o.

Similarly, your other rules have a similar problem.

What you want is this:
all: jnicalc

jnicalc: calculator.o calcdriver.o
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

calcdriver.o: calcdriver.c
gcc -std=c99 -pedantic -c calcdriver.c

calculator.o: calculator.c
gcc -std=c99 -pedantic -c calculator.c

Now if you don't have the .c files, but only the .o files, you can just take
out the last two rules that build the .o's.

(Don't have rules in your makefile to build stuff from other stuff, if you
already have stuff, and don't have the other stuff.)
 
R

Robbie Brown

On 2014-03-13, Robbie Brown <[email protected]> wrote:

What you want is this:


Now if you don't have the .c files, but only the .o files, you can just take
out the last two rules that build the .o's.


OK, I think I've got to the bottom of this now.

If I compile on the command line

gcc -std=c99 -c calcdriver.c
gcc -std=c99 -c calculator.c
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

This fails

If I try

gcc -std=c99 -fPIC -c calcdriver.c
gcc -std=c99 -fPIC -c calculator.c
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

Then I get my shared library

So, it looks like if the object file has been compiled with -fPIC then I
can use it to build my shared library, if it hasn't then I can't.

But of course nothing is this simple ... is it :-|


--
Rob - not floundering, fishing
========================================================
For every SCoP we generate the polyhedral representation
and transform it back to gimple.
========================================================
Seems perfectly reasonable to me.
 
G

Geoff

OK, I think I've got to the bottom of this now.

If I compile on the command line

gcc -std=c99 -c calcdriver.c
gcc -std=c99 -c calculator.c
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

This fails

If I try

gcc -std=c99 -fPIC -c calcdriver.c
gcc -std=c99 -fPIC -c calculator.c
gcc -shared -o libcalc.so -fPIC calculator.o calcdriver.o

Then I get my shared library

So, it looks like if the object file has been compiled with -fPIC then I
can use it to build my shared library, if it hasn't then I can't.

But of course nothing is this simple ... is it :-|

Yes, all modules must be compiled as -fPIC before the linker can make
it a shared library and makefile magical incantations are a world unto
themselves. Not only that, the Linux library tool is "ldd" but on OS X
it's "otool". So if you are coding makefiles across platforms you have
to test the shell to see which tools you need to select. Here's a
sample from a moderate sized project I am dealing with:

<code>
ARCH=i386

#use these cflags to optimize this build
CFLAGS=-O3 -m32 -march=$(ARCH) -DARCH="$(ARCH)"
#use these when debugging
#CFLAGS=-g -m32 -DARCH="$(ARCH)" -Wall

# flavors of Linux
ifeq ($(shell uname),Linux)
CFLAGS += -DLINUX
LIBTOOL = ldd
endif

# OS X wants to be Linux and FreeBSD too.
ifeq ($(shell uname),Darwin)
CFLAGS += -DLINUX
LIBTOOL = otool
endif

SHLIBEXT=so
#set position independent code
SHLIBCFLAGS=-fPIC

ORIGDIR=Source

DO_SHLIB_CC=$(CC) $(CFLAGS) $(SHLIBCFLAGS) -o $@ -c $<

..c.o:
$(DO_SHLIB_CC)

# more stuff goes here...
</code>

The SHLIBCFLAGS could have been left out and the -fPIC placed into the
CFLAGS definitions since this project always builds a shared library
but I keep it for future reference when I need to remember how it's
done for libraries that can be shared or static. I don't know how many
times I've had to tweak this recipe when the Linux version changes or
the tool set changes and the make syntax has been "revised" without
warning. Indentation is IMPORTANT too! Screw up the indentation and
the thing fails in weird and wondrous ways. But all this is OT here,
makefile madness is not C, thank God. Most won't touch the topic here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top