Undefined reference to member function found in static library - c++

I know what you're thinking - this has been answered a million times. I wish I could put something in the Title that would make it clear that I already know that the order of libraries is important.
I'm using g++ on Centos 6 64. I have a simple test program:
#include <ptlib.h>
int main()
{
PTimer indirectTimer1_;
indirectTimer1_.SetNotifier(0);
return 0;
}
and I compile and link it with this command:
g++ -I./ptlib/include/ mm.cpp ptlib/lib_linux_x86_64/libpt_s.a -lpthread -lrt
and I get this as an answer back:
/tmp/cc53itXb.o: In function `main':
mm.cpp:(.text+0x52): undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
collect2: ld returned 1 exit status
But I know that PTimer::SetNotifier is in the .a file:
nm -AC ptlib/lib_linux_x86_64/libpt_s.a | grep SetNotifier
ptlib/lib_linux_x86_64/libpt_s.a:osutil.o:0000000000003dd8 T PTimer::SetNotifier(PNotifierTemplate<long> const&)
To make matters more confusing, when I compile the library on Centos 5, 32bit, and run the same test, it links just fine.
I've tried using '-Lptlib/lib_linux_x86_64 -lpt_s' and I've tried using the -Wl,--start-group / -Wl,--end-group args to no avail. If I add 'ptlib/src/ptlib/unix/osutil.cxx' to the g++ line, it compiles and links just fine. Unfortunately, this is but a sample of the unreferenced functions in our main program. Not all of the, just a few seemingly random methods like this one are undefined (which also links just fine on Centos 5, 32 bit).
As you can see, I've tried a lot of things but still haven't figured it out. I need something else to try! Or someone to point out something terribly easy that I am doing wrong.

undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
PTimer::SetNotifier(PNotifierTemplate<long> const&)
Notice the difference in int vs. long?

Related

Can't link a dynamic lib including iostream [duplicate]

This question already has answers here:
Using only g++ works, but not "g++ -c" and ld
(4 answers)
Closed 6 years ago.
ld is acting weirdly and I want to understand what's going on. In a mycode.cpp, I have the following:
#include <algorithm>
#include "mycode.hpp"
// awesome stuff here
I compile it with g++ -fPIC -c mycode.cpp and link it with ld -Bshareable -o libmylib.so mycode.o. Works like a charm.
Then I want to call cout in mycode.cpp. Actually, even before adding this cout, if I just add #include <iostream> in the code above, while linking I get the error
mycode.o: In function `__static_initialization_and_destruction_0(int, int)':
mycode.cpp:(.text+0x50): undefined reference to `__dso_handle'
ld: mycode.o: relocation R_X86_64_PC32 against undefined hidden symbol `__dso_handle' can not be used when making a shared object
ld: final link failed: Bad value
If I link it with g++ -shared, it works, but that's not the point. I do not understand what's wrong here, and I am looking for insights.
EDIT: I understand one must call g++ instead of ld directly. My problem is, I want to understand what's under the hood: why including iostream break things while algorithm is already here (so ld knows stdc++)
link it with ld -Bshareable -o libmylib.so mycode.o. Works like a charm.
It only works by accident.
User-level code should never be linked directly with ld, and should always use the appropriate compiler driver (g++ here) to perform the link. Anything else, and you'll fail with weird link-time or runtime errors (just as you did here).

why I use the static library to compile and link is corrent while the dynamic library is wrong for am ace demo?

