64,650
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
void printf(char *p);
void out(int a)
{
printf("haha");
}
#include "stdafx.h"
void out(int a);
int _tmain(int argc, _TCHAR* argv[])
{
out(1);
return 0;
}
#include "stdafx.h"
#include <iostream>
void out(int a)
{
printf("haha");
}
STL的代码呢? [quote=引用 1 楼 movsd 的回复:] 静态库中只有你写的代码,你所调用的所有外部函数的代码都不会编译到静态库中。
STL的代码呢? 静态库中只有你写的代码,你所调用的所有外部函数的代码都不会编译到静态库中。
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <stdio.h>
#include <tchar.h>
// TODO: 在此处引用程序需要的其他头文件
其实,你只要把你的 printf 声明去掉,就能够链接成功,不然,你试试静态库中只有你写的代码,你所调用的所有外部函数的代码都不会编译到静态库中。
我感觉是不链接的,因为如下生成静态库成功了:但库中的out函数是用不了的,工程设置附加依赖项填上上面生成的静态库文件,但用户程序链接时说找不到printf,代码如下:#include "stdafx.h" void printf(char *p); void out(int a) { printf("haha"); }
那我把静态库改成如下重新生成,用户程序就链接成功了:#include "stdafx.h" void out(int a); int _tmain(int argc, _TCHAR* argv[]) { out(1); return 0; }
我就搞不清了,我生成的静态库中到底有没有printf的实现的代码?#include "stdafx.h" #include <iostream> void out(int a) { printf("haha"); }
[quote=引用 6 楼 wangdahu888 的回复:] 看一下 stdafx.h的文件内容,就明白了其实,你只要把你的 printf 声明去掉,就能够链接成功,不然,你试试// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 // #pragma once #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 #include <stdio.h> #include <tchar.h> // TODO: 在此处引用程序需要的其他头文件
void printf(char *p); 这里你自定义了一个printf, 却没有去实现它, 而你的out函数中又使用了这个函数, 那肯定就会提示找不到实现了, 当你去掉这掉这个void printf(char *p);函数声明, 你的out函数中使用的就是标准的C函数了, 所以能正常编译通过. 所以正像你说的, 你生成的静态库中确实没有void printf(char *p);这个自定义函数的实现, 只有C的标准printf的实现.
看一下 stdafx.h的文件内容,就明白了其实,你只要把你的 printf 声明去掉,就能够链接成功,不然,你试试// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 // #pragma once #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 #include <stdio.h> #include <tchar.h> // TODO: 在此处引用程序需要的其他头文件
#include <iostream>
void out(int a)
{
printf("haha");
}
用户程序cpp,工程设置不使用预编译头:
void out(int a);
int main(int argc, char* argv[])
{
out(1);
return 0;
}
怎么感觉像是静态库lib里面有printf()的代码一样啊?