调用程序中对库类成员的未定义引用错误
下面添加的其他问题,2011 年 4 月 11 日
我正在开发一套跨平台的共享库 DLL/Sos 和 C++ 测试程序,尽管我必须能够支持 C。这些库将发布仅作为目标代码,但测试程序将附带源代码,以便我们的客户可以获得示例代码。因此,我正在设计要在运行时加载的库,即使用 dlopen()/LoadLibraryA() 进行动态链接。
我在 Umbutu 10.04 上使用 g++ 4.4.3-4,在 Vista/64 上使用 VC++ 2008(32 位模式)。
一切似乎在 Windows 上运行得很好(现在)。然而,当我在 Linux 上编译时,我遇到了一些我无法弄清楚的错误。
测试器和库有几个用多个 .cpp 和 .h 编码的类。除了主要入口点之外,库中的类和大多数内容都位于命名空间 DISCOVER_NS 中。
以下是该项目的简要概述:
首先,承认,我缩短了一些名称,以便代码更具可读性。
discover.cpp
创建一个类对象,并带有指向它的指针,称为 DiscoverObject 类型的 theMainObject。
具有一个外部“C”函数,该函数将MainObject 作为void* 返回给调用者程序。
DiscoverObject 有多种方法,并实例化在单独的 cpp 和 .h 中找到的其他类。一个特定的方法名为 Hello(),它执行您所期望的操作,它打印一条“hello”测试消息。
tester.cpp
获取库的句柄
获取指向返回 theMainObject 的函数的函数指针。
执行函数(指针)并将返回的地址从 void* 转换为 DISCOVER_NS::DiscoverObject* aDiscoverObject。
运行aDiscoverObject->Hello()。
我编译时使用:
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
现在,当我编译时,出现以下错误: *Tester.cpp:142: 对 `Discover_NS::DiscoverObject::hello()' 的未定义引用*
我还从 discovery.so 中得到了一堆其他未定义的引用错误,例如: *discover.so: 对 `Discover_NS::DeviceList::~DeviceList()* 的未定义引用
我已经尝试在 SO extern“C” 中制作几乎所有内容。没有区别。
我尝试将语句放入 discovery.cpp 中,如下所示: extern void Discover_NS::OtherClass::method( args ); 但这给了我关于“类外声明不是定义”错误的错误。
我知道查看代码会有所帮助,但我需要时间敲出一些小东西来发布。
任何人都可以提供解决这个混乱的想法吗?
谢谢,
韦斯·德米特里( Wes
Dmitry)的解决方案并不完全是解决方案的全部,但却是解决方案中的必要元素。检查我的 makefile 后,我发现了几行无意中重复的行(我将其删除),以及两个“拼写错误”,其中 -o 的编码路径错误,并被编码到了编译步骤中。破碎的步骤编译了logger.cpp和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
然后我发现了真正的bug。我完全忽略了这样一个事实:我没有在 Discover 目录中编译所有 .cpp!。花了一个小时才删除所有的小问题,但现在她从 makefile 中恢复过来了。
原始问题的新版本:现在我知道它可以通过 makefile 工作,我如何让 Eclipse 执行与 makefile 相同的操作?
谢谢德米特里。
Wes
好吧,我的问题仍然存在。
我已经按照 Dmitry (@Dmitry) 的建议编译了我的代码。只是,它们似乎引起了一个单独的问题。我希望我的库在运行时动态链接到主测试程序。将 -l Discover -l EdgeIO 添加到链接可以编译所有内容,但它给了我静态链接。
仅供参考,未使用的“pi”是指 SO 中有一个浮点数,因此将使用浮点支持进行编译。如果调用者想要使用浮点数,则为必需。有人有更好的方法来强制 g++ 编译包含浮点的代码吗?
在修复了 Dmitry 帮助我找到的许多错误之后,我现在得到了这个输出:
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
我从 Eclipse 得到了相同的错误消息集。
RemKonTester.cpp 包含声明这些项目的所有 .h。我已经用 extern "C" 声明尝试过它们,但没有。
希望得到帮助,
韦斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题似乎是
-l
的位置:它们应该位于目标文件之后,因为链接器在命令行遇到它们时会加载它们并搜索未定义的符号。
有关详细信息,请参阅
man ld
(特别是-l
选项):这应该适合你:
./RemKonTester/make.ERR请注意,StackOverflow 中有一个用于编辑问题的选项,发布附加信息作为答案并不是一个好的做法。
Your problem seems to be the position of
-l<library>
: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:This should work for you:
P.S. Note that there an option for editing your question in StackOverflow, posting additional info as an answer is not a good practice.
编辑:终于弄清楚了“编辑器”。弄得更漂亮了wjm
抱歉耽搁了。昨晚我不得不把前院的一棵倒下的树清理掉。
注意:我更改了原始注释中的一些名称以使其更短。这个答案已经说明了一切。
g++ 输出...
这是 makefile。抱歉,我不太会说 make,所以 makefile 是实用的,而不是漂亮的。
谢谢,
韦斯
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...
And this is the makefile. Sorry, I don't speak make that well, so the makefile is functional, not pretty.
Thanks,
Wes