Unsdefined reference to library class members errors from caller program

我与影子孤独终老i 提交于 2019-12-05 23:07:25

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.

EDITED: Finally figured out the "editor". Made it prettier. wjm

Sorry for the delay. I had to get a fallen tree out of my front yard last night.

Note: I changes a few names in the original note to make them shorter. This answer has everything spelled out.


g++ output...

$ make
./EdgeIO/dllMain.cpp: In function ‘void InitalizeLibraryServices()’:
./EdgeIO/dllMain.cpp:158: warning: unused variable ‘pi’
./Discover/dllmain.cpp: In function ‘void InitalizeLibraryServices()’:
./Discover/dllmain.cpp:174: warning: unused variable ‘pi’
./RemKonTester/RemKonTester.cpp: In function ‘int main(int, char**)’:
./RemKonTester/RemKonTester.cpp:130: warning: ISO C++ forbids casting between     
pointer-to-function and pointer-to-object
./RemKonTester/RemKonTester.cpp:152: warning: unused variable ‘searchResp’
./RemKonTester/libraryClass.cpp: In member function ‘int 
library::AttachLibrary()’:
./RemKonTester/libraryClass.cpp:132: warning: ISO C++ forbids casting between 
pointer-to-function and pointer-to-object
./RemKonTester/libraryClass.cpp:154: warning: ISO C++ forbids casting between 
pointer-to-function and pointer-to-object
./RemKonTester/Debug/RemKonTester.o: In function `main': 
/home/wmiller/Projects/Eclipse/
./RemKonTester/RemKonTester.cpp:142: undefined reference to 
`RemKon_Discover::DiscoverObject::hello()'
collect2: ld returned 1 exit status
gflags = -g3
tstlinkflags = -ldl  -lpthread
defines =  -D__linux__   -D_DEBUG   -D_IPC_ARCH_INTEL=1   -D_THREAD_SAFE
./Debug/libdiscover.so: undefined reference to     
`RemKon_Discover::RemKonDeviceList::~RemKonDeviceList()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::ClientPort(unsigned short)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::PayloadSize(unsigned int)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::Socket() const'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemKonDeviceList::Count() const'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemException::~RemException()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::UDPbinder()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::ListenTimeOut(int)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::ServerPort(unsigned short)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemKonDeviceList::Next()'
./Debug/libdiscover.so: undefined reference to `RemKon_Discover::Log(char 
const*, ...)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::UDPlisten(unsigned char*, int*, sockaddr_in*, int*)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemException::RemException()'
./Debug/libdiscover.so: undefined reference to `RemKon_Discover::VerboseLogging()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::RegisterCallbackFunction(void (*)(void*, unsigned 
char*, int), void*)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemKonDeviceList::First()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::TestData(unsigned int, unsigned int, unsigned char*, 
unsigned int)'
./Debug/libdiscover.so: undefined reference to `RemKon_Discover::Logging()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::MessageLength(int)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::LocalIpAddress(int)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::UDPsend()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::DiscoverObject::LocalIpAddress() const'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::SetBroadcastMode()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::MakeMacAddressString(unsigned char*, char*)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemKonDeviceList::RemKonDeviceList()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemKonDeviceList::New()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::ActivateLogging(unsigned int)'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::RemSocket()'
./Debug/libdiscover.so: undefined reference to 
`RemKon_Discover::RemSocket::Payload(unsigned int)'
collect2: ld returned 1 exit status

And this is the makefile. Sorry, I don't speak make that well, so the makefile is functional, not pretty.

CC = @g++
gflags = -g3
cflags = -fPIC -Wall -pedantic
liblinkflags := -shared -fPIC -lstdc++ -lrt -lpthread -rdynamic
tstlinkflags := -ldl  -lpthread
defines = -D__linux__   -D_DEBUG   -D_IPC_ARCH_INTEL=1   -D_THREAD_SAFE


all : clean edgeio discover tester


############################################

edgeio : ./EdgeIO/dllMain.o ./EdgeIO/EdgeIO.o
$(CC) $(gflags)  $(liblinkflags) $(defines)    -Wl,-soname,./Debug
    /libedgeio.so.1  -o ./Debug/libedgeio.so.1.0 ./EdgeIO/Debug/edgeio.o  
    ./EdgeIO/Debug/dllmain.o    2>&1  | tee ./EdgeIO/make.ERR
            @cd ./Debug; ln -sf libedgeio.so.1.0    libedgeio.so
        @cd ./Debug; ln -sf libedgeio.so.1.0    libedgeio.so.1

./EdgeIO/dllMain.o : ./EdgeIO/dllMain.cpp
        $(CC) $(gflags)  $(cflags) -c $(defines)   -I ./common  -I ./EdgeIO  
    -I ./Discover   -o./EdgeIO/Debug/dllmain.o  ./EdgeIO/dllMain.cpp   2>&1  
    | tee ./EdgeIO/dllmain.ERR

./EdgeIO/EdgeIO.o : ./EdgeIO/EdgeIO.cpp
    $(CC) $(gflags)  $(cflags) -c $(defines)   -I ./common  -I ./EdgeIO  
    -I ./Discover   -o./EdgeIO/Debug/edgeio.o  ./EdgeIO/EdgeIO.cpp     2>&1  | 
    tee ./EdgeIO/EdgeIO.ERR

############################################

discover : ./Discover/Discover.o ./Discover/dllmain.o
    $(CC) $(gflags)  $(liblinkflags) $(defines)    -Wl,-soname,./Debug
        /libdiscover.so.1  -o ./Debug/libdiscover.so.1.0          ./Discover/Debug
        /Discover.o  ./Discover/Debug/dllmain.o    2>&1  | tee ./Discover/make.ERR
    @cd ./Debug; ln -sf libdiscover.so.1.0  libdiscover.so
    @cd ./Debug; ln -sf libdiscover.so.1.0  libdiscover.so.1

./Discover/Discover.o : ./Discover/Discover.cpp
    $(CC) $(gflags)  $(cflags) -c $(defines)   -I ./common  -I ./EdgeIO  
    -I ./Discover   -o./Discover/Debug/Discover.o  ./Discover/Discover.cpp  
     2>&1  | tee ./Discover/Discover.ERR

./Discover/dllmain.o : ./Discover/dllmain.cpp
        $(CC) $(gflags)  $(cflags) -c  $(defines)  -I ./common  -I ./EdgeIO  
        -I ./Discover   -o./Discover/Debug/dllmain.o  ./Discover/dllmain.cpp   
        2>&1  | tee ./Discover/dllmain.ERR

############################################

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

    @echo "gflags = $(gflags)"   2>&1  | tee ./RemKonTester/make.ERR
    @echo "tstlinkflags = $(tstlinkflags)"   2>&1  | tee ./RemKonTester/make.ERR
    @echo "defines =  $(defines)"   2>&1  | tee ./RemKonTester/make.ERR

    $(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

./RemKonTester/RemKonTester.o : ./RemKonTester/RemKonTester.cpp
    $(CC) $(gflags)  $(cflags) -c $(defines)   -I ./common  -I ./EdgeIO  
        -I ./Discover   -o ./RemKonTester/Debug/RemKonTester.o   ./RemKonTester
        /RemKonTester.cpp   2>&1  | tee ./RemKonTester/RemKonTester.ERR

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

./RemKonTester/libraryClass.o : ./RemKonTester/libraryClass.cpp
    $(CC) $(gflags)  $(cflags) -c  $(defines)  -I ./common  -I ./EdgeIO  
        -I ./Discover    -o ./RemKonTester/Debug/libraryClass.o 
        ./RemKonTester/libraryClass.cpp   2>&1  | tee ./RemKonTester
        /libraryClass.ERR

./common/RemException.o : ./common/RemException.cpp
    $(CC) $(gflags)  $(cflags) -c  $(defines)  -I ./common  -I ./EdgeIO  
        -I ./Discover    -o ./RemKonTester/Debug/RemException.o ./common
        /RemException.cpp   2>&1  | tee ./RemKonTester/RemException.ERR


############################################

clean :
    @rm -f *.ERR
    @rm -f ./common/*.ERR
    @rm -f ./EdgeIO/*.ERR
    @rm -f ./Discover/*.ERR
    @rm -f ./RemKonTester/*.ERR

    @rm -f QEMACRO*
    @rm -f ./common/EdgeIO/QEMACRO*
    @rm -f ./EdgeIO/QEMACRO*
    @rm -f ./Discover/QEMACRO*
    @rm -f ./RemKonTester/QEMACRO*

    @rm -f ./Debug/*.o
    @rm -f ./common/Debug/*.o
    @rm -f ./EdgeIO/Debug/*.o
    @rm -f ./Discover/Debug/*.o
    @rm -f ./RemKonTester/Debug/*.o

    @rm -f ./Debug/*.log
    @rm -f ./common/Debug/*.log
    @rm -f ./EdgeIO/Debug/*.log
    @rm -f ./Discover/Debug/*.log
    @rm -f ./RemKonTester/Debug/*.log

    @rm -f ./Debug/*.so*

############################################

Thanks,

Wes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!