Compiling both static and shared versions of a library

428 views
Skip to first unread message

Kelly Sommers

unread,
Jul 16, 2013, 7:35:17 PM7/16/13
to gyp-de...@googlegroups.com
I'm looking to compile both a static and shared version of a library. I've created two targets that include all the same source files.

The static target includes:

'target_name': 'haywire',
'product_name': 'haywire',
'type': 'static_library',
'dependencies': [
  './lib/libuv/uv.gyp:libuv',
],

The shared target includes:

'target_name': 'haywire_shared',
'product_name': 'haywire',
'type': 'shared_library',
'dependencies': [
  './lib/libuv/uv.gyp:libuv',
],

When compiling the shared library I get the following error:

/usr/bin/ld: ./builds/unix/debug/obj.target/haywire_shared/src/haywire/http_parser.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
./builds/unix/debug/obj.target/haywire_shared/src/haywire/http_parser.o: could not read symbols: Bad value

So I added the "-fPIC" to the haywire_shared target as suggested:

'cflags': [
  '-std=c99',
  '-fPIC',
],

But the same linker error seems to happen. Any ideas what I've done wrong? Here's the link to the full gyp file.


Thanks for the help in advance!

Evan Martin

unread,
Jul 17, 2013, 2:26:36 PM7/17/13
to Kelly Sommers, gyp-developer
That is strange! Some ideas:

- Try running make with "make V=1" to get the full command line used
to build that path; maybe the -fPIC is being lost somewhere due to a
hard-to-see typo?

- From the Chrome build files I see they set -fPIC for both cflags and
ldflags, so maybe that is necessary:
'cflags': [
'-fPIC',
],
'ldflags': [
'-fPIC',
],
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "gyp-developer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to gyp-develope...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Scott Graham

unread,
Jul 17, 2013, 2:29:03 PM7/17/13
to Evan Martin, Kelly Sommers, gyp-developer
I think it'd have to go in a target_defaults or something, otherwise it'd only affect the stuff in haywire_shared, not in libuv?

(I "might" have no idea what I'm talking about)

Evan Martin

unread,
Jul 17, 2013, 2:45:59 PM7/17/13
to Scott Graham, Kelly Sommers, gyp-developer
Good point! I think the Chrome setting is in target_defaults, but
that file is so nested it's hard to say. :\

https://code.google.com/p/chromium/codesearch#chromium/src/build/common.gypi&q=fpic%20common.gypi&sq=package:chromium&type=cs&l=3104

PS: gyp tries to make this easier, by letting you just reference the
static library as a dependency of the shared library, but fPIC screws
that up so I think Kelly's version with all the source files listed
twice is the best you can do.

Kelly Sommers

unread,
Jul 17, 2013, 9:58:58 PM7/17/13
to Evan Martin, Scott Graham, gyp-developer
Putting -fPIC in the target_defaults worked! Thanks so much :)
Reply all
Reply to author
Forward
0 new messages