链接到 C++ 中的 C 库应用
我正在 Visual Studio 2010 中开发 C/C++ 应用程序。该解决方案有多个项目,这些项目被编译并汇总到单个可执行文件中。我正在尝试添加打印语句来调试一些关键函数。
我已在其中一个 .c 文件的函数中添加了一条 printf 语句。我已小心地包含
。 C 标头包含在较高位置的 C++ 文件中。我知道 C 和 C++ 链接器的操作方式以及 extern
强制行为之间存在差异。然而,我不是这些细微差别的专家。
代码按以下方式设置:
// proc.h
#ifdef __cplusplus
extern "C" {
#endif
void do_stuff();
#ifdef __cplusplus
}
#endif
-----------------------------------------------
// proc.c
#include "proc.h"
#include <stdio.h>
void do_stuff()
{
printf("Hello from proc.c -- do_stuff()");
// Some other stuff
}
当我构建应用程序时,我在 VS 输出窗口中看到以下错误消息:[proj 路径]\common.lib(proc.obj) :错误 LNK2019:函数 _do_stuff@20 中引用了无法解析的外部符号 _printf
我还尝试将 #include
放入头文件中,而不是 c 文件中。在这种情况下,我得到:错误 LNK2001:无法解析的外部符号 printf
I'm working on a C/C++ application in Visual Studio 2010. The solution has several projects that are compiled and rolled up into a single executable. I'm trying to add print statements to debug a few of the critical functions.
I've added a printf statement in a function in one of the .c files. I have taken care to include <stdio.h>
. The C header is included in a C++ file somewhere higher up. I know that there are differences between how the C and C++ linkers operate and extern
forces behavior. However, I'm not an expert on the nuances.
The code is set up in the following manner:
// proc.h
#ifdef __cplusplus
extern "C" {
#endif
void do_stuff();
#ifdef __cplusplus
}
#endif
-----------------------------------------------
// proc.c
#include "proc.h"
#include <stdio.h>
void do_stuff()
{
printf("Hello from proc.c -- do_stuff()");
// Some other stuff
}
When I build the application, I see the following error message in VS output window:[proj path]\common.lib(proc.obj) : error LNK2019: unresolved external symbol _printf referenced in function _do_stuff@20
I also tried putting the #include <stdio.h>
in the header file, rather than the c file. In that case I get:error LNK2001: unresolved external symbol printf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试使用
而不是
?Did you try using
<cstdio>
instead of<stdio.h>
?这意味着您没有链接位于 lib 中的 printf 函数的实现。
对于我相信它在 cstdio 中?
This means you aren't linking in the implementation of the printf function, which is located in a lib.
For <stdio.h> I believe its in cstdio?
转到项目的属性并转到链接器部分。在“输入”子部分中,您是否忽略默认库,或者是否列出了任何要忽略的库?
printf 所在的库取决于您如何链接到运行时(C/C++ 部分,代码生成子部分 - 查看“运行时库”)。本知识库文章中的表格告诉您“运行时库”的每个值使用哪个标准 .lib
http://msdn.microsoft.com/en-us/library/6wtdswk0(v=vs.71).aspx
弄清楚发生了什么打开,您可以将 /VERBOSE:LIB 添加到链接器行,这将告诉您它查找的每个库(在链接器部分的命令行小节中执行此操作)
Go to the properties of your project and go to the Linker section. In the Input sub-section, are you ignoring default libraries, or do you have any libraries listed to ignore?
The library printf is in depends on how you link to the runtime (C/C++ section, Code Generation sub-section -- look at "Runtime Library"). The table in this KB tells you which standard .lib to use for each value of "Runtime Library"
http://msdn.microsoft.com/en-us/library/6wtdswk0(v=vs.71).aspx
To figure out what is going on, you can add /VERBOSE:LIB to your linker line, which will tell you every library it looked in (Do this in the command-line subsection of the Linker section)