Re: linking problem - "undefined reference to ..."

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Chris Bouchard wrote:

> I searched the library and found:
> --------------------------------------------------------
> [cbouchrd@lx6 ~/tsil-1.1]$ nm -AP libtsil.a | grep ' T '
> [snip other functions]
> libtsil.a[initialize.o]: TSIL_SetParameters T 0000030a 00000c6d
> [snip other functions]
> --------------------------------------------------------
> I see the addition of T 0000030a00000c6d at the end of the function name... is this the name mangling you're talking about?

No, those are just the address within the object and the size.  The
mangled name would look something like
"_Z18TSIL_SetParametersP9TSIL_Dataeeeeee".  In fact you can see what it
is exactly by looking at how g++ encodes the call:

g++ -S fig6.cpp -o - | grep TSIL_SetParameters

> I did get the library from elsewhere.  Is their a way to enforce compatibility?  
In a sense, yes.  C has a much more stable and unchanging ABI, and is
callable from C++.  So it is much easier to distribute a C library and
have it usable by everyone than it is to distribute a C++ library.

> Perhaps by forcing compilation by a specific ABI version of gcc using an option I seem to remember reading about... okay I just looked it up... would "-fabi-version=n" fix this?

I don't know what the extent of what -fabi-version is able to change,
but it's not significant.  There's no switch to make a 4.3 gcc ABI
compatible with 3.3 gcc for example, nor one to make gcc compatible with
the C++ ABI of other vendors' compilers.  But that's not relevant here
as this is a C library.

> >From the website where I downloaded the code, "It is written in C, and can be linked to C/C++ and Fortran applications."  So, perhaps I should alter the tsil.h header to declare all extern "C".
> 
> I must admit, I don't know what this means or how to do it but I'll get my books out and start studying.  I also don't know what the statement "if __cplusplus is true" means.

If you look at some headers for common C libraries you will often see
near the top:

#ifdef __cplusplus
extern "C" {
#endif

and near the end:

#ifdef __cplusplus
}
#endif

The preprocessor symbol __cplusplus is defined when the file is being
compiled as C++, and is used to wrap all the declarations with a large
"extern "C" { ... }".  The reason for the #ifdef is that so it remains a
valid C file as well.

> 2.  maybe the library is a C library and I should declare the functions in tsil.h as extern "C"

That or:

mv fig6.cpp fig6.c
gcc -o fig6 fig6.c -L. -ltsil

Brian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux