undefined reference when linking with a c library from c++ code

81 views
Skip to first unread message

tfa...@chromium.org

unread,
Nov 2, 2013, 8:10:46 PM11/2/13
to gyp-de...@googlegroups.com
Hi,

I'm getting this in my sample application:

$ ninja -C out/Default/
ninja: Entering directory `out/Default/'
[2/2] LINK example_cpp
FAILED: clang++  -o example_cpp -Wl,--start-group obj/example_cpp.example_cpp.o obj/liblinenoise.a  -Wl,--end-group 
obj/example_cpp.example_cpp.o: In function `main':
../../example_cpp.cc:(.text+0x18): undefined reference to `linenoise(char const*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Anyone knows why is that? What am I missing?

I attached the two files to demonstrate the failure. The rest of the code you can see at github.com/tfarina/cmdline-history

example_cpp.cc
example.gyp

tfa...@chromium.org

unread,
Nov 2, 2013, 8:11:47 PM11/2/13
to gyp-de...@googlegroups.com
the right link to the repo is github.com/tfarina/cmdline_history

Evan Martin

unread,
Nov 4, 2013, 4:53:55 PM11/4/13
to Thiago Farina, gyp-developer
It looks like Ami helped you out (for the list: the problem was C++ name mangling).

One way to have diagnosed this is to compare the symbols needed by your example object against the symbols exported by your linenoise object.

Try temporarily undoing your 'extern C' bit to produce the error again, then run these commands:

$ nm out/Default/obj/linenoise.linenoise.o | grep linenoise
=> should produce ouput including something like this:
0000000000001b36 T linenoise

$ nm out/Default/obj/example_cpp.example_cpp.o | grep linenoise
=> should produce ouput including something like this:
                 U _Z9linenoisePKc

Then try running "c++filt" and pasting the second one in:
$ c++filt
                 U _Z9linenoisePKc
It will print
                 U linenoise(char const*)
which is the unmangled name (and the one printed in your error message).






--
 
---
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.

Reply all
Reply to author
Forward
0 new messages