Microsoft Visual Studio 中的外部链接问题
我有两个共享外部变量的库。 在 libA 中,我在标头中声明了这样的变量:
extern int Gbl;
然后我在源代码中再次将其定义为:
int Gbl;
接下来然后我设置 Gbl=1;
在 libB 中,我包含 libA 的头文件。还有 printf Gbl,期望在这里得到 1。
虽然不幸的是我什至无法编译它,因为我收到错误:
undefined symbol '_Gbl' referenced in ....
我被告知这是一个链接错误,但我不知道从这里做什么
I have two libs that share a extern variable.
In libA I have the variable declared in the header as such:
extern int Gbl;
then I define it again in the source as:
int Gbl;
next then I set Gbl=1;
in libB, I include libA's header file. And printf Gbl, expecting to get 1 here.
though unfortunately i can't even compile this since I get the error:
undefined symbol '_Gbl' referenced in ....
I was told that this is a linking error, but I'm not sure what to do from here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要编译包含 libA 的 libB。这是通过将 -llibA 添加到编译行来完成的。如果您没有正确设置库选项,则需要添加 -lpath_to_library
You need to compile the libB with libA included. This is done by adding -llibA to the compile line. If you don't have library options set correctly, you need to add -lpath_to_library