Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsdefined reference to library class members errors from caller program

Additional Questions added below, 4/11/2011

I am developing a cross-platform set of shared libraries DLLs/Sos and tester programs in C++ though I have to be able to support C. The libraries will ship as object code only, but the tester program(s) will ship with source so our customers can have example code. For this reason I am designing the libraries to be loaded at runtime, i.e. dynamic linking using dlopen()/LoadLibraryA().

I am using g++ 4.4.3-4 on Umbutu 10.04 and VC++ 2008 on Vista/64 (in 32 bit mode).

Everything seems to work just fine on Windows (right now). However, when I compile on Linux I am getting some errors I can't figure out.

The tester and the library have several classes coded in several .cpp's and .h's. The classes and most everything in the library except the main entry points are in a namespace DISCOVER_NS.

Here is a brief sketch of the project:

First, an admission, I've shortened a bunch of names so the code is more readable.

discover.cpp

  • Creates a class object with a pointer to it called theMainObject of type DiscoverObject.

  • Has an extern "C" function that returns theMainObject to the caller program as void*.

  • DiscoverObject has several methods and instantiates other classes found in separate cpp's and .h's. One particular method is named Hello(), which does what you'd expect, it prints a "hello" test message..


tester.cpp

  • Gets a handle to the library

  • gets the function pointer to the function that returns theMainObject.

  • Executes the function (pointer) and casts the returned address from void* to DISCOVER_NS::DiscoverObject* aDiscoverObject.

  • Run aDiscoverObject->Hello().


I compile with:

CC = @g++

gflags = -g3

cflags = -fPIC -Wall -pedantic

lib_linkflags := -shared -fPIC -lstdc++ -lrt -lpthread -rdynamic

tester_linkflags := -ldl -lpthread

defines = -D_linux_ -D_DEBUG -D_IPC_ARCH_INTEL=1 -D_THREAD_SAFE


Now, when I compile I get these errors: *Tester.cpp:142: undefined reference to `Discover_NS::DiscoverObject::hello()'*

I also get a bunch of other undefined reference errors from discover.so saying, for example: *discover.so: undefined reference to `Discover_NS::DeviceList::~DeviceList()*


I have tried making virtually everything in the SO extern "C". No difference.

I tried putting statements into discover.cpp that look like: extern void Discover_NS::OtherClass::method( args ); but that gives me errors about" declaration outside of class is not deffinition" errors.


I know it will help to see code, but I need time to whack out something small for posting.

Can anyone offer ideas to solve this mess?

Thanks,

Wes

Dmitry's solution was not quite all of the fix, but was a necessary element in the solution. Upon examination of my makefile I found a couple of unintentionally duplicated lines, which I removed, and two "typos" where I had the wrong path for -o's coded into compile steps. The broken steps compiled logger.cpp and RemException.cpp:

./common/logger.o : ./common/logger.cpp
    $(CC) $(gflags)  $(cflags) -c  $(defines)  -I ./common  
        -I ./EdgeIO  -I ./Discover   
        -o ./common/Debug/logger.o   <+++++++++ path to .o was wrong
        ./common/logger.cpp   2>&1  | tee ./RemKonTester/logger.ERR

Then I found the real bug. I completely missed the fact that I wasn't compiling all of my .cpp's in the Discover directory!. It took a good hour to get all the nit-pics removed but now she comiles from a makefile just fine.

NEW VERSION OF THE ORIGINAL QUESTION: NOw that I know it WILL work via makefile, how do I talk Eclipse into doing the same thing the makefile is doing?

Thanks Dmitry.

Wes

Well, my problem is still here.

I have my code compiling with Dmitry's (@Dmitry) suggestions in place. Only, they appear to be causing a separate problem. I want my libraries to link to he main test program dynamically, at run time. Adding the -l Discover -l EdgeIO to the link gets everything to compile, but it give me static linking.

FYI, The unused "pi"s are so the SO has a floating point number in it and will thus compile with floating point support. Required if caller ever wants to use floating point numbers. Anybody got a better way to force g++ to comkpile with floating point included?

After fixing the many bugs Dmitry helped me find, I now get this output:

make
./Discover/dllmain.cpp: In function ‘void InitalizeLibraryServices()’:
./Discover/dllmain.cpp:175: warning: unused variable ‘pi’


./EdgeIO/dllMain.cpp: In function ‘void InitalizeLibraryServices()’:
./EdgeIO/dllMain.cpp:158: warning: unused variable ‘pi’


linking RemKonTester
    gflags = -g3
    tstlinkflags = -ldl  -lpthread 
    defines =  -D__linux__   -D_DEBUG   -D_IPC_ARCH_INTEL=1   -D_THREAD_SAFE

./RemKonTester/Debug/RemKonTester.o: In function `main':
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:81: undefined 
    reference to `RemKon_EdgeIO::EdgeIoObject::hello()'
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:111: undefined 
    reference to `RemKon_Discover::DiscoverObject::hello()'
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:116: undefined 
    reference to `RemKon_Discover::DiscoverObject::SetLogLevel(unsigned int)'
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:117: undefined 
    reference to `RemKon_Discover::DiscoverObject::hello()'
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:118: undefined 
    reference to `RemKon_Discover::DiscoverObject::LocalIpAddress(int)'
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:122: undefined 
    reference to `RemKon_Discover::DiscoverObject::RegisterCallback(bool(*)
    (void*), void*)'
/home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:123: undefined 
    reference to `RemKon_Discover::DiscoverObject::Search()'

collect2: ld returned 1 exit status

I get the same set of error messages from Eclipse.

RemKonTester.cpp includes all the .h's where these items are declared. I have tried them with the declarations extern "C" and not.

Hoping for help,

Wes

like image 458
Wes Miller Avatar asked Dec 21 '25 07:12

Wes Miller


1 Answers

Your problem seems to be the position of -l<library>:

$(CC)  $(gflags)  $(tstlinkflags) $(defines)      -L ./Debug    -ldiscover   
        -ledgeio -o ./Debug/RemKonTester  ./RemKonTester/Debug/RemKonTester.o  
        ./RemKonTester/Debug/logger.o  ./RemKonTester/Debug/libraryClass.o   
        2>&1  | tee ./RemKonTester/make.ERR

They should be after the object files, because the linker loads them when they're met at the command line and search for the undefined symbols.

See man ld (specifically -l option) for more info:

-l namespec

...

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

This should work for you:

$(CC) $(gflags) $(tstlinkflags) $(defines) -L ./Debug -o ./Debug/RemKonTester ./RemKonTester/Debug/RemKonTester.o ./RemKonTester/Debug/logger.o ./RemKonTester/Debug/libraryClass.o -ldiscover -ledgeio 2>&1 | tee ./RemKonTester/make.ERR

P.S. Note that there an option for editing your question in StackOverflow, posting additional info as an answer is not a good practice.

like image 55
Dmitry Yudakov Avatar answered Dec 23 '25 22:12

Dmitry Yudakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!