my gcc/g++ version is 4.1.2 , ACE-6.10 in CentOS 5.10 and I make the ACE Library with static_libs=1 option to get the static library, after make and make install ,I get such libraries such as libACE.so , libACE.a and so on, and then I write following code to test, The codes shows as follow:
#include <ace/Log_Msg.h>
#include <ace/OS_main.h>
using namespace std;
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
ACE_DEBUG( (LM_DEBUG, ACE_TEXT("Hello World!\n") ) );
return 0;
}
then I use the following two method to compile and link :
method 1:
g++ -p -o acetest acetest.cpp /usr/local/src/ACE_wrappers/lib/libACE.a -I$ACE_ROOT -I$ACE_ROOT/ace -pthread -ldl -lrt
method 2:
[root#localhost testCode]# g++ -p -o acetest acetest.cpp -L/usr/local/src/ACE_wrappers/lib -lACE -I$ACE_ROOT -I$ACE_ROOT/ace -pthread -ldl -lrt
/tmp/cc0eKwlC.o: In function `main':
acetest.cpp:(.text+0x15): undefined reference to `ACE_Log_Msg::last_error_adapter()'
acetest.cpp:(.text+0x1d): undefined reference to `ACE_Log_Msg::instance()'
acetest.cpp:(.text+0x3f): undefined reference to `ACE_Log_Msg::conditional_set(char const*, int, int, int)'
acetest.cpp:(.text+0x57): undefined reference to `ACE_Log_Msg::log(ACE_Log_Priority, char const*, ...)'
collect2: ld 返回 1
And it comes to the question, the method 1 to use static library is corrent, why method 2,which uses the dynamic library, is wrong?
hunger for the answer, thanks all;
When you compile your application you should add the -DACE_AS_STATIC_LIBS flag to the compiler to indicate that you want to link statically with ACE
Try adding no_hidden_visibility=1 to your platform_macros.GNU file. I believe ACE builds its shared libraries with symbols hidden by default.
See here for the benefits this can provide. However, it doesn't seem to work well when mixing static and dynamic libraries. If anyone has more info on why that is, feel free to chime in.

Google RE2 library cannot compile with 'make testinstall' in ubuntu

Gurus!
I am using Ubuntu 13.10 64-bit to compile latest Google RE2 library, but 'make testinstall' failed to compile, here is the log:
kevin#ubuntu:~/re2$ make testinstall
cp testinstall.cc obj
(cd obj && g++ -I/usr/local/include -L/usr/local/lib testinstall.cc -lre2 -pthread -o testinstall)
/tmp/ccSsaSXS.o: In function main':
testinstall.cc:(.text+0xce): undefined reference tore2::FilteredRE2::FirstMatch(re2::StringPiece const&, std::vector > const&) const'
/usr/local/lib/libre2.so: undefined reference to pthread_rwlock_rdlock'
/usr/local/lib/libre2.so: undefined reference topthread_rwlock_wrlock'
/usr/local/lib/libre2.so: undefined reference to pthread_rwlock_destroy'
/usr/local/lib/libre2.so: undefined reference topthread_rwlock_init'
/usr/local/lib/libre2.so: undefined reference to `pthread_rwlock_unlock'
collect2: error: ld returned 1 exit status
make: * [testinstall] Error 1
I tried to replace -pthread with -lpthread, still failed, then I dumped libre2.so and found that pthread_xxx is in it.
Here is the issue tracking in RE2 forum: https://code.google.com/p/re2/issues/detail?id=100
Anyone here have ever complied RE2 successfully ? Thank you!
See this comment:
Adding -pthread to LDFLAGS seems to fix make test (all tests are
passing), but not make testinstall.
That will get you to the next error
Depending on what you build it for 'make testinstall' might not be necessary.
I just needed to get python re2 port working, and this can be installed after running make install.
I encounter this problem before. Modify the makefile and use -lpthread instead of -pthread.
So I tried looking for the lines in testinstall.cc that were causing the symbol errors and I found out that the only line was on line 18:
18 - f.firstMatch(:abbccc:, ids);
I commented this line out (so that the FullMatch function below is still called) and just ran g++ testinstall.cc -lre2 -pthread -o testinstall (basically what the Makefile does) and I was able to get a binary successfully. Although this might not really solve the problem, its good to know that we can still use the RE2::Fullmatch and partial match functions
If I were to guess, maybe there is a dependency somewhere inside the filtered_re2 module?
I had the same problem. But if you compile with -static everything goes well.
nm -C shows that the "missing" symbol exists in both .a and .so files.

Undefined references in png++

I am new to C++ on linux enviroment and am trying to use the png++ library for a project. The problem I am facing is that that a simple program I write using png++ incudes does not work and shows me the following errors
AProg.o: In function `png::info_base::info_base(png::io_base&, png_struct_def*)':
AProg.cpp:(.text._ZN3png9info_baseC2ERNS_7io_baseEP14png_struct_def[_ZN3png9info_baseC5ERNS_7io_baseEP14png_struct_def]+0x21): undefined reference to `png_create_info_struct'
AProg.o: In function `png::info::write() const':
AProg.cpp:(.text._ZNK3png4info5writeEv[png::info::write() const]+0xd4): undefined reference to `png_set_PLTE'
AProg.cpp:(.text._ZNK3png4info5writeEv[png::info::write() const]+0x137): undefined reference to `png_set_tRNS'
AProg.cpp:(.text._ZNK3png4info5writeEv[png::info::write() const]+0x14f): undefined reference to `png_write_info'
AProg.o: In function `png::info::sync_ihdr() const':
AProg.cpp:(.text._ZNK3png4info9sync_ihdrEv[png::info::sync_ihdr() const]+0x79): undefined reference to `png_set_IHDR'
AProg.o: In function `png::end_info::destroy()':
AProg.cpp:(.text._ZN3png8end_info7destroyEv[png::end_info::destroy()]+0x48): undefined reference to `png_destroy_info_struct'
AProg.o: In function `png::end_info::write() const':
AProg.cpp:(.text._ZNK3png8end_info5writeEv[png::end_info::write() const]+0x1a): undefined reference to `png_write_end'
AProg.o: In function `png::io_base::set_swap() const':
AProg.cpp:(.text._ZNK3png7io_base8set_swapEv[png::io_base::set_swap() const]+0x1b): undefined reference to `png_set_swap'
.............. (and it goes on).
The background of what I have done so far.
1 : I have gcc/g++ configured correctly.
2 : I have correctly installed libpng-1.2.50. The result seems to be correct.
This is how my usr/local folder looks now
:/usr/local/include$ ls
libpng12 libpng15 png++ pngconf.h png.h pnglibconf.h
:/usr/local/lib$ ls
libpng12.a libpng12.so libpng12.so.0.50.0 libpng15.la libpng15.so.15 libpng.a libpng.so libpng.so.3.50.0 python2.7
libpng12.la libpng12.so.0 libpng15.a libpng15.so libpng15.so.15.12.0 libpng.la libpng.so.3 pkgconfig
:/usr/local/bin$ ls
eclipse libpng-1.2.50 libpng12-config libpng-1.5.12 libpng15-config libpng-config png++-0.2.5
3 : After that I followed the following ( http://www.nongnu.org/pngpp/doc/0.2.5/ ) to install the png++-0.2.5 and all the five steps didnt gave any error.
But after that when I tried to compile a simple program (with the instructions given at the same site : http://www.nongnu.org/pngpp/doc/0.2.5/ ) it would not compile.
:~/workspace/AProg$ g++ -o AProg AProg.o 'libpng-config --ldflags'
g++: error: libpng-config --ldflags: No such file or directory
Then I tried to solve the problem and google it and gave this command which seems to work fine at compile but when I tried to run it. I got the error as mentioned at the top of the post
:~/workspace/AProg$ g++ -c AProg.cpp -I/usr/local/include/libpng12 -L/usr/local/lib -lpng -I/usr/local/include/png++
:~/workspace/AProg$
I am sorry for such a long post. But I just wanted to explain anything /everything related to my problem. Hope somebody helps me here.
It seems that the quotes you use in the linker command line is not the correct ones, it should be the single back quote:
$ g++ -o AProg AProg.o `libpng-config --ldflags`

undefined reference to MinFilter and MaxFilter in StereoMatch code

everyone
Recently, I'm working on stereo vision. And I download the StereoMatch code from middlebury website: http://vision.middlebury.edu/stereo/code/
I used Ubuntu 11.10 to run this code. After I run this code, I got the following error. There are 3 'undefined reference to ' error. But the code has already included the header file 'MinFilter.h'. And I get confused. Could someone help me? I would appreciate it.
errors:
StcAggregate.o: In function CStereoMatcher::AggrMin()':
StcAggregate.cpp:(.text+0x20f): undefined reference tovoid MinFilter(CImageOf&, CImageOf&, int, int)'
StcEvaluate.o: In function _ZL14PartialShuffle8CImageOfIhERS0_S1_f.constprop.2':
StcEvaluate.cpp:(.text+0x37): undefined reference tovoid MinFilter(CImageOf&, CImageOf&, int, int)'
StcEvaluate.cpp:(.text+0x5b): undefined reference to `void MaxFilter(CImageOf&, CImageOf&, int, int)'
collect2: ld returned 1 exit status
make: * [StereoMatch] Error 1
This is an error from the linker (and/or runtime environment) which cannot find some symbols (i.e. code) which the header files you've included promised to exist. In order for these symbols to be found, you must tell the linker to link against the library (or object file) containing them. This library may have come with the code pre-compiled or may have been made during installation. Have you not got a README file where it is explained how to use (i.e. link and run) the code?
I met exactly the same problem and solved it like this:
Open the Makefile and change the first line from
CPPFLAGS = -O2 -Wall
to
CPPFLAGS = -O -Wall
Then "make clean" and "make", it works for me. So I guess it is because param
"-O2" does more optimization than expected